Tumblr.pm

filter: Statementstmt 86.3% 101 / 117 Branchbran 35.7% 10 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.0% 22 / 25 Podpod 0.0% 0 / 4 totaltotal 76.4% 133 / 174 CC 15 SCAR 14.7
CC
15
Cov
77%
_session
20.0
__ANON__
6.0
_oauth_request
5.0
CRAP 4.4
« Tumblr/Blog.pm ↑ Summary Tumblr/User.pm »
1 package WWW::Tumblr;
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 16 our $VERSION = '5.1';
7  
8 =pod
9  
10 =head1 NAME
11  
12 WWW::Tumblr - Perl bindings for the Tumblr API
13  
14 =head1 VERSION
15  
16 5.00
17  
18 =head1 SYNOPSIS
19  
20 my $t = WWW::Tumblr->new(
21 consumer_key => $consumer_key,
22 secret_key => $secret_key,
23 token => $token,
24 token_secret => $token_secret,
25 );
26
27 my $blog = $t->blog('perlapi.tumblr.com');
28  
29 print Dumper $blog->info;
30  
31 =head1 DESCRIPTION
32  
33 This module makes use of some sort of the same models as the upstream API,
34 meaning that you will have User, Blog and Tagged methods:
35  
36 my $t = WWW::Tumblr->new(
37 consumer_key => $consumer_key,
38 secret_key => $secret_key,
39 token => $token,
40 token_secret => $token_secret,
41 );
42  
43 # Once you have a WWW::Tumblr object, you can get a WWW::Tumblr::Blog object
44 # by calling the blog() method from the former object:
45
46 my $blog = $t->blog('perlapi.tumblr.com');
47
48 # And then just use WWW::Tumblr::Blog methods from it:
49 if ( my $post = $blog->post( type => 'text', body => 'Hell yeah, son!' ) ) {
50 say "I have published post id: " . $post->{id};
51 } else {
52 print STDERR Dumper $blog->error;
53 die "I couldn't post it :(";
54 }
55  
56 You can also work directly with a L<WWW::Tumblr::Blog> class for example:
57  
58 # You will need to set base_hostname:
59 my $blog = WWW::Tumblr::Blog->new(
60 %four_tokens,
61 base_hostname => 'myblogontumblr.com'
62 );
63  
64 All operation methods on the entire API will return false in case of an
65 upstream error and you can check the status with C<error()>:
66  
67 die Dumper $blog->error unless $blog->info();
68  
69 On success, methods will return a hash reference with the JSON representation
70 of the upstream response. This behavior has not changed from previous versions
71 of this module.
72  
73 =head1 METHOD PARAMETERS
74  
75 All methods require the same parameters as the upstream API, passed as hash
76 where the keys are the request parameters and the values the corresponding
77 data.
78  
79 =head1 DOCUMENTATION
80  
81 Please refer to each module for further tips, tricks and slightly more detailed
82 documentation:
83  
84 =over
85  
86 =item *
87  
88 L<WWW::Tumblr::Blog>
89  
90 =item *
91  
92 L<WWW::Tumblr::User>
93  
94 =item *
95  
96 L<WWW::Tumblr::Tagged>
97  
98 =item *
99  
100 L<WWW::Tumblr::ResponseError>
101  
102 =back
103  
104 Take also a look at the C<t/> directory inside the distribution. There you can see
105 how you can do a bunch of things: get posts, submissions, post quotes, text,
106 etc, etc.
107  
108 =head1 AUTHORIZATION
109  
110 It is possible to generate authorization URLs and do the whole OAuth dance. Please
111 refer to the C<examples/> directory within the distribution to learn more.
112  
113 =head1 CAVEATS
114  
115 This is considered an experimental version of the module. The request engine
116 needs a complete rewrite, as well as proper documentation. The main author of the
117 module wanted to release it like this to have people interested on Tumblr and Perl
118 give it a spin.
119  
120 =head1 BUGS
121  
122 Please report as many as you want/can. File them up at GitHub:
123 L<https://github.com/damog/www-tumblr/issues/new>. Please don't use the CPAN RT.
124  
125 =head1 MODULE AND TUMBLR API VERSION NOTE
126  
127 This module supports Tumblr API v2, starting from module version 5. Since the
128 previous API was deprecated upstream anyway, there's no backwards compatibility
129 with < 5 versions.
130  
131 =head1 AUTHOR(S)
132  
133 L<David Moreno|http://damog.net/> is the main author and maintainer of this module.
134 The following amazing people have also contributed from version 5 onwards: Artem
135 Krivopolenov, Squeeks, Fernando Vezzosi.
136  
137 =head1 SEE ALSO
138  
139 =over
140  
141 =item *
142  
143 L<Net::OAuth> because, you know, we're based off it.
144  
145 =item *
146  
147 L<Moose>, likewise.
148  
149 =back
150  
151 =head1 COPYRIGHT and LICENSE
152  
153 This software is copyright (c) 2013 by David Moreno.
154
155 This is free software; you can redistribute it and/or modify it under
156 the same terms as the Perl 5 programming language system itself.
157  
158 =head1 DISCLAIMER
159  
160 The author is in no way affiliated to Tumblr or Yahoo! Inc. If either of them
161 want to show their appreciation for this work, they can contact the author directly
162 or donate a few of those billion dollars Yahoo! paid for Tumblr, to the Perl
163 Foundation at L<http://donate.perlfoundation.org/>.
164  
165 =cut
166  
167 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 16 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 16 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 16 ▶use Net::OAuth::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 16 ▶use WWW::Tumblr::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 16 ▶use WWW::Tumblr::Blog;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 16 ▶use WWW::Tumblr::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 16 ▶use WWW::Tumblr::Authentication;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 16 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 16 has 'consumer_key', is => 'rw', isa => 'Str';
179 16 has 'secret_key', is => 'rw', isa => 'Str';
180 16 has 'token', is => 'rw', isa => 'Str';
181 16 has 'token_secret', is => 'rw', isa => 'Str';
182  
183 16 has 'callback', is => 'rw', isa => 'Str';
184 16 has 'error', is => 'rw', isa => 'WWW::Tumblr::ResponseError';
185 28 ▶has 'ua', is => 'rw', isa => 'LWP::UserAgent', default => sub { LWP::UserAgent->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
186  
187 28 ▶has 'session_store', is => 'rw', isa => 'HashRef', default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
188  
189 has 'oauth', is => 'rw', isa => 'Net::OAuth::Client', default => sub {
190 28 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=3.1
191 Net::OAuth::Client->new(
192 $self->consumer_key,
193 $self->secret_key,
194 request_token_path => 'http://www.tumblr.com/oauth/request_token',
195 authorize_path => 'http://www.tumblr.com/oauth/authorize',
196 access_token_path => 'http://www.tumblr.com/oauth/access_token',
197 callback => $self->callback,
198 0 ▶ session => sub { if (@_ > 1) { $self->_session($_[0] => $_[1]) }; return $self->_session($_[0]) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
199 28 );
200 16 };
201  
202 sub user {
203 6 ▶ my ( $self ) = shift;
Subroutine user: called CC=1 SCAR=0.0
Pod undocumented
204 6 return WWW::Tumblr::User->new({
205 consumer_key => $self->consumer_key,
206 secret_key => $self->secret_key,
207 token => $self->token,
208 token_secret => $self->token_secret,
209 });
210 }
211  
212 sub blog {
213 7 ▶ my ( $self ) = shift;
Subroutine blog: called CC=2 SCAR=7.1
Pod undocumented
214 7 ▶ my $name = shift or croak "A blog host name is needed.";
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or croak("A blog host name is needed.") 0 7 7
215  
216 7 return WWW::Tumblr::Blog->new({
217 consumer_key => $self->consumer_key,
218 secret_key => $self->secret_key,
219 token => $self->token,
220 token_secret => $self->token_secret,
221 base_hostname => $name,
222 });
223 }
224  
225 sub tagged {
226 1 ▶ my $self = shift;
Subroutine tagged: called CC=1 SCAR=0.0
Pod undocumented
227 1 my $args = { @_ };
228  
229 1 return $self->_tumblr_api_request({
230 auth => 'apikey',
231 http_method => 'GET',
232 url_path => 'tagged',
233 extra_args => $args,
234 });
235 }
236  
237 sub oauth_tools {
238 0 ▶ my ( $self ) = shift;
Subroutine oauth_tools: not called CC=1 SCAR=6.9
Pod undocumented
239 0 return WWW::Tumblr::Authentication::OAuth->new(
240 consumer_key => $self->consumer_key,
241 secret_key => $self->secret_key,
242 callback => $self->callback,
243 );
244 }
245  
246 sub _tumblr_api_request {
247 28 ▶ my $self = shift;
Subroutine _tumblr_api_request: called CC=1 SCAR=0.0
248 28 my $r = shift; #args
249  
250 28 my $method_to_call = '_' . $r->{auth} . '_request';
251 return $self->$method_to_call(
252 $r->{http_method}, $r->{url_path}, $r->{extra_args}
253 28 );
254 }
255  
256 sub _none_request {
257 10 ▶ my $self = shift;
Subroutine _none_request: called CC=4 SCAR=15.4
258 10 my $method = shift;
259 10 my $url_path = shift;
260 10 my $params = shift;
261  
262 10 my $req;
263 10 ▶ if ( $method eq 'GET' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 10 0 10
elsif ($method eq 'POST') { } 0 0 0
264 10 print "Requesting... " .'http://api.tumblr.com/v2/' . $url_path, "\n";
265 10 $req = HTTP::Request->new(
266 $method => 'http://api.tumblr.com/v2/' . $url_path,
267 );
268 } elsif ( $method eq 'POST' ) {
269 0 Carp::croak "Unimplemented";
270 } else {
271 0 die "dude, wtf.";
272 }
273  
274 10 my $res = $self->ua->request( $req );
275  
276 10 ▶ if ( my $prev = $res->previous ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $res->previous) { } 10 0 10
277 10 return $prev;
278 0 } else { return $res };
279 }
280  
281 sub _apikey_request {
282 6 ▶ my $self = shift;
Subroutine _apikey_request: called CC=3 SCAR=12.0
283 6 my $method = shift;
284 6 my $url_path = shift;
285 6 my $params = shift;
286  
287 6 my $req; # request object
288 6 ▶ if ( $method eq 'GET' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 6 0 6
elsif ($method eq 'POST') { } 0 0 0
289 $req = HTTP::Request->new(
290 $method => 'http://api.tumblr.com/v2/' . $url_path . '?api_key='.$self->consumer_key . '&' .
291 6 ( join '&', map { $_ .'='. $params->{ $_} } keys %$params )
292 );
293 } elsif ( $method eq 'POST' ) {
294 0 Carp::croak "Unimplemented";
295 } else {
296 0 die "$method misunderstood";
297 }
298  
299 6 my $res = $self->ua->request( $req );
300  
301 }
302  
303 sub _oauth_request {
304 12 ▶ my $self = shift;
Subroutine _oauth_request: called CC=5 SCAR=16.2
305 12 my $method = shift;
306 12 my $url_path= shift;
307 12 my $params = shift;
308  
309 12 my $data = delete $params->{data};
310  
311 12 my $request = $self->oauth->_make_request(
312 'protected resource',
313 request_method => uc $method,
314 request_url => 'http://api.tumblr.com/v2/' . $url_path,
315 consumer_key => $self->consumer_key,
316 consumer_secret => $self->secret_key,
317 token => $self->token,
318 token_secret => $self->token_secret,
319 extra_params => $params,
320 );
321 12 $request->sign;
322  
323 12 my $authorization_signature = $request->to_authorization_header;
324  
325 12 my $message;
326 12 ▶ if ( $method eq 'GET' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 8 4 12
elsif ($method eq 'POST') { } 4 0 4
327 8 $message = GET 'http://api.tumblr.com/v2/' . $url_path . '?' . $request->normalized_message_parameters, 'Authorization' => $authorization_signature;
328 } elsif ( $method eq 'POST' ) {
329 $message = POST('http://api.tumblr.com/v2/' . $url_path,
330 Content_Type => 'form-data',
331 Authorization => $authorization_signature,
332 Content => [
333 4 ▶ %$params, ( $data ? do {
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 2 2 4
334 2 ▶ if (ref($data) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 2 0 2
335 2 my $i = -1;
336 2 map { $i++; 'data[' . $i .']' => [ $_ ] } @$data
337 } else {
338 0 'data' => [ $data ]
339 }
340 } : () )
341 ]);
342 }
343  
344 12 return $self->ua->request( $message );
345 }
346  
347 sub _session {
348 0 ▶ my $self = shift;
Subroutine _session: not called CC=4 SCAR=30.0
349  
350 0 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
elsif (@_ > 1) { } 0 0 0
351 0 $self->session_store($_[0]);
352 } elsif ( @_ > 1 ) {
353 0 $self->session_store->{$_[0]} = $_[1]
354 }
355 0 ▶ return $_[0] ? $self->session_store->{$_[0]} : $self->session_store;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
356 }
357  
358 16 1;
359  
« Tumblr/Blog.pm ↑ Summary Tumblr/User.pm »