Navigation Menu

Skip to content

Commit

Permalink
improved dom and json methods to cache data consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 24, 2012
1 parent 5e85909 commit 3dea656
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
18 changes: 10 additions & 8 deletions lib/Mojo/Message.pm
Expand Up @@ -124,11 +124,11 @@ sub dom {
my $self = shift;

return if $self->is_multipart;
my $dom = Mojo::DOM->new;
$dom->charset($self->content->charset);
$dom->parse($self->body);
my $dom = $self->{dom}
||= Mojo::DOM->new->charset($self->content->charset // undef)
->parse($self->body);

return @_ ? ($self->{dom} = $dom)->find(@_) : $dom;
return @_ ? $dom->find(@_) : $dom;
}

# DEPRECATED in Rainbow!
Expand Down Expand Up @@ -220,8 +220,8 @@ sub is_multipart { shift->content->is_multipart }
sub json {
my ($self, $pointer) = @_;
return if $self->is_multipart;
my $data = Mojo::JSON->new->decode($self->body);
return $pointer ? Mojo::JSON::Pointer->get($data, $pointer) : $data;
my $data = $self->{json} ||= Mojo::JSON->new->decode($self->body);
return $pointer ? Mojo::JSON::Pointer->new->get($data, $pointer) : $data;
}

# DEPRECATED in Rainbow!
Expand Down Expand Up @@ -626,7 +626,8 @@ Access message cookies, meant to be overloaded in a subclass.
Turns message body into a L<Mojo::DOM> object and takes an optional selector
to perform a C<find> on it right away, which returns a L<Mojo::Collection>
object.
object. Note that this method caches all data, so it should not be called
before the entire message body has been received.
# Perform "find" right away
say $message->dom('h1, h2, h3')->pluck('text');
Expand Down Expand Up @@ -726,7 +727,8 @@ Alias for L<Mojo::Content/"is_multipart">.
Decode JSON message body directly using L<Mojo::JSON> if possible, returns
C<undef> otherwise. An optional JSON Pointer can be used to extract a specific
value with L<Mojo::JSON::Pointer>.
value with L<Mojo::JSON::Pointer>. Note that this method caches all data, so
it should not be called before the entire message body has been received.
say $message->json->{foo}{bar}[23];
say $message->json('/foo/bar/23');
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/get.pm
Expand Up @@ -145,7 +145,7 @@ sub run {
sub _json {
my $json = Mojo::JSON->new;
return unless my $data = $json->decode(shift);
return unless defined($data = Mojo::JSON::Pointer->get($data, shift));
return unless defined($data = Mojo::JSON::Pointer->new->get($data, shift));
ref $data ~~ [qw(HASH ARRAY)] ? say($json->encode($data)) : _say($data);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Test/Mojo.pm
Expand Up @@ -153,14 +153,14 @@ sub json_has {
my ($self, $p, $desc) = @_;
$desc ||= qq{has value for JSON Pointer "$p"};
return $self->_test('ok',
Mojo::JSON::Pointer->contains($self->tx->res->json, $p), $desc);
Mojo::JSON::Pointer->new->contains($self->tx->res->json, $p), $desc);
}

sub json_hasnt {
my ($self, $p, $desc) = @_;
$desc ||= qq{has no value for JSON Pointer "$p"};
return $self->_test('ok',
!Mojo::JSON::Pointer->contains($self->tx->res->json, $p), $desc);
!Mojo::JSON::Pointer->new->contains($self->tx->res->json, $p), $desc);
}

sub message_is {
Expand Down
10 changes: 7 additions & 3 deletions t/mojo/response.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 373;
use Test::More tests => 375;

# "Quick Smithers. Bring the mind eraser device!
# You mean the revolver, sir?
Expand Down Expand Up @@ -709,6 +709,8 @@ is_deeply $res->json, {foo => 'bar', baz => [1, 2, 3]}, 'right JSON data';
is $res->json('/foo'), 'bar', 'right result';
is $res->json('/baz/1'), 2, 'right result';
is_deeply $res->json('/baz'), [1, 2, 3], 'right result';
$res->json->{baz}->[1] = 4;
is_deeply $res->json('/baz'), [1, 4, 3], 'right result';

# Parse response and extract HTML
$res = Mojo::Message::Response->new;
Expand All @@ -723,15 +725,17 @@ is $res->version, '1.1', 'right version';
is $res->dom->at('p')->text, 'foo', 'right value';
is $res->dom->at('p > a')->text, 'bar', 'right value';
is $res->dom('p')->first->text, 'foo', 'right value';
is_deeply [$res->dom('p > a')->pluck('text')->each], [qw(bar baz)],
'right values';
my @text = $res->dom('a')->pluck(replace_content => 'yada')
->first->root->find('p > a')->pluck('text')->each;
is_deeply \@text, [qw(yada yada)], 'right values';
is_deeply [$res->dom('p > a')->pluck('text')->each], [qw(bar baz)],
is_deeply [$res->dom('p > a')->pluck('text')->each], [qw(yada yada)],
'right values';
@text = $res->dom->find('a')->pluck(replace_content => 'test')
->first->root->find('p > a')->pluck('text')->each;
is_deeply \@text, [qw(test test)], 'right values';
is_deeply [$res->dom->find('p > a')->pluck('text')->each], [qw(bar baz)],
is_deeply [$res->dom->find('p > a')->pluck('text')->each], [qw(test test)],
'right values';

# Build DOM from response with charset
Expand Down

0 comments on commit 3dea656

Please sign in to comment.