Tumblr/Blog.pm

filter: Statementstmt 72.5% 45 / 62 Branchbran 35.0% 7 / 20 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 4 totaltotal 60.9% 64 / 105 CC 13 SCAR 22.0
CC
13
Cov
64%
post_reblog
20.0
_post
10.9
post_edit
6.0
CRAP 9.0
« Tumblr/API.pm ↑ Summary Tumblr.pm »
1 package WWW::Tumblr::Blog;
2 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 ▶use WWW::Tumblr::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 extends 'WWW::Tumblr';
9  
10 16 has 'base_hostname', is => 'rw', isa => 'Str', required => 1;
11  
12 16 tumblr_api_method info => [ 'GET', 'apikey' ];
13 16 tumblr_api_method avatar => [ 'GET', 'none', undef, 'size' ];
14 16 tumblr_api_method likes => [ 'GET', 'apikey'];
15 16 tumblr_api_method followers => [ 'GET', 'oauth' ];
16  
17 16 tumblr_api_method posts => [ 'GET', 'apikey', undef, 'type' ];
18 16 tumblr_api_method posts_queue => [ 'GET', 'oauth' ];
19 16 tumblr_api_method posts_draft => [ 'GET', 'oauth' ];
20 16 tumblr_api_method posts_submission => [ 'GET', 'oauth' ];
21  
22 16 tumblr_api_method post_delete => [ 'POST', 'oauth' ];
23  
24 # posting methods!
25  
26 16 my %post_required_params = (
27 text => 'body',
28 photo => { any => [qw(source data)] },
29 quote => 'quote',
30 link => 'url',
31 chat => 'conversation',
32 audio => { any => [qw(external_url data)] },
33 video => { any => [qw(embed data)] },
34 );
35  
36 7 ▶sub post {
Subroutine post: called CC=1 SCAR=0.0
Pod undocumented
37 7 my $self = shift;
38 7 my %args = @_;
39  
40 7 $self->_post( %args );
41 }
42  
43 7 ▶sub _post {
Subroutine _post: called CC=9 SCAR=23.9
44 7 my $self = shift;
45 7 my %args = @_;
46  
47 7 my $subr = join('/', split( /_/, [ split '::', [ caller( 1 ) ]->[3] ]->[-1] ) );
48  
49 Carp::croak "no type specified when trying to post"
50 7 ▶ unless $args{ type };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"type"} 0 7 7
51  
52 # check for required params per type:
53  
54 7 ▶ if ( $post_required_params{ $args{ type } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($post_required_params{$args{"type"}}) 7 0 7
55 7 my $req = $post_required_params{ $args{ type } };
56 7 ▶ if ( ref $req && ref $req eq 'HASH' && defined $req->{any} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $req and ref $req eq 'HASH' and defined $req->{'any'}) { } 3 4 7
Condition66%4/6
ref $req and ref $req eq "HASH" and defined $req->{"any"}
!ll&&!rl&&r
403
ref $req and ref $req eq "HASH"
!ll&&!rl&&r
403
MC/DC33%1/3
ref $req and ref $req eq "HASH" and defined $req->{"any"}
ref $req ref $req eq "HASH" defined $req->{"any"}
Truth table50%2/4
A and B and C
A= ref $reqB= ref $req eq "HASH"C= defined $req->{"any"}
ABCresult
0XX0
10X0
1100
1111
57 Carp::croak "Trying to post type ".$args{type}." without any of: " .
58 0 join( ' ', @{ $req->{any} } )
59 3 ▶ if scalar( grep { $args{ $_ } } @{ $req->{any} } ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar grep({$args{$_};} @{$$req{"any"};}) == 0 0 3 3
60 } else {
61 Carp::croak "Trying to post type ".$args{type}." without: $req"
62 4 ▶ unless defined $args{ $req };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$req} 0 4 4
63 }
64 }
65  
66 7 my $response = $self->_tumblr_api_request({
67 auth => 'oauth',
68 http_method => 'POST',
69 url_path => 'blog/' . $self->base_hostname . '/' . $subr,
70 extra_args => \%args,
71 });
72  
73 7 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 7 0 7
74 7 return decode_json( $response->decoded_content)->{response};
75 } else {
76 0 $self->error( WWW::Tumblr::ResponseError->new(
77 response => $response
78 ));
79 return
80 0 }
81 }
82  
83 0 ▶sub post_edit {
Subroutine post_edit: not called CC=2 SCAR=17.9
Pod undocumented
84 0 my $self = shift;
85 0 my %args = @_;
86 Carp::croak "no id specified when trying to edit a post!"
87 0 ▶ unless $args{ id };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"id"} 0 0 0
88  
89 0 $self->_post( %args );
90 }
91  
92 0 ▶sub post_reblog {
Subroutine post_reblog: not called CC=4 SCAR=30.0
Pod undocumented
93 0 my $self = shift;
94 0 my %args = @_;
95  
96 Carp::croak "no id specified when trying to reblog a post!"
97 0 ▶ unless $args{ id };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"id"} 0 0 0
98 Carp::croak "no reblog_key specified when trying to reblog a post!"
99 0 ▶ unless $args{ reblog_key };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"reblog_key"} 0 0 0
100  
101 # Reblog doesn't go through _post() as it doesn't need a type
102 0 my $response = $self->_tumblr_api_request({
103 auth => 'oauth',
104 http_method => 'POST',
105 url_path => 'blog/' . $self->base_hostname . '/post/reblog',
106 extra_args => \%args,
107 });
108  
109 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
110 0 return decode_json( $response->decoded_content)->{response};
111 } else {
112 0 $self->error( WWW::Tumblr::ResponseError->new(
113 response => $response
114 ));
115 0 return;
116 }
117 }
118  
119 0 ▶sub blog { Carp::croak "Unsupported" }
Subroutine blog: not called CC=1 SCAR=6.9
Pod undocumented
120  
121 16 1;
122  
123 =pod
124  
125 =head1 NAME
126  
127 WWW::Tumblr::Blog
128  
129 =head1 SYNOPSIS
130  
131 my $blog = $t->blog('stuff.tumblr.com');
132 # or the domain name:
133 # my $blog = $t->blog('myblogontumblrandstuff.com');
134  
135 # as per http://www.tumblr.com/docs/en/api/v2#blog-info
136 my $info = $blog->info;
137  
138 # as per http://www.tumblr.com/docs/en/api/v2#blog-likes
139 my $likes = $blog->likes;
140 my $likes = $blog->likes(
141 limit => 5,
142 offset => 10,
143 );
144  
145 # as per http://www.tumblr.com/docs/en/api/v2#photo-posts
146 my $posts = $blog->posts(
147 type => 'photo',
148 ... # etc
149 );
150  
151 # Posting to the blog:
152  
153 # using the source param:
154 my $post = $blog->post(
155 type => 'photo',
156 source => 'http://someserver.com/photo.jpg',
157 );
158  
159 # using local files with the data param
160 # which needs to be an array reference
161 my $post = $blog->post(
162 type => 'photo',
163 data => [ '/home/david/larry.jpg' ],
164 );
165  
166 # you can post multiple files, as per the Tumblr API:
167 my $post = $blog->post(
168 type => 'photo',
169 data => [ '/file1.jpg', 'file2.jpg', ... ],
170 );
171  
172 # if the result was false (empty list), then do something with the
173 # error:
174 do_something_with_the_error( $tumblr->error ) unless $post;
175 # or $likes
176 # or $info
177 # or anything else
178  
179 # Reblogging a post (photosets, text, anything):
180 # First get the post you want to reblog to obtain its reblog_key
181 my $source = $t->blog('someblog.tumblr.com');
182 my $posts = $source->posts(limit => 1);
183 my $original = $posts->{posts}[0];
184  
185 # Then reblog it to your blog:
186 my $reblog = $blog->post_reblog(
187 id => $original->{id},
188 reblog_key => $original->{reblog_key},
189 comment => 'Nice post!', # optional
190 );
191  
192 =head1 CAVEATS
193  
194 =over
195  
196 =item *
197  
198 I never really tried posting audios or videos.
199  
200 =item *
201  
202 B<Image format limitations:> This module uses Tumblr's legacy posting API which
203 only supports B<JPEG, PNG, and GIF> images. Formats like B<WebP are not supported>
204 by the legacy API. If you try to upload a webp file, you'll get:
205  
206 Error 400: Mime type "image/webp" not supported from file
207  
208 This is a Tumblr API limitation, not a bug in this module. Tumblr's web interface
209 uses their newer NPF (Neue Post Format) which supports more formats. NPF support
210 would require significant changes to this module and is tracked as a future
211 enhancement. See L<https://www.tumblr.com/docs/npf> for NPF documentation.
212  
213 =item *
214  
215 B<Video uploads:> Direct video file uploads have similar limitations. The legacy
216 API primarily supports video via embed URLs (YouTube, Vimeo, etc.) rather than
217 direct file uploads.
218  
219 =back
220  
221 =head1 BUGS
222  
223 Please refer to L<WWW::Tumblr>.
224  
225 =head1 AUTHOR(S)
226  
227 The same folks as L<WWW::Tumblr>.
228  
229 =head1 SEE ALSO
230  
231 L<WWW::Tumblr>, L<WWW::Tumblr::ResponseError>.
232  
233 =head1 COPYRIGHT and LICENSE
234  
235 Same as L<WWW::Tumblr>.
236  
237 =cut
« Tumblr/API.pm ↑ Summary Tumblr.pm »