Skip to content

Commit

Permalink
replaced ua method in Mojolicious::Controller with a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 8, 2013
1 parent aebf8eb commit 28aa59d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 39 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

4.13 2013-06-09
- Replaced ua method in Mojolicious::Controller with a helper.
- Fixed url_for support for absolute URLs without scheme or authority.
(bduggan, sri)

Expand Down
3 changes: 1 addition & 2 deletions lib/Mojolicious.pm
Expand Up @@ -72,8 +72,7 @@ sub new {
$r->hide(qw(AUTOLOAD DESTROY app cookie finish flash handler on param));
$r->hide(qw(redirect_to render render_exception render_maybe));
$r->hide(qw(render_not_found render_static rendered req res respond_to));
$r->hide(qw(send session signed_cookie stash tx ua url_for write));
$r->hide('write_chunk');
$r->hide(qw(send session signed_cookie stash tx url_for write write_chunk));

# Check if we have a log directory
my $mode = $self->mode;
Expand Down
34 changes: 0 additions & 34 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -374,8 +374,6 @@ sub stash {
return $self;
}

sub ua { shift->app->ua }

sub url_for {
my $self = shift;
my $target = shift // '';
Expand Down Expand Up @@ -860,38 +858,6 @@ that all stash values with a C<mojo.*> prefix are reserved for internal use.
# Remove value
my $foo = delete $c->stash->{foo};
=head2 ua
my $ua = $c->ua;
Get L<Mojo::UserAgent> object from L<Mojo/"ua">.
# Longer version
my $ua = $c->app->ua;
# Blocking
my $tx = $c->ua->get('http://example.com');
my $tx = $c->ua->post('example.com/login' => form => {user => 'mojo'});
# Non-blocking
$c->ua->get('http://example.com' => sub {
my ($ua, $tx) = @_;
$c->render(data => $tx->res->body);
});
# Parallel non-blocking
my $delay = Mojo::IOLoop->delay(sub {
my ($delay, @titles) = @_;
$c->render(json => \@titles);
});
for my $url ('http://mojolicio.us', 'https://metacpan.org') {
my $end = $delay->begin(0);
$c->ua->get($url => sub {
my ($ua, $tx) = @_;
$end->($tx->res->dom->html->head->title->text);
});
}
=head2 url_for
my $url = $c->url_for;
Expand Down
32 changes: 31 additions & 1 deletion lib/Mojolicious/Lite.pm
Expand Up @@ -839,21 +839,51 @@ variable.
=head2 User agent
With L<Mojolicious::Controller/"ua"> there's a full featured HTTP and
With L<Mojo::UserAgent>, which is available through the helper
L<Mojolicious::Plugin::DefaultHelpers/"ua">, there's a full featured HTTP and
WebSocket user agent built right in. Especially in combination with
L<Mojo::JSON> and L<Mojo::DOM> this can be a very powerful tool.
use Mojolicious::Lite;
# Blocking
get '/headers' => sub {
my $self = shift;
my $url = $self->param('url') || 'http://mojolicio.us';
my $dom = $self->ua->get($url)->res->dom;
$self->render(json => [$dom->find('h1, h2, h3')->pluck('text')->each]);
};
# Non-blocking
get '/title' => sub {
my $self = shift;
$self->ua->get('mojolicio.us' => sub {
my ($ua, $tx) = @_;
$self->render(data => $tx->res->dom->at('title')->text);
});
};
# Parallel non-blocking
get '/titles' => sub {
my $self = shift;
my $delay = Mojo::IOLoop->delay(sub {
my ($delay, @titles) = @_;
$self->render(json => \@titles);
});
for my $url ('http://mojolicio.us', 'https://metacpan.org') {
my $end = $delay->begin(0);
$self->ua->get($url => sub {
my ($ua, $tx) = @_;
$end->($tx->res->dom->html->head->title->text);
});
}
};
app->start;
For more information about the user agent see also
L<Mojolicious::Guides::Cookbook/"USER AGENT">.
=head2 WebSockets
WebSocket applications have never been this simple before. Just receive
Expand Down
8 changes: 7 additions & 1 deletion lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -26,12 +26,12 @@ sub register {
}

$app->helper(config => sub { shift->app->config(@_) });

$app->helper(content => \&_content);
$app->helper(content_for => \&_content_for);
$app->helper(current_route => \&_current_route);
$app->helper(dumper => \&_dumper);
$app->helper(include => \&_include);
$app->helper(ua => sub { shift->app->ua });
$app->helper(url_with => \&_url_with);
}

Expand Down Expand Up @@ -228,6 +228,12 @@ Alias for L<Mojolicious::Controller/"stash">.
Page title. All additional values get merged into the C<stash>.
=head2 ua
%= ua->get('mojolicio.us')->res->dom->at('title')->text
Alias for L<Mojo/"ua">.
=head2 url_for
%= url_for 'named', controller => 'bar', action => 'baz'
Expand Down
1 change: 0 additions & 1 deletion t/mojolicious/app.t
Expand Up @@ -94,7 +94,6 @@ ok $t->app->routes->is_hidden('signed_cookie'), 'is hidden';
ok $t->app->routes->is_hidden('stash'), 'is hidden';
ok $t->app->routes->is_hidden('tap'), 'is hidden';
ok $t->app->routes->is_hidden('tx'), 'is hidden';
ok $t->app->routes->is_hidden('ua'), 'is hidden';
ok $t->app->routes->is_hidden('url_for'), 'is hidden';
ok $t->app->routes->is_hidden('write'), 'is hidden';
ok $t->app->routes->is_hidden('write_chunk'), 'is hidden';
Expand Down

0 comments on commit 28aa59d

Please sign in to comment.