Skip to content

Commit

Permalink
more verbose examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 23, 2011
1 parent 5267c15 commit c58bed9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
25 changes: 16 additions & 9 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -601,7 +601,7 @@ Mojo::UserAgent - Non-Blocking I/O HTTP 1.1 And WebSocket User Agent
# WebSocket request
$ua->websocket('ws://websockets.org:8787' => sub {
my $tx = pop;
my ($self, $tx) = @_;
$tx->on_finish(sub { Mojo::IOLoop->stop });
$tx->on_message(sub {
my ($tx, $message) = @_;
Expand Down Expand Up @@ -794,7 +794,8 @@ L<Mojo::UserAgent::Transactor/"tx"> (except for the method).
You can also append a callback to perform requests non-blocking.
$ua->delete('http://kraih.com' => sub {
print pop->res->body;
my ($self, $tx) = @_;
print $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Expand All @@ -816,7 +817,8 @@ L<Mojo::UserAgent::Transactor/"tx"> (except for the method).
You can also append a callback to perform requests non-blocking.
$ua->get('http://kraih.com' => sub {
print pop->res->body;
my ($self, $tx) = @_;
print $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Expand All @@ -831,7 +833,8 @@ L<Mojo::UserAgent::Transactor/"tx"> (except for the method).
You can also append a callback to perform requests non-blocking.
$ua->head('http://kraih.com' => sub {
print pop->res->body;
my ($self, $tx) = @_;
print $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Expand All @@ -853,7 +856,8 @@ L<Mojo::UserAgent::Transactor/"tx"> (except for the method).
You can also append a callback to perform requests non-blocking.
$ua->post('http://kraih.com' => sub {
print pop->res->body;
my ($self, $tx) = @_;
print $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Expand All @@ -868,7 +872,8 @@ L<Mojo::UserAgent::Transactor/"form">.
You can also append a callback to perform requests non-blocking.
$ua->post_form('http://kraih.com' => {q => 'test'} => sub {
print pop->res->body;
my ($self, $tx) = @_;
print $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Expand All @@ -883,7 +888,8 @@ L<Mojo::UserAgent::Transactor/"tx"> (except for the method).
You can also append a callback to perform requests non-blocking.
$ua->put('http://kraih.com' => sub {
print pop->res->body;
my ($self, $tx) = @_;
print $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Expand All @@ -896,7 +902,8 @@ Process blocking transaction.
You can also append a callback to perform transactions non-blocking.
$ua->start($tx => sub {
print pop->res->body;
my ($self, $tx) = @_;
print $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
Expand All @@ -919,7 +926,7 @@ Open a non-blocking WebSocket connection with transparent handshake, takes
the exact same arguments as L<Mojo::UserAgent::Transactor/"websocket">.
$ua->websocket('ws://localhost:3000/echo' => sub {
my $tx = pop;
my ($self, $tx) = @_;
$tx->on_finish(sub { Mojo::IOLoop->stop });
$tx->on_message(sub {
my ($tx, $message) = @_;
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -1030,7 +1030,7 @@ A L<Mojo::UserAgent> prepared for the current environment.
# Non-blocking
$c->ua->get('http://mojolicio.us' => sub {
my $tx = pop;
my ($self, $tx) = @_;
$c->render_data($tx->res->body);
});
Expand All @@ -1042,7 +1042,8 @@ A L<Mojo::UserAgent> prepared for the current environment.
for my $url ('http://mojolicio.us', 'https://metacpan.org') {
$t->begin;
$c->ua->get($url => sub {
$t->end(pop->res->dom->html->head->title->text);
my ($self, $tx) = @_;
$t->end($tx->res->dom->html->head->title->text);
});
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -299,7 +299,7 @@ been initialized and before a connection gets associated with them.

# Add a witty header to every request
$ua->on_start(sub {
my $tx = pop;
my ($self, $tx) = @_;
$tx->req->headers->header('X-Bender' => 'Bite my shiny metal ass!');
print 'Request: ', $tx->req->url->clone->to_abs, "\n";
});
Expand Down Expand Up @@ -406,7 +406,7 @@ time.

# Fetch non-blocking just by adding a callback
$ua->get($url => sub {
my $tx = pop;
my ($self, $tx) = @_;

# Extract URLs
print "[$id] $url\n";
Expand Down

0 comments on commit c58bed9

Please sign in to comment.