Tumblr.pm

filter: Statementstmt 86.8% 119 / 137 Branchbran 37.5% 12 / 32 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 89.2% 25 / 28 Podpod 0.0% 0 / 4 totaltotal 76.2% 157 / 206 CC 19 SCAR 15.6
CC
19
Cov
77%
_session
20.0
_oauth_request
8.3
__ANON__
6.0
CRAP 4.7
« 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.4';
7  
8 =pod
9  
10 =head1 NAME
11  
12 WWW::Tumblr - Perl bindings for the Tumblr API
13  
14 =head1 VERSION
15  
16 5.4
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 JSON 'decode_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
171 16 ▶use Encode 'encode_utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
172 16 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 16 ▶use Net::OAuth::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 16 ▶use WWW::Tumblr::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 16 ▶use WWW::Tumblr::Blog;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 16 ▶use WWW::Tumblr::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
177 16 ▶use WWW::Tumblr::Authentication;
Subroutine BEGIN: called CC=1 SCAR=0.0
178 16 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
179  
180 16 has 'consumer_key', is => 'rw', isa => 'Str';
181 16 has 'secret_key', is => 'rw', isa => 'Str';
182 16 has 'token', is => 'rw', isa => 'Str';
183 16 has 'token_secret', is => 'rw', isa => 'Str';
184  
185 16 has 'callback', is => 'rw', isa => 'Str';
186 16 has 'error', is => 'rw', isa => 'WWW::Tumblr::ResponseError';
187 29 ▶has 'ua', is => 'rw', isa => 'LWP::UserAgent', default => sub { LWP::UserAgent->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
188  
189 29 ▶has 'session_store', is => 'rw', isa => 'HashRef', default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
190  
191 29 ▶has 'oauth', is => 'rw', isa => 'Net::OAuth::Client', default => sub {
Subroutine __ANON__: called CC=1 SCAR=3.1
192 29 my $self = shift;
193 Net::OAuth::Client->new(
194 $self->consumer_key,
195 $self->secret_key,
196 request_token_path => 'https://www.tumblr.com/oauth/request_token',
197 authorize_path => 'https://www.tumblr.com/oauth/authorize',
198 access_token_path => 'https://www.tumblr.com/oauth/access_token',
199 callback => $self->callback,
200 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
201 29 );
202 16 };
203  
204 6 ▶sub user {
Subroutine user: called CC=1 SCAR=0.0
Pod undocumented
205 6 my ( $self ) = shift;
206 6 return WWW::Tumblr::User->new({
207 consumer_key => $self->consumer_key,
208 secret_key => $self->secret_key,
209 token => $self->token,
210 token_secret => $self->token_secret,
211 });
212 }
213  
214 8 ▶sub blog {
Subroutine blog: called CC=2 SCAR=7.1
Pod undocumented
215 8 my ( $self ) = shift;
216 8 ▶ 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.") 8 0 8
217  
218 8 return WWW::Tumblr::Blog->new({
219 consumer_key => $self->consumer_key,
220 secret_key => $self->secret_key,
221 token => $self->token,
222 token_secret => $self->token_secret,
223 base_hostname => $name,
224 });
225 }
226  
227 1 ▶sub tagged {
Subroutine tagged: called CC=2 SCAR=7.6
Pod undocumented
228 1 my $self = shift;
229 1 my $args = { @_ };
230  
231 1 my $response = $self->_tumblr_api_request({
232 auth => 'apikey',
233 http_method => 'GET',
234 url_path => 'tagged',
235 extra_args => $args,
236 });
237  
238 1 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 1 0 1
239 1 return decode_json($response->decoded_content)->{response};
240 } else {
241 0 $self->error( WWW::Tumblr::ResponseError->new(
242 response => $response
243 ));
244 0 return;
245 }
246 }
247  
248 0 ▶sub oauth_tools {
Subroutine oauth_tools: not called CC=1 SCAR=6.9
Pod undocumented
249 0 my ( $self ) = shift;
250 0 return WWW::Tumblr::Authentication::OAuth->new(
251 consumer_key => $self->consumer_key,
252 secret_key => $self->secret_key,
253 callback => $self->callback,
254 );
255 }
256  
257 33 ▶sub _tumblr_api_request {
Subroutine _tumblr_api_request: called CC=1 SCAR=0.0
258 33 my $self = shift;
259 33 my $r = shift; #args
260  
261 33 my $method_to_call = '_' . $r->{auth} . '_request';
262 return $self->$method_to_call(
263 $r->{http_method}, $r->{url_path}, $r->{extra_args}
264 33 );
265 }
266  
267 10 ▶sub _none_request {
Subroutine _none_request: called CC=4 SCAR=15.4
268 10 my $self = shift;
269 10 my $method = shift;
270 10 my $url_path = shift;
271 10 my $params = shift;
272  
273 10 my $req;
274 10 ▶ if ( $method eq 'GET' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 10 0 10
elsif ($method eq 'POST') { } 0 0 0
275 # print "Requesting... " .'https://api.tumblr.com/v2/' . $url_path, "\n";
276  
277 10 my $request = $self->_oauth_request_sign(
278 $method, $url_path, $params
279 );
280 10 $req = HTTP::Request->new(
281 $method => 'https://api.tumblr.com/v2/' . $url_path,
282 [ Accept => 'application/json', Authorization => $request->to_authorization_header ]
283 );
284  
285 } elsif ( $method eq 'POST' ) {
286 0 Carp::croak "Unimplemented";
287 } else {
288 0 die "dude, wtf.";
289 }
290  
291 10 my $res = $self->ua->request( $req );
292  
293 10 ▶ if ( my $prev = $res->previous ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $res->previous) { } 10 0 10
294 10 return $prev;
295 0 } else { return $res };
296 }
297  
298 6 ▶sub _apikey_request {
Subroutine _apikey_request: called CC=3 SCAR=12.0
299 6 my $self = shift;
300 6 my $method = shift;
301 6 my $url_path = shift;
302 6 my $params = shift;
303  
304 6 my $req; # request object
305 6 ▶ if ( $method eq 'GET' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 6 0 6
elsif ($method eq 'POST') { } 0 0 0
306 $req = HTTP::Request->new(
307 $method => 'https://api.tumblr.com/v2/' . $url_path . '?api_key='.$self->consumer_key . '&' .
308 6 ( join '&', map { $_ .'='. $params->{ $_} } keys %$params )
309 );
310 } elsif ( $method eq 'POST' ) {
311 0 Carp::croak "Unimplemented";
312 } else {
313 0 die "$method misunderstood";
314 }
315  
316 6 my $res = $self->ua->request( $req );
317  
318 }
319  
320 27 ▶sub _oauth_request_sign {
Subroutine _oauth_request_sign: called CC=1 SCAR=0.0
321 27 my $self = shift;
322 27 my $method = shift;
323 27 my $url_path = shift;
324 27 my $params = shift;
325  
326 27 my $request = $self->oauth->_make_request(
327 'protected resource',
328 request_method => uc $method,
329 request_url => 'https://api.tumblr.com/v2/' . $url_path,
330 consumer_key => $self->consumer_key,
331 consumer_secret => $self->secret_key,
332 token => $self->token,
333 token_secret => $self->token_secret,
334 extra_params => $params,
335 );
336 27 $request->sign;
337 27 return $request;
338 }
339  
340 17 ▶sub _oauth_request {
Subroutine _oauth_request: called CC=8 SCAR=21.2
341 17 my $self = shift;
342 17 my $method = shift;
343 17 my $url_path= shift;
344 17 my $params = shift;
345  
346 17 my $data = delete $params->{data};
347  
348 17 my $request = $self->_oauth_request_sign(
349 $method, $url_path, $params
350 );
351  
352 17 my $authorization_header = $request->to_authorization_header;
353  
354 17 my $message;
355 17 ▶ if ( $method eq 'GET' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 8 9 17
elsif ($method eq 'POST') { } 9 0 9
356 8 $message = GET 'https://api.tumblr.com/v2/' . $url_path . '?' . $request->normalized_message_parameters, 'Authorization' => $authorization_header;
357 } elsif ( $method eq 'POST' ) {
358 # Encode string parameters to UTF-8 bytes (fixes UTF-8 posting, PR #15)
359 9 for my $key ( keys %$params ) {
360 14 ▶ next unless defined $params->{$key} && !ref($params->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{$key} and not ref $params->{$key} 0 14 14
Condition33%1/3
defined $params->{$key} and not ref $params->{$key}
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $params->{$key} and not ref $params->{$key}
defined $params->{$key} not ref $params->{$key}
Truth table33%1/3
A and not(B)
A= defined $params->{$key}B= not ref $params->{$key}
ABresult
0X0
100
111
361 14 $params->{$key} = encode_utf8($params->{$key});
362 }
363 $message = POST('https://api.tumblr.com/v2/' . $url_path,
364 Content_Type => 'form-data',
365 Authorization => $authorization_header,
366 Content => [
367 9 ▶ %$params, ( $data ? do {
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 2 7 9
368 2 ▶ if (ref($data) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 2 0 2
369 2 my $i = -1;
370 2 map { $i++; 'data[' . $i .']' => [ $_ ] } @$data
371 } else {
372 0 'data' => [ $data ]
373 }
374 } : () )
375 ]);
376 }
377  
378 17 my $res = $self->ua->request( $message );
379  
380 17 return $res;
381 }
382  
383 0 ▶sub _session {
Subroutine _session: not called CC=4 SCAR=30.0
384 0 my $self = shift;
385  
386 0 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
elsif (@_ > 1) { } 0 0 0
387 0 $self->session_store($_[0]);
388 } elsif ( @_ > 1 ) {
389 0 $self->session_store->{$_[0]} = $_[1]
390 }
391 0 ▶ return $_[0] ? $self->session_store->{$_[0]} : $self->session_store;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
392 }
393  
394 16 1;
« Tumblr/Blog.pm ↑ Summary Tumblr/User.pm »