Skip to content

Commit

Permalink
replaced Twitter with MetaCPAN in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 15, 2013
1 parent 4b9cf12 commit 78f1e0b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

4.15 2013-06-14
4.15 2013-06-16
- Fixed a few error reporting bugs in Mojo::IOLoop::Client and
Mojo::IOLoop::Server.

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -494,8 +494,8 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
}
# Quick JSON API request with Basic authentication
say $ua->get('https://sri:s3cret@search.twitter.com/search.json?q=perl')
->res->json('/results/0/text');
say $ua->get('https://sri:s3cret@example.com/search.json?q=perl')
->res->json('/results/0');
# Extract data from HTML and XML resources
say $ua->get('mojolicio.us')->res->dom->html->head->title->text;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/get.pm
Expand Up @@ -24,7 +24,7 @@ usage: $0 get [OPTIONS] URL [SELECTOR|JSON-POINTER] [COMMANDS]
mojo get mojolicio.us a attr href
mojo get mojolicio.us '*' attr id
mojo get mojolicio.us 'h1, h2, h3' 3 text
mojo get http://search.twitter.com/search.json /error
mojo get mojo get https://api.metacpan.org/v0/author/SRI /name
These options are available:
-C, --charset <charset> Charset of HTML/XML content, defaults to auto
Expand Down
56 changes: 25 additions & 31 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -322,25 +322,25 @@ latency backend web services.

use Mojolicious::Lite;

# Search Twitter for "perl"
# Search MetaCPAN for "mojolicious"
get '/' => sub {
my $self = shift;
$self->ua->get('http://search.twitter.com/search.json?q=perl' => sub {
$self->ua->get('api.metacpan.org/v0/module/_search?q=mojolicious' => sub {
my ($ua, $tx) = @_;
$self->render('twitter', results => $tx->res->json->{results});
$self->render('metacpan', hits => $tx->res->json->{hits}{hits});
});
};

app->start;
__DATA__

@@ twitter.html.ep
@@ metacpan.html.ep
<!DOCTYPE html>
<html>
<head><title>Twitter results for "perl"</title></head>
<head><title>MetaCPAN results for "mojolicious"</title></head>
<body>
% for my $result (@$results) {
<p><%= $result->{text} %></p>
% for my $hit (@$hits) {
<p><%= $hit->{_source}{release} %></p>
% }
</body>
</html>
Expand All @@ -352,7 +352,7 @@ L<Mojo::IOLoop> delay.
use Mojo::IOLoop;
use Mojo::URL;

# Search Twitter for "perl" and "python"
# Search MetaCPAN for "mojo" and "mango"
get '/' => sub {
my $self = shift;

Expand All @@ -362,17 +362,18 @@ L<Mojo::IOLoop> delay.
# Parallel requests
sub {
my $delay = shift;
my $url = Mojo::URL->new('http://search.twitter.com/search.json');
$self->ua->get($url->clone->query({q => 'perl'}) => $delay->begin);
$self->ua->get($url->clone->query({q => 'python'}) => $delay->begin);
my $url = Mojo::URL->new('api.metacpan.org/v0/module/_search');
$url->query({sort => 'date:desc'});
$self->ua->get($url->clone->query({q => 'mojo'}) => $delay->begin);
$self->ua->get($url->clone->query({q => 'mango'}) => $delay->begin);
},

# Delayed rendering
sub {
my ($delay, $perl, $python) = @_;
my ($delay, $mojo, $mango) = @_;
$self->render(json => {
perl => $perl->res->json('/results/0/text'),
python => $python->res->json('/results/0/text')
mojo => $mojo->res->json('/hits/hits/0/_source/release'),
mango => $mango->res->json('/hits/hits/0/_source/release')
});
}
);
Expand Down Expand Up @@ -696,12 +697,12 @@ Who actually controls the event loop backend is not important.
use EV;
use AnyEvent;

# Search Twitter for "perl"
# Search MetaCPAN for "mojolicious"
my $cv = AE::cv;
my $ua = Mojo::UserAgent->new;
$ua->get('http://search.twitter.com/search.json?q=perl' => sub {
$ua->get('api.metacpan.org/v0/module/_search?q=mojolicious' => sub {
my ($ua, $tx) = @_;
$cv->send($tx->res->json('/results/0/text'));
$cv->send($tx->res->json('/hits/hits/0/_source/release'));
});
say $cv->recv;

Expand Down Expand Up @@ -772,23 +773,16 @@ That's why L<Mojolicious> comes with the possibly fastest pure-Perl
implementation L<Mojo::JSON> built right in.

use Mojo::UserAgent;
use Mojo::Util 'encode';
use Mojo::URL;

# Fresh user agent
my $ua = Mojo::UserAgent->new;

# Fetch the latest news about Mojolicious from Twitter
my $search = 'http://search.twitter.com/search.json?q=Mojolicious';
for $tweet (@{$ua->get($search)->res->json->{results}}) {

# Tweet text
my $text = $tweet->{text};

# Twitter user
my $user = $tweet->{from_user};

# Show both
say encode('UTF-8', "$text --$user");
# Search MetaCPAN for "mojolicious" and list latest releases
my $url = Mojo::URL->new('http://api.metacpan.org/v0/release/_search');
$url->query({q => 'mojolicious', sort => 'date:desc'});
for my $hit (@{$ua->get($url)->res->json->{hits}{hits}}) {
say "$hit->{_source}{name} ($hit->{_source}{author})";
}

=head2 Basic authentication
Expand Down Expand Up @@ -1042,7 +1036,7 @@ You can follow redirects and view the headers for all messages.

Extract just the information you really need from JSON data structures.

$ mojo get http://search.twitter.com/search.json /error
$ mojo get mojo get https://api.metacpan.org/v0/author/SRI /name

This can be an invaluable tool for testing your applications.

Expand Down

0 comments on commit 78f1e0b

Please sign in to comment.