File Coverage

blib/lib/Net/Topsy.pm
Criterion Covered Total %
statement 25 25 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 34 34 100.0


line stmt bran cond sub pod time code
1 6     6   379554 use MooseX::Declare;
  6         10342543  
  6         21  
2              
3 6     6   141442 class Net::Topsy with Net::Topsy::Role::API {
  6     6   11  
  6     6   167  
  6     6   73870  
  6         18  
  6         60  
  6         24999  
  6         11  
  6         55  
  6         502  
4 6     6   257 use Carp qw/croak confess/;
  6         8  
  6         403  
5 6     6   24 use Moose;
  6         6  
  6         25  
6 6     6   9536 use URI::Escape;
  6         2708  
  6         383  
7 6     6   3450 use JSON::Any qw/XS DWIW JSON/;
  6         15827  
  6         23  
8             use Data::Dumper;
9             use LWP::UserAgent;
10             use Net::Topsy::Result;
11             our $VERSION = '0.01_01';
12             $VERSION = eval $VERSION;
13              
14             use namespace::autoclean;
15              
16             has useragent_class => ( isa => 'Str', is => 'ro', default => 'LWP::UserAgent' );
17             has useragent_args => ( isa => 'HashRef', is => 'ro', default => sub { {} } );
18             has ua => ( isa => 'Object', is => 'rw' );
19             has key => ( isa => 'Str', is => 'rw', required => 0 );
20             has format => ( isa => 'Str', is => 'rw', required => 1, default => '.json' );
21             has base_url => ( isa => 'Str', is => 'ro', default => 'http://otter.topsy.com' );
22             has useragent => ( isa => 'Str', is => 'ro', default => "Net::Topsy/$VERSION (Perl)" );
23              
24             method BUILD {
25             $self->ua($self->useragent_class->new(%{$self->useragent_args}));
26             $self->ua->agent($self->useragent);
27              
28             my @api_methods = keys %{$self->API->{$self->base_url}};
29              
30             for my $method (@api_methods) {
31             Net::Topsy->meta->make_mutable;
32             Net::Topsy->meta->add_method( substr($method, 1) , sub {
33             my ($self, $params) = @_;
34             $params ||= {};
35             return $self->_topsy_api($params, $method);
36             });
37             Net::Topsy->meta->make_immutable;
38             }
39             }
40              
41             method _topsy_api ($params, $route) {
42             die 'no route to _topsy_api!' unless $route;
43              
44             $self->_validate_params($params, $route);
45             my $url = $self->_make_url($params, $route);
46             return $self->_handle_response( $self->ua->get( $url ) );
47             }
48              
49             method _validate_params ($params, $route) {
50             my %topsy_api = %{$self->API};
51              
52             my $api_entry = $topsy_api{$self->base_url}{$route}
53             || croak "$route is not a topsy api entry";
54              
55             my @required = grep { $api_entry->{args}{$_} } keys %{$api_entry->{args}};
56              
57             if ( my @missing = grep { !exists $params->{$_} } @required ) {
58             croak "$route -> required params missing: @missing";
59             }
60              
61             if ( my @undefined = grep { $params->{$_} eq '' } keys %$params ) {
62             croak "params with undefined values: @undefined";
63             }
64              
65             my %unexpected_params = map { $_ => 1 } keys %$params;
66             delete $unexpected_params{$_} for keys %{$api_entry->{args}};
67             if ( my @unexpected_params = sort keys %unexpected_params ) {
68             # topsy seems to ignore unexpected params, so don't fail, just diag
69             print "# unexpected params: @unexpected_params\n" if $self->print_diags;
70             }
71              
72             }
73              
74             method _make_url ($params,$route) {
75             $route = $self->base_url . $route . $self->format;
76             my $url = $route . "?beta=" . ($self->key || '');
77             while( my ($k,$v) = each %$params) {
78             $url .= "&$k=" . uri_escape($v) . "&" if defined $v;
79             }
80             #warn "requesting $url";
81             return $url;
82             }
83              
84             method _handle_response ( $response ) {
85             if ($response->is_success) {
86              
87             my $perl = $self->_from_json( $response->content );
88              
89             my $result = Net::Topsy::Result->new(
90             response => $response,
91             json => $response->content,
92             perl => $perl,
93             );
94             return $result;
95             } else {
96             die $response->status_line;
97             }
98             }
99              
100             method _from_json ($json) {
101             my $perl = eval { JSON::Any->from_json($json) };
102             confess $@ if $@;
103             return $perl;
104             }
105              
106             }
107             =head1 NAME
108              
109             Net::Topsy - Perl Interface to the Otter API to Topsy.com
110              
111             =head1 VERSION
112              
113             Version 0.01_01
114              
115             =cut
116              
117              
118             =head1 SYNOPSIS
119              
120             use Net::Topsy;
121              
122             my $topsy = Net::Topsy->new( { key => $beta_key } );
123             my $search1 = $topsy->search( { q => 'perl' } );
124             my $search2 = $topsy->search( { q => 'lolcats', page => 3, perpage => 20 } );
125              
126              
127             All API methods take a hash reference of CGI parameters. These will be
128             URI-escaped, so that does not have to be done before calling these methods.
129              
130             Expect this API to change when Topsy is out of beta. Unknown parameters are
131             currently ignored by Topsy, but that could change at any time.
132              
133             =head1 METHODS
134              
135             =over
136              
137             =item authorinfo
138              
139             =item authorsearch
140              
141             =item credit
142              
143             =item linkposts
144              
145             =item profilesearch
146              
147             =item related
148              
149             =item stats
150              
151             =item search
152              
153             my $search = $topsy->search( { q => 'perl', window => 'd' } );
154              
155             Takes mantadory parameter "q", a string to search for, and the optional
156             parameter "window", which defaults to "a". Other options for the "window"
157             parameter are: "auto" - automagically pick the best window. Other choices: "h"
158             last hour, "d" last day, "w" last week, "m" last month, "a" all time.
159              
160             =item searchcount
161              
162             =item tags
163              
164             =item trackbacks
165              
166             =item trending
167              
168             my $trends = $topsy->trending;
169              
170             This method takes no arguments and returns a hash reference of trending terms.
171              
172             =item urlinfo
173              
174             =back
175              
176             =head1 AUTHOR
177              
178             Jonathan Leto, C<< <jonathan at leto.net> >>
179              
180             =head1 BUGS
181              
182             Please report any bugs or feature requests to C<bug-net-topsy at rt.cpan.org>,
183             or through the web interface at
184             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net::Topsy>. I will be
185             notified, and then you'll automatically be notified of progress on your bug as I
186             make changes.
187              
188             =head1 SUPPORT
189              
190             You can find documentation for this module with the perldoc command.
191              
192             perldoc Net::Topsy
193              
194             For documentation about the Otter API to Topsy.com : L<http://code.google.com/p/otterapi> .
195              
196             You can also look for information at:
197              
198             =over 4
199              
200             =item * RT: CPAN's request tracker
201              
202             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net::Topsy>
203              
204             =item * AnnoCPAN: Annotated CPAN documentation
205              
206             L<http://annocpan.org/dist/Net::Topsy>
207              
208             =item * CPAN Ratings
209              
210             L<http://cpanratings.perl.org/d/Net::Topsy>
211              
212             =item * Search CPAN
213              
214             L<http://search.cpan.org/dist/Net::Topsy>
215              
216             =back
217              
218              
219             =head1 ACKNOWLEDGEMENTS
220              
221             Many thanks to Marc Mims <marc@questright.com>, the author of Net::Twitter, for the
222             Mock::LWP::UserAgent module that mocks out LWP::UserAgent for the tests.
223              
224             =head1 COPYRIGHT & LICENSE
225              
226             Copyright 2009 Jonathan Leto <jonathan@leto.net>, all rights reserved.
227              
228             This program is free software; you can redistribute it and/or modify it
229             under the same terms as Perl itself.
230              
231              
232             =cut
233              
234             1;