Tumblr.pm

filter: Statementstmt 72.5% 132 / 182 Branchbran 54.4% 37 / 68 Conditioncond 35.0% 7 / 20 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 67.5% 25 / 37 Podpod 15.0% 3 / 20 totaltotal 60.5% 206 / 340 CC 47 SCAR 27.9
CC
47
Cov
65%
hash_refactor
54.7
pages
12.0
dispatch
6.1
CRAP 16.3
↑ Summary Tumblr/Result.pm »
1 package WebService::Tumblr;
2 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 $WebService::Tumblr::VERSION = '0.0010';
4 }
5 # ABSTRACT: A Perl interface to the Tumblr web API
6  
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use HTTP::Request::Common();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use URI::PathAbstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 our $json = JSON->new->pretty;
17 0 ▶sub json { $json }
Subroutine json: not called CC=1 SCAR=6.9
Pod undocumented
18  
19 3 ▶use WebService::Tumblr::Dispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use WebService::Tumblr::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use constant TUMBLR_URL => 'http://www.tumblr.com';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 14 ▶sub empty ($) {
Subroutine empty: called CC=2 SCAR=7.2
Pod undocumented
25 14 ▶ return not defined $_[0] && length $_[0];
Condition66%2/3
defined $_[0] && length $_[0]
!ll&&!rl&&r
608
MC/DC50%1/2
defined $_[0] && length $_[0]
defined $_[0] length $_[0]
Truth table66%2/3
A && B
A= defined $_[0]B= length $_[0]
ABresult
0X0
100
111
26 }
27  
28 3 ▶sub hash_refactor {
Subroutine hash_refactor: called CC=21 SCAR=40.0
Pod undocumented
29 3 my %arguments = @_;
30 3 my ( $hash, $key0, $else, $delete, $exclusive, $exists ) = @arguments{qw/ hash key else delete exclusive exists /};
31  
32 # FIXME Return value of key0
33 3 ▶ return unless defined $else;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $else 0 3 3
34 3 ▶ $else = [ $else ] unless ref $else eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $else eq "ARRAY" 0 3 3
35  
36 3 my @found;
37 3 for my $key ( $key0, @$else ) {
38 11 ▶ if ( $exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exists) { } 0 11 11
39 0 ▶ if ( $exists eq 'empty' ) { push @found, $key if ! empty $hash->{ $key } }
Branch: 2 branches
BranchTrueFalseTotal
if ($exists eq 'empty') { } 0 0 0
unless &empty($hash->{$key}) 0 0 0
40 0 ▶ else { push @found, $key if exists $hash->{ $key } }
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$key} 0 0 0
41 }
42 11 ▶ else { push @found, $key if $hash->{ $key } }
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{$key} 6 5 11
43 }
44  
45 3 ▶ return unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 0 3 3
46  
47 3 my $has;
48 3 ▶ if ( @found and $found[ 0 ] eq $key0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found and $found[0] eq $key0) 2 1 3
Condition66%2/3
@found and $found[0] eq $key0
!ll&&!rl&&r
012
MC/DC50%1/2
@found and $found[0] eq $key0
@found $found[0] eq $key0
Truth table66%2/3
A and B
A= @foundB= $found[0] eq $key0
ABresult
0X0
100
111
49 2 $has = 1;
50 2 shift @found;
51 }
52  
53 3 my $value;
54 3 ▶ if ( $exclusive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exclusive) 0 3 3
55 0 ▶ if ( $has && @found ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has and @found) 0 0 0
Condition0%0/3
$has and @found
!ll&&!rl&&r
000
MC/DC0%0/2
$has and @found
$has @found
Truth table0%0/3
A and B
A= $hasB= @found
ABresult
0X0
100
111
56 0 die "Exclusivity violated: $key0 @found";
57 }
58  
59 0 ▶ if ( @found > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found > 1) 0 0 0
60 0 die "Exclusivity violated: @found";
61 }
62 }
63  
64 3 ▶ if ( $has ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has) { } 2 1 3
65 2 ▶ if ( $delete ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete) 1 1 2
66 1 delete $hash->{ $_ } for @found;
67 }
68 2 return $hash->{ $key0 };
69 }
70 else {
71 1 my $value = $hash->{ $found[0] };
72 1 ▶ if ( $delete ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete) 0 1 1
73 0 delete $hash->{ $_ } for @found;
74 }
75 1 return $hash->{ $key0 } = $value;
76 }
77 }
78  
79 4 ▶sub _urlify ($) {
Subroutine _urlify: called CC=4 SCAR=13.9
80 4 my $given = shift;
81  
82 4 ▶ return if empty $given;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($given) 0 4 4
83  
84 4 ▶ if ( $given !~ m/\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($given =~ /\./) 2 2 4
85 2 $given = "$given.tumblr.com";
86 }
87 4 ▶ if ( $given !~ m/^[A-Za-z0-9]+:\/\// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($given =~ m[^[A-Za-z0-9]+://]) 3 1 4
88 3 $given = "http://$given";
89 }
90  
91 4 return $given;
92 }
93  
94 1 ▶sub _url_from ($) {
Subroutine _url_from: called CC=3 SCAR=11.2
95 1 my $self = shift;
96 1 my $given = shift;
97  
98 1 my ( $url, $blog ) = delete @$given{qw/ url blog /};
99  
100 1 ▶ return _urlify $url unless empty $url;
Branch: 1 branch
BranchTrueFalseTotal
unless &empty($url) 0 1 1
101 1 ▶ return _urlify $blog unless empty $blog;
Branch: 1 branch
BranchTrueFalseTotal
unless &empty($blog) 0 1 1
102 1 return;
103 }
104  
105 2 ▶sub _extract ($@) {
Subroutine _extract: called CC=3 SCAR=11.0
106 2 my $self = shift;
107 2 my $given = shift;
108  
109 2 my %return;
110 2 for ( @_ ) {
111 44 ▶ $return{ $_ } = delete $given->{ $_ } if exists $given->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
if exists $given->{$_} 0 44 44
112 }
113  
114 2 return \%return;
115 }
116  
117 1 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=14.3
Pod documented
118 1 my $self = shift;
119 1 my $given = shift;
120  
121 1 ▶ die "*** Two or more of: name, blog, url" if 1 < grep { ! empty $given->{ $_ } } qw/ name blog url /;
Branch: 1 branch
BranchTrueFalseTotal
if 1 < grep({not &empty($given->{$_});} "name", "blog", "url") 0 1 1
122 1 ▶ ! empty $given->{ $_ } and $self->url( $given->{ $_ } ) for qw/ name blog /;
Branch: 1 branch
BranchTrueFalseTotal
&empty($given->{$_}) or $self->url($given->{$_}) 2 0 2
123 }
124  
125 3 has secure => qw/ is rw default 1 /;
126  
127 3 has agent => qw/ is ro lazy_build 1 /;
128 0 ▶sub _build_agent {
Subroutine _build_agent: not called CC=1 SCAR=6.9
129 0 my $agent = LWP::UserAgent->new;
130 0 return $agent;
131 }
132  
133 3 has [qw/ email password /] => qw/ is rw /;
134  
135 3 has _name => qw/ is rw /;
136 3 ▶has url => qw/ is rw /, trigger => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
137 3 my ( $self, $value ) = @_;
138 3 my $url = _urlify $value;
139 3 ▶ if ( empty $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&empty($url)) 0 3 3
140 0 $self->_name( undef );
141 0 return undef;
142 }
143 3 my $uri = URI->new( $url );
144 3 my $host = $uri->host;
145 3 ▶ if ( $host =~ m/^([^\.]+)\.tumblr.com$/ ) { $self->_name( $1 ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /^([^\.]+)\.tumblr.com$/) { } 3 0 3
146 0 else { $self->_name( $host ) }
147 3 $self->{ url } = $url; # FIXME This is a hack, do this better
148 3 };
149  
150 4 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
151 4 my $self = shift;
152 4 ▶ return $self->_name unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 3 1 4
153 1 $self->url( @_ );
154 }
155  
156 0 ▶sub blog { return shift->name( @_ ) }
Subroutine blog: not called CC=1 SCAR=6.9
Pod undocumented
157  
158 1 ▶sub _url {
Subroutine _url: called CC=2 SCAR=7.0
159 1 my $self = shift;
160 1 my $url = $self->url;
161 1 ▶ die "*** Missing url" unless $self->url;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->url 0 1 1
162 1 return _urlify $url;
163 }
164  
165 2 ▶sub identity {
Subroutine identity: called CC=2 SCAR=6.9
Pod undocumented
166 2 my $self = shift;
167 2 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 1 2
168 1 my ( $email, $password ) = @_;
169 1 $self->email( $email );
170 1 $self->password( $password );
171 }
172 else {
173 1 return ( $self->email, $self->password );
174 }
175 }
176  
177 2 ▶sub new_dispatch {
Subroutine new_dispatch: called CC=1 SCAR=0.0
Pod undocumented
178 2 my $self = shift;
179 2 return WebService::Tumblr::Dispatch->new( tumblr => $self, @_ );
180 }
181  
182 2 ▶sub dispatch {
Subroutine dispatch: called CC=6 SCAR=18.1
Pod undocumented
183 2 my $self = shift;
184 2 my $arguments = shift;
185 2 my %options = @_;
186  
187 2 my $base = TUMBLR_URL;
188 2 $base = URI->new( $base );
189 2 ▶ $base->scheme( 'https' ) if $self->secure;
Branch: 1 branch
BranchTrueFalseTotal
if $self->secure 2 0 2
190  
191 2 ▶ if ( $options{ url } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"url"}) 1 1 2
192 1 $base = $self->_url_from( $arguments );
193 1 ▶ $base ||= $self->_url;
Condition33%1/3
$base ||= $self->_url
l!l&&r!l&&!r
010
MC/DC0%0/2
$base ||= $self->_url
$base $self->_url
Truth table33%1/3
A ||= B
A= $baseB= $self->_url
ABresult
1X1
011
000
194 }
195 2 my $url = URI::PathAbstract->new( $base, path => $options{ path } );
196 2 ▶ my $query = $self->_extract( $arguments, @{ $options{ passthrough } || [] } );
Condition50%1/2
$options{'passthrough'} || []
l!l
20
MC/DC0%0/1
$options{'passthrough'} || []
$options{'passthrough'}
Truth table50%1/2
A
A= $options{'passthrough'}
Aresult
11
00
197 2 my $dispatch = $self->new_dispatch( method => 'GET', url => $url, query => $query );
198 2 ▶ $dispatch->authenticate if $options{ authenticate };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"authenticate"} 1 1 2
199  
200 2 return $dispatch;
201 }
202  
203 1 ▶sub write {
Subroutine write: called CC=1 SCAR=0.0
Pod documented
204 1 my $self = shift;
205 1 my %arguments = @_;
206  
207 1 my $dispatch = $self->dispatch( \%arguments,
208 path => 'api/write',
209 passthrough => [qw/
210 email password type generator date private tags format group slug state send-to-twitter
211 post-id
212 title body
213 source data caption click-through-url
214 quote source
215 name url description
216 title conversation
217 embed data title caption
218 data externally-hosted-url caption
219 /],
220 authenticate => 1,
221 );
222  
223 1 return $dispatch;
224 }
225  
226 0 ▶sub edit {
Subroutine edit: not called CC=2 SCAR=17.9
Pod undocumented
227 0 my $self = shift;
228 0 my %arguments = @_;
229  
230 0 ▶ die "*** Missing post-id" unless $arguments{ 'post-id' };
Branch: 1 branch
BranchTrueFalseTotal
unless $arguments{"post-id"} 0 0 0
231  
232 0 return $self->write( @_ );
233 }
234  
235 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
236 0 my $self = shift;
237 0 my %arguments = @_;
238  
239 0 my $dispatch = $self->dispatch( \%arguments,
240 path => 'api/delete',
241 passthrough => [qw/ email password post-id /],
242 authenticate => 1,
243 );
244  
245 0 return $dispatch;
246 }
247  
248 0 ▶sub reblog {
Subroutine reblog: not called CC=1 SCAR=6.9
Pod undocumented
249 0 my $self = shift;
250 0 my %arguments = @_;
251  
252 0 my $dispatch = $self->dispatch( \%arguments,
253 path => 'api/delete',
254 passthrough => [qw/ email password post-id reblog-key comment as /],
255 authenticate => 1,
256 );
257  
258 0 return $dispatch;
259 }
260  
261 0 ▶sub pages {
Subroutine pages: not called CC=3 SCAR=24.8
Pod undocumented
262 0 my $self = shift;
263 0 my %arguments = @_;
264  
265 0 my $dispatch = $self->dispatch( \%arguments,
266 url => 1,
267 path => 'api/pages',
268 passthrough => [qw/ email password /],
269 );
270 0 ▶ $dispatch->authenticate if $arguments{ all } || $dispatch->query->{ state };
Branch: 1 branch
BranchTrueFalseTotal
if $arguments{"all"} or $dispatch->query->{"state"} 0 0 0
Condition0%0/3
$arguments{"all"} or $dispatch->query->{"state"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$arguments{"all"} or $dispatch->query->{"state"}
$arguments{"all"} $dispatch->query->{"state"}
Truth table0%0/3
A or B
A= $arguments{"all"}B= $dispatch->query->{"state"}
ABresult
1X1
011
000
271  
272 0 return $dispatch;
273 }
274  
275 1 ▶sub posts {
Subroutine posts: called CC=3 SCAR=11.8
Pod undocumented
276 1 my $self = shift;
277 1 my %arguments = @_;
278  
279 1 my $dispatch = $self->dispatch( \%arguments,
280 url => 1,
281 path => 'api/read',
282 passthrough => [qw/ email password start num type id filter tagged chrono search state /],
283 );
284 1 ▶ $dispatch->authenticate if $arguments{ all } || $dispatch->query->{ state };
Branch: 1 branch
BranchTrueFalseTotal
if $arguments{"all"} or $dispatch->query->{"state"} 0 1 1
Condition33%1/3
$arguments{"all"} or $dispatch->query->{"state"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$arguments{"all"} or $dispatch->query->{"state"}
$arguments{"all"} $dispatch->query->{"state"}
Truth table33%1/3
A or B
A= $arguments{"all"}B= $dispatch->query->{"state"}
ABresult
1X1
011
000
285  
286 1 return $dispatch;
287 }
288  
289 0 ▶sub read {
Subroutine read: not called CC=1 SCAR=6.9
Pod undocumented
290 0 my $self = shift;
291  
292 0 return $self->posts( @_ );
293 }
294  
295 0 ▶sub dashboard {
Subroutine dashboard: not called CC=1 SCAR=6.9
Pod undocumented
296 0 my $self = shift;
297 0 my %arguments = @_;
298  
299 0 my $dispatch = $self->dispatch( \%arguments,
300 path => 'api/dashboard',
301 passthrough => [qw/ email password start num filter likes /],
302 authenticate => 1,
303 );
304  
305 0 return $dispatch;
306 }
307  
308 0 ▶sub likes {
Subroutine likes: not called CC=1 SCAR=6.9
Pod undocumented
309 0 my $self = shift;
310 0 my %arguments = @_;
311  
312 0 my $dispatch = $self->dispatch( \%arguments,
313 path => 'api/dashboard',
314 passthrough => [qw/ email password start num filter /],
315 authenticate => 1,
316 );
317  
318 0 return $dispatch;
319 }
320  
321 0 ▶sub like {
Subroutine like: not called CC=1 SCAR=6.9
Pod undocumented
322 0 my $self = shift;
323 0 my %arguments = @_;
324  
325 0 my $dispatch = $self->dispatch( \%arguments,
326 path => 'api/like',
327 passthrough => [qw/ email password post-id reblog-key /],
328 authenticate => 1,
329 );
330  
331 0 return $dispatch;
332 }
333  
334 0 ▶sub authenticate {
Subroutine authenticate: not called CC=1 SCAR=6.9
Pod undocumented
335 0 my $self = shift;
336 0 my %arguments = @_;
337  
338 0 my $dispatch = $self->dispatch( \%arguments,
339 path => 'api/authenticate',
340 passthrough => [qw/ email password include-theme /],
341 authenticate => 1,
342 );
343  
344 0 return $dispatch;
345 }
346  
347 3 1;
348  
349 __END__
↑ Summary Tumblr/Result.pm »