TypePad.pm

filter: Statementstmt 44.7% 77 / 172 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 69.7% 23 / 33 Podpod 0.0% 0 / 6 totaltotal 36.2% 100 / 276 CC 26 SCAR 39.9
CC
26
Cov
34%
_call
182.0
call_upload
72.0
make_restricted_request
30.0
CRAP 54.3
↑ Summary TypePad/CodeGen.pm »
1 package WWW::TypePad;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.4002';
6  
7 1 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Net::OAuth::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use WWW::TypePad::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # TODO import flag to preload them all
17 1 ▶use WWW::TypePad::ApiKeys;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use WWW::TypePad::Applications;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use WWW::TypePad::Assets;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use WWW::TypePad::AuthTokens;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use WWW::TypePad::Blogs;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use WWW::TypePad::Events;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use WWW::TypePad::ExternalFeedSubscriptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use WWW::TypePad::Favorites;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use WWW::TypePad::Groups;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use WWW::TypePad::ImportJobs;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use WWW::TypePad::Relationships;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use WWW::TypePad::Users;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 has 'consumer_key' => ( is => 'rw' );
31 1 has 'consumer_secret' => ( is => 'rw' );
32 1 has 'access_token' => ( is => 'rw' );
33 1 has 'access_token_secret' => ( is => 'rw' );
34 1 has 'host' => ( is => 'rw', default => 'api.typepad.com' );
35 1 has '_oauth' => ( is => 'rw' );
36  
37 has 'ua' => (
38 is => 'rw',
39 isa => 'LWP::UserAgent',
40  
41 # All browsers must be an instance of an LWP::UserAgent, so that
42 # we can guarantee that we can disable redirects.
43 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 my $ua = LWP::UserAgent->new;
45 0 $ua->max_redirect( 0 );
46 0 return $ua;
47 },
48 0 ▶ trigger => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 my( $self, $ua, $attr ) = @_;
50 0 $ua->max_redirect( 0 );
51 },
52 1 );
53  
54 0 ▶sub oauth {
Subroutine oauth: not called CC=2 SCAR=17.9
Pod undocumented
55 0 my $api = shift;
56 0 ▶ unless ( defined $api->_oauth ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $api->_oauth) 0 0 0
57 0 my $apikey = $api->get_apikey( $api->consumer_key );
58 0 my $app = $apikey->{owner};
59  
60 my $oauth = Net::OAuth::Simple::AuthHeader->new(
61 tokens => {
62 consumer_key => $api->consumer_key,
63 consumer_secret => $api->consumer_secret,
64 access_token => $api->access_token,
65 access_token_secret => $api->access_token_secret,
66 },
67 urls => {
68 authorization_url => $app->{oauthAuthorizationUrl},
69 request_token_url => $app->{oauthRequestTokenUrl},
70 access_token_url => $app->{oauthAccessTokenUrl},
71 },
72 0 );
73  
74 # Substitute our own LWP::UserAgent instance for the OAuth browser.
75 0 $oauth->{browser} = $api->ua;
76  
77 0 $api->_oauth( $oauth );
78 }
79 0 return $api->_oauth;
80 }
81  
82 0 ▶sub get_apikey {
Subroutine get_apikey: not called CC=1 SCAR=6.9
Pod undocumented
83 0 my $api = shift;
84 0 my( $key ) = @_;
85 0 return $api->call_anon( GET => '/api-keys/' . $key . '.json' );
86 }
87  
88 0 ▶sub uri_for {
Subroutine uri_for: not called CC=2 SCAR=17.9
Pod undocumented
89 0 my $api = shift;
90 0 my( $path ) = @_;
91 0 ▶ $path = '/' . $path unless $path =~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/] 0 0 0
92 0 return 'http://' . $api->host . $path;
93 }
94  
95 0 ▶sub call {
Subroutine call: not called CC=1 SCAR=6.9
Pod undocumented
96 0 my $api = shift;
97 0 return $api->_call(0, @_);
98 }
99  
100 0 ▶sub call_anon {
Subroutine call_anon: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my $api = shift;
102 0 return $api->_call(1, @_);
103 }
104  
105 0 ▶sub _call {
Subroutine _call: not called CC=13 SCAR=52.0
106 0 my $api = shift;
107 0 my( $anon, $method, $uri, $qs ) = @_;
108 0 ▶ unless ( $uri =~ /^http/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri =~ /^http/) 0 0 0
109 0 $uri = $api->uri_for( $uri );
110 }
111 0 ▶ if ( $method eq 'GET'&& $qs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "GET" and $qs) 0 0 0
Condition0%0/3
$method eq "GET" and $qs
!ll&&!rl&&r
000
MC/DC0%0/2
$method eq "GET" and $qs
$method eq "GET" $qs
Truth table0%0/3
A and B
A= $method eq "GET"B= $qs
ABresult
0X0
100
111
112 0 $uri = URI->new( $uri );
113 0 $uri->query_form( $qs );
114 }
115 0 my $res;
116 0 ▶ if ( $api->access_token && !$anon ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($api->access_token and not $anon) { } 0 0 0
Condition0%0/3
$api->access_token and not $anon
!ll&&!rl&&r
000
MC/DC0%0/2
$api->access_token and not $anon
$api->access_token not $anon
Truth table0%0/3
A and not(B)
A= $api->access_tokenB= not $anon
ABresult
0X0
100
111
117 0 my %extra;
118 0 ▶ if (($method eq 'POST' or $method eq 'PUT') and $qs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "POST" || $method eq "PUT" and $qs) 0 0 0
Condition0%0/6
$method eq "POST" || $method eq "PUT" and $qs
!ll&&!rl&&r
000
$method eq "POST" or $method eq "PUT"
l!l&&r!l&&!r
000
MC/DC0%0/3
$method eq "POST" || $method eq "PUT" and $qs
$method eq "POST" $method eq "PUT" $qs
Truth table0%0/5
A or B and C
A= $method eq "POST"B= $method eq "PUT"C= $qs
ABCresult
00X0
1X00
0100
1X11
0111
119 0 $extra{ContentBody} = JSON::encode_json($qs);
120 0 $extra{ContentType} = 'application/json';
121 }
122  
123 0 my $oauth = $api->oauth;
124 0 $res = $oauth->make_restricted_request( $uri, $method, %extra );
125
126 0 ▶ if ( $res->is_redirect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_redirect) 0 0 0
127 0 $res = $oauth->make_restricted_request(
128 $res->header( 'Location' ), $method, %extra
129 );
130 }
131 } else {
132 0 my $req = HTTP::Request->new( $method => $uri );
133 0 $res = $api->ua->request( $req );
134
135 0 ▶ if ( $res->is_redirect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_redirect) 0 0 0
136 0 $req = HTTP::Request->new( $method => $res->header( 'Location' ) );
137 0 $res = $api->ua->request( $req );
138 }
139 }
140  
141 0 ▶ unless ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
142 0 WWW::TypePad::Error::HTTP->throw( $res->code, $res->content );
143 }
144  
145 0 ▶ return 1 if $res->code == 204;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 204 0 0 0
146 0 return JSON::decode_json( $res->content );
147 }
148  
149 0 ▶sub call_upload {
Subroutine call_upload: not called CC=8 SCAR=42.8
Pod undocumented
150 0 my $api = shift;
151 0 my( $form ) = @_;
152  
153 0 ▶ croak "call_upload requires an access token"
Branch: 1 branch
BranchTrueFalseTotal
unless $api->access_token 0 0 0
154 unless $api->access_token;
155  
156 my $target_uri = delete $form->{target_url}
157 0 ▶ or croak "call_upload requires a target_url";
Branch: 1 branch
BranchTrueFalseTotal
my $target_uri = delete $form->{"target_url"} or croak("call_upload requires a target_url") 0 0 0
158  
159 my $filename = delete $form->{filename}
160 0 ▶ or croak "call_upload requires a filename";
Branch: 1 branch
BranchTrueFalseTotal
my $filename = delete $form->{"filename"} or croak("call_upload requires a filename") 0 0 0
161  
162 0 ▶ my $asset = delete $form->{asset} || {};
Condition0%0/2
delete $form->{'asset'} || {}
l!l
00
MC/DC0%0/1
delete $form->{'asset'} || {}
delete $form->{'asset'}
Truth table0%0/2
A
A= delete $form->{'asset'}
Aresult
11
00
163 0 $asset = JSON::encode_json( $asset );
164  
165 0 my $uri = URI->new( $api->uri_for( '/browser-upload.json' ) );
166 0 $uri->scheme( 'https' );
167  
168 # Construct the OAuth parameters to get a signature.
169 0 my $nonce = Net::OAuth::Simple::AuthHeader->_nonce;
170 0 my $oauth_req = Net::OAuth::ProtectedResourceRequest->new(
171 consumer_key => $api->consumer_key,
172 consumer_secret => $api->consumer_secret,
173 token => $api->access_token,
174 token_secret => $api->access_token_secret,
175 request_url => $uri->as_string,
176 request_method => 'POST',
177 signature_method => 'HMAC-SHA1',
178 timestamp => time,
179 nonce => $nonce,
180 );
181 0 $oauth_req->sign;
182  
183 # Send all of the OAuth parameters in the query string.
184 0 $uri->query_form( $oauth_req->to_hash );
185  
186 # And now, construct the actual HTTP::Request object that contains
187 # all of the fields we need to send.
188 0 my $req = POST $uri,
189 'Content-Type' => 'multipart/form-data',
190 Content => [
191 # Fake the redirect_to, since we just want to capture the
192 # 302 response, and not actually follow the redirect.
193 redirect_to => 'http://example.com/none',
194  
195 target_url => $target_uri,
196 asset => $asset,
197 file => [ $filename ],
198 ];
199  
200 # The response to an upload is always a redirect; if it's anything
201 # else, this indicates some internal error we weren't planning for,
202 # so bail early.
203 0 my $res = $api->ua->request( $req );
204 0 ▶ unless ( $res->code == RC_FOUND && $res->header( 'Location' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->code == 302 and $res->header("Location")) 0 0 0
Condition0%0/3
$res->code == 302 and $res->header("Location")
!ll&&!rl&&r
000
MC/DC0%0/2
$res->code == 302 and $res->header("Location")
$res->code == 302 $res->header("Location")
Truth table0%0/3
A and B
A= $res->code == 302B= $res->header("Location")
ABresult
0X0
100
111
205 0 WWW::TypePad::Error::HTTP->throw( $res );
206 }
207  
208 # Otherwise, extract the response from the Location header. Successful
209 # uploads will result in a status=201 query string parameter...
210 0 my $loc = URI->new( $res->header( 'Location' ) );
211 0 my %form = $loc->query_form;
212 0 ▶ unless ( $form{status} == RC_CREATED ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($form{"status"} == 201) 0 0 0
213 0 WWW::TypePad::Error::HTTP->throw( $form{status}, $form{error} );
214 }
215  
216 # ... and an asset_url, which we can GET to get back an asset
217 # dictionary.
218 0 my $asset_uri = $form{asset_url};
219 0 return $api->call_anon( GET => $asset_uri );
220 }
221  
222 package Net::OAuth::Simple::AuthHeader;
223 # we need Net::OAuth::Simple to make requests with the OAuth credentials
224 # in an Authorization header, as required by the API, rather than the query string
225  
226 1 ▶use base qw( Net::OAuth::Simple );
Subroutine BEGIN: called CC=1 SCAR=0.0
227  
228 0 ▶sub make_restricted_request {
Subroutine make_restricted_request: not called CC=5 SCAR=34.0
229 0 my $self = shift;
230 0 ▶ croak $Net::OAuth::Simple::UNAUTHORIZED unless $self->authorized;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->authorized 0 0 0
231  
232 0 my( $url, $method, %extras ) = @_;
233 # Use SSL.
234 0 $url =~ s/^http:/https:/;
235  
236 0 my $uri = URI->new( $url );
237 0 my %query = $uri->query_form;
238 0 $uri->query_form( {} );
239  
240 0 $method = lc $method;
241  
242 0 my $content_body = delete $extras{ContentBody};
243 0 my $content_type = delete $extras{ContentType};
244  
245 0 ▶ my $request = Net::OAuth::ProtectedResourceRequest->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->oauth_1_0a ? : 0 0 0
246 consumer_key => $self->consumer_key,
247 consumer_secret => $self->consumer_secret,
248 request_url => $uri,
249 request_method => uc( $method ),
250 signature_method => $self->signature_method,
251 protocol_version => $self->oauth_1_0a ?
252 Net::OAuth::PROTOCOL_VERSION_1_0A :
253 Net::OAuth::PROTOCOL_VERSION_1_0,
254 timestamp => time,
255 nonce => $self->_nonce,
256 token => $self->access_token,
257 token_secret => $self->access_token_secret,
258 extra_params => { %query, %extras },
259 );
260 0 $request->sign;
261 0 ▶ die "COULDN'T VERIFY! Check OAuth parameters.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $request->verify 0 0 0
262 unless $request->verify;
263  
264 0 my $request_url = URI->new( $url );
265  
266 0 my $req = HTTP::Request->new(uc($method) => $request_url);
267 0 $req->header('Authorization' => $request->to_authorization_header);
268 0 ▶ if ($content_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_body) 0 0 0
269 0 $req->content_type($content_type);
270 0 $req->content_length(length $content_body);
271 0 $req->content($content_body);
272 }
273  
274 0 my $response = $self->{browser}->request($req);
275 0 return $response;
276 }
277  
278 1 1;
279 __END__
↑ Summary TypePad/CodeGen.pm »