Tumblr.pm

filter: Statementstmt 87.1% 108 / 124 Branchbran 35.7% 10 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.4% 23 / 26 Podpod 0.0% 0 / 4 totaltotal 77.4% 141 / 182 CC 15 SCAR 14.5
CC
15
Cov
78%
_session
20.0
__ANON__
6.0
_oauth_request
5.0
CRAP 4.3
« 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.3';
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 # print "Requesting... " .'https://api.tumblr.com/v2/' . $url_path, "\n";
265  
266 10 my $request = $self->_oauth_request_sign(
267 $method, $url_path, $params
268 );
269 10 $req = HTTP::Request->new(
270 $method => 'http://api.tumblr.com/v2/' . $url_path,
271 [ Accept => 'application/json', Authorization => $request->to_authorization_header ]
272 );
273  
274 } elsif ( $method eq 'POST' ) {
275 0 Carp::croak "Unimplemented";
276 } else {
277 0 die "dude, wtf.";
278 }
279  
280 10 my $res = $self->ua->request( $req );
281  
282 10 ▶ if ( my $prev = $res->previous ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $res->previous) { } 10 0 10
283 10 return $prev;
284 0 } else { return $res };
285 }
286  
287 sub _apikey_request {
288 6 ▶ my $self = shift;
Subroutine _apikey_request: called CC=3 SCAR=12.0
289 6 my $method = shift;
290 6 my $url_path = shift;
291 6 my $params = shift;
292  
293 6 my $req; # request object
294 6 ▶ if ( $method eq 'GET' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 6 0 6
elsif ($method eq 'POST') { } 0 0 0
295 $req = HTTP::Request->new(
296 $method => 'http://api.tumblr.com/v2/' . $url_path . '?api_key='.$self->consumer_key . '&' .
297 6 ( join '&', map { $_ .'='. $params->{ $_} } keys %$params )
298 );
299 } elsif ( $method eq 'POST' ) {
300 0 Carp::croak "Unimplemented";
301 } else {
302 0 die "$method misunderstood";
303 }
304  
305 6 my $res = $self->ua->request( $req );
306  
307 }
308  
309 sub _oauth_request_sign {
310 22 ▶ my $self = shift;
Subroutine _oauth_request_sign: called CC=1 SCAR=0.0
311 22 my $method = shift;
312 22 my $url_path = shift;
313 22 my $params = shift;
314  
315 22 my $request = $self->oauth->_make_request(
316 'protected resource',
317 request_method => uc $method,
318 request_url => 'http://api.tumblr.com/v2/' . $url_path,
319 consumer_key => $self->consumer_key,
320 consumer_secret => $self->secret_key,
321 token => $self->token,
322 token_secret => $self->token_secret,
323 extra_params => $params,
324 );
325 22 $request->sign;
326 22 return $request;
327 }
328  
329 sub _oauth_request {
330 12 ▶ my $self = shift;
Subroutine _oauth_request: called CC=5 SCAR=16.2
331 12 my $method = shift;
332 12 my $url_path= shift;
333 12 my $params = shift;
334  
335 12 my $data = delete $params->{data};
336  
337 12 my $request = $self->_oauth_request_sign(
338 $method, $url_path, $params
339 );
340  
341 12 my $authorization_header = $request->to_authorization_header;
342  
343 12 my $message;
344 12 ▶ if ( $method eq 'GET' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 8 4 12
elsif ($method eq 'POST') { } 4 0 4
345 8 $message = GET 'http://api.tumblr.com/v2/' . $url_path . '?' . $request->normalized_message_parameters, 'Authorization' => $authorization_header;
346 } elsif ( $method eq 'POST' ) {
347 $message = POST('http://api.tumblr.com/v2/' . $url_path,
348 Content_Type => 'form-data',
349 Authorization => $authorization_header,
350 Content => [
351 4 ▶ %$params, ( $data ? do {
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 2 2 4
352 2 ▶ if (ref($data) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 2 0 2
353 2 my $i = -1;
354 2 map { $i++; 'data[' . $i .']' => [ $_ ] } @$data
355 } else {
356 0 'data' => [ $data ]
357 }
358 } : () )
359 ]);
360 }
361  
362 12 my $res = $self->ua->request( $message );
363  
364 12 return $res;
365 }
366  
367 sub _session {
368 0 ▶ my $self = shift;
Subroutine _session: not called CC=4 SCAR=30.0
369  
370 0 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
elsif (@_ > 1) { } 0 0 0
371 0 $self->session_store($_[0]);
372 } elsif ( @_ > 1 ) {
373 0 $self->session_store->{$_[0]} = $_[1]
374 }
375 0 ▶ return $_[0] ? $self->session_store->{$_[0]} : $self->session_store;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
376 }
377  
378 16 1;
« Tumblr/Blog.pm ↑ Summary Tumblr/User.pm »