Skip to content

Commit

Permalink
use the result method in all examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 9, 2017
1 parent 789b94c commit beaa053
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

7.25 2017-02-06
7.25 2017-02-09

7.24 2017-02-05
- Added open method to Mojo::File.
Expand Down
42 changes: 21 additions & 21 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -340,29 +340,29 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
use Mojo::UserAgent;
# Say hello to the Unicode snowman and include an Accept header
my $ua = Mojo::UserAgent->new;
say $ua->get('www.☃.net?hello=there' => {Accept => '*/*'})->res->body;
# Fine grained response handling (dies on connection errors)
my $ua = Mojo::UserAgent->new;
my $res = $ua->get('mojolicious.org/perldoc')->result;
if ($res->is_success) { say $res->body }
elsif ($res->is_error) { say $res->message }
elsif ($res->code == 301) { say $res->headers->location }
else { say 'Whatever...' }
# Say hello to the Unicode snowman and include an Accept header
say $ua->get('www.☃.net?hello=there' => {Accept => '*/*'})->result->body;
# Extract data from HTML and XML resources with CSS selectors
say $ua->get('www.perl.org')->res->dom->at('title')->text;
say $ua->get('www.perl.org')->result->dom->at('title')->text;
# Scrape the latest headlines from a news site
say $ua->get('blogs.perl.org')
->res->dom->find('h2 > a')->map('text')->join("\n");
->result->dom->find('h2 > a')->map('text')->join("\n");
# IPv6 PUT request with Content-Type header and content
my $tx = $ua->put('[::1]:3000' => {'Content-Type' => 'text/plain'} => 'Hi!');
# Quick JSON API request with Basic authentication
my $value = $ua->get('https://sri:s3cret@example.com/test.json')->res->json;
my $value = $ua->get('https://sri:t3st@example.com/test.json')->result->json;
# JSON POST (application/json) with TLS certificate authentication
my $tx = $ua->cert('tls.crt')->key('tls.key')
Expand All @@ -371,12 +371,12 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
# Search DuckDuckGo anonymously through Tor
$ua->proxy->http('socks://127.0.0.1:9050');
say $ua->get('api.3g2upl4pq6kufc4m.onion/?q=mojolicious&format=json')
->res->json('/Abstract');
->result->json('/Abstract');
# Follow redirects to download Mojolicious from GitHub
$ua->max_redirects(5)
->get('https://www.github.com/kraih/mojo/tarball/master')
->res->content->asset->move_to('/home/sri/mojo.tar.gz');
->result->content->asset->move_to('/home/sri/mojo.tar.gz');
# Form POST (application/x-www-form-urlencoded) with manual exception handling
my $tx = $ua->post('https://metacpan.org/search' => form => {q => 'mojo'});
Expand All @@ -390,7 +390,7 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
# Non-blocking request
$ua->get('mojolicious.org' => sub {
my ($ua, $tx) = @_;
say $tx->res->dom->at('title')->text;
say $tx->result->dom->at('title')->text;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Expand All @@ -403,8 +403,8 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
},
sub {
my ($delay, $mojo, $cpan) = @_;
say $mojo->res->dom->at('title')->text;
say $cpan->res->dom->at('title')->text;
say $mojo->result->dom->at('title')->text;
say $cpan->result->dom->at('title')->text;
}
)->wait;
Expand Down Expand Up @@ -631,7 +631,7 @@ L<Mojo::UserAgent::Server> object.
my $c = shift;
$c->render(json => {now => time});
});
my $time = $ua->get('/time')->res->json->{now};
my $time = $ua->get('/time')->result->json->{now};
# Change log level
$ua->server->app->log->level('fatal');
Expand Down Expand Up @@ -729,7 +729,7 @@ implied). You can also append a callback to perform requests non-blocking.
$ua->delete('http://example.com' => json => {a => 'b'} => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
say $tx->result->body;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Expand All @@ -749,7 +749,7 @@ perform requests non-blocking.
$ua->get('http://example.com' => json => {a => 'b'} => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
say $tx->result->body;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Expand All @@ -769,7 +769,7 @@ implied). You can also append a callback to perform requests non-blocking.
$ua->head('http://example.com' => json => {a => 'b'} => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
say $tx->result->body;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Expand All @@ -789,7 +789,7 @@ implied). You can also append a callback to perform requests non-blocking.
$ua->options('http://example.com' => json => {a => 'b'} => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
say $tx->result->body;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Expand All @@ -809,7 +809,7 @@ implied). You can also append a callback to perform requests non-blocking.
$ua->patch('http://example.com' => json => {a => 'b'} => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
say $tx->result->body;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Expand All @@ -829,7 +829,7 @@ implied). You can also append a callback to perform requests non-blocking.
$ua->post('http://example.com' => json => {a => 'b'} => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
say $tx->result->body;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Expand All @@ -849,7 +849,7 @@ perform requests non-blocking.
$ua->put('http://example.com' => json => {a => 'b'} => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
say $tx->result->body;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Expand All @@ -864,7 +864,7 @@ to perform requests non-blocking.
my $tx = $ua->build_tx(GET => 'http://example.com');
$ua->start($tx => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
say $tx->result->body;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/version.pm
Expand Up @@ -33,7 +33,7 @@ EOF
# Check latest version on CPAN
my $latest = eval {
$self->app->ua->max_redirects(10)->tap(sub { $_->proxy->detect })
->get('api.metacpan.org/v0/release/Mojolicious')->res->json->{version};
->get('api.metacpan.org/v0/release/Mojolicious')->result->json->{version};
} or return;

my $msg = 'This version is up to date, have fun!';
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -451,7 +451,7 @@ latency backend web services.
my $c = shift;
$c->ua->get('api.metacpan.org/v0/module/_search?q=mojolicious' => sub {
my ($ua, $tx) = @_;
$c->render('metacpan', hits => $tx->res->json->{hits}{hits});
$c->render('metacpan', hits => $tx->result->json->{hits}{hits});
});
};

Expand Down Expand Up @@ -503,8 +503,8 @@ style.
sub {
my ($delay, $mojo, $minion) = @_;
$c->render(json => {
mojo => $mojo->res->json('/hits/hits/0/_source/release'),
minion => $minion->res->json('/hits/hits/0/_source/release')
mojo => $mojo->result->json('/hits/hits/0/_source/release'),
minion => $minion->result->json('/hits/hits/0/_source/release')
});
}
);
Expand Down Expand Up @@ -572,7 +572,7 @@ created at startup time.
Mojo::IOLoop->recurring(10 => sub {
app->ua->get('http://mojolicious.org' => sub {
my ($ua, $tx) = @_;
$title = $tx->res->dom->at('title')->text;
$title = $tx->result->dom->at('title')->text;
});
});

Expand Down

0 comments on commit beaa053

Please sign in to comment.