File Coverage

blib/lib/Net/Topsy.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


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