Easy.pm

filter: Statementstmt 39.5% 53 / 134 Branchbran 13.6% 9 / 66 Conditioncond 6.2% 2 / 32 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 46.6% 14 / 30 Podpod 85.0% 17 / 20 totaltotal 31.3% 95 / 303 CC 47 SCAR 33.0
CC
47
Cov
28%
make_request
110.0
process_access_token_input
56.0
add_auth_headers
30.0
CRAP 27.0
↑ Summary Easy/Roles/Types.pm »
1 package Net::OAuth::Easy;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Net::OAuth::Easy::VERSION = '0.001_07';
4 }
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Digest::MD5 qw{md5_hex};
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 require Net::OAuth;
8 1 require HTTP::Request;
9  
10 # ABSTRACT: A moose class that abstracts Net::OAuth for you
11  
12  
13 1 with qw{
14 Net::OAuth::Easy::Roles::Types
15 };
16  
17  
18 has ua => (
19 is => 'rw',
20 isa => 'LWP::UserAgent',
21 lazy => 1,
22 0 ▶ default => sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 require LWP::UserAgent;
24 0 LWP::UserAgent->new;
25 },
26 1 );
27  
28  
29 has protocol => (
30 is => 'rw',
31 isa => 'OAuthProtocol',
32 lazy => 1,
33 2 ▶ default => sub{'1.0a'},
Subroutine __ANON__: called CC=1 SCAR=0.0
34 1 trigger => \&set_net_oauth_protocol,
35 );
36 2 ▶sub set_net_oauth_protocol {
Subroutine set_net_oauth_protocol: called CC=2 SCAR=7.0
Pod undocumented
37 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶ $Net::OAuth::PROTOCOL_VERSION = (shift->protocol eq '1.0a') ? &Net::OAuth::PROTOCOL_VERSION_1_0A : &Net::OAuth::PROTOCOL_VERSION_1_0;
Branch: 1 branch
BranchTrueFalseTotal
(shift())->protocol eq '1.0a' ? : 2 0 2
39 }
40  
41 2 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
42 2 my $self = shift;
43 2 $self->set_net_oauth_protocol;
44 }
45  
46  
47 has $_ => (
48 is => 'rw',
49 isa => 'Str',
50 predicate => qq{has_$_},
51 clearer => qq{clear_$_},
52 1 ) for qw{ consumer_key consumer_secret };
53  
54  
55 has $_ => (
56 is => 'rw',
57 isa => 'ValidURI',
58 predicate => qq{has_$_},
59 clearer => qq{clear_$_},
60 1 ) for qw{ request_token_url authorize_token_url access_token_url callback };
61  
62  
63 1 has request_method => (
64 is => 'rw',
65 isa => 'RequestMethod',
66 default => 'GET',
67 );
68  
69  
70 1 has signature_method => (
71 is => 'rw',
72 isa => 'SignatureMethod',
73 default => 'HMAC-SHA1',
74 );
75  
76  
77 1 has signature_key => (
78 is => 'rw',
79 isa => 'SignatureKey',
80 coerce => 1,
81 predicate => 'has_signature_key',
82 clearer => 'clear_signature_key',
83 );
84  
85  
86 1 ▶sub timestamp { time };
Subroutine timestamp: called CC=1 SCAR=0.0
Pod documented
87  
88  
89 3 ▶sub nonce { md5_hex( join '', rand(2**32), time, rand(2**32) ); };
Subroutine nonce: called CC=1 SCAR=0.0
Pod documented
90  
91  
92 has request_parameters => (
93 is => 'rw',
94 isa => 'HashRef[ArrayRef]',
95 2 ▶ default => sub{{ request_token => [qw{consumer_key
Subroutine __ANON__: called CC=1 SCAR=0.0
96 consumer_secret
97 request_url
98 request_method
99 signature_key
100 signature_method
101 protocol_version
102 timestamp
103 nonce
104 callback
105 token
106 token_secret
107 verifier
108 }],
109 access_token => [qw{consumer_key
110 consumer_secret
111 request_url
112 request_method
113 protocol_version
114 signature_key
115 signature_method
116 timestamp
117 nonce
118 token
119 token_secret
120 verifier
121 }],
122  
123 protected_resource => [qw{
124 consumer_key
125 consumer_secret
126 request_url
127 request_method
128 signature_key
129 signature_method
130 protocol_version
131 timestamp
132 nonce
133 token
134 token_secret
135 }],
136 #verifier
137 }},
138 1 );
139  
140  
141 has exception_handle => (
142 is => 'rw',
143 isa => 'CodeRef',
144 0 ▶ default => sub{sub{shift;die @_}},
Subroutine __ANON__: called CC=1 SCAR=2.6
Subroutine __ANON__: not called CC=1 SCAR=2.6
145 1 );
146  
147  
148 1 ▶sub build_request {
Subroutine build_request: called CC=3 SCAR=12.7
Pod documented
149 1 my $self = shift;
150 1 my $type = shift;
151 1 my $request = Net::OAuth->request($type)->new($self->gather_request_parts($type => @_));
152  
153 1 ▶ $self->exception_handle->( q{Unable to sign request} )
Branch: 1 branch
BranchTrueFalseTotal
unless $request->sign 0 1 1
154 unless $request->sign;
155  
156 1 ▶ $self->exception_handle->( q{Unable to verify request} )
Branch: 1 branch
BranchTrueFalseTotal
unless $request->verify 0 0 0
157 unless $request->verify;
158  
159 0 return $request;
160 }
161  
162  
163 1 ▶sub gather_request_parts {
Subroutine gather_request_parts: called CC=8 SCAR=21.7
Pod documented
164 1 my $self = shift;
165 1 my $type = shift;
166 1 my %opts = @_;
167  
168 # use type to grab the right url
169 1 my $url_method = sprintf q{%s_url}, $type;
170 1 ▶ $opts{request_url} ||= $self->can($url_method) ? $self->$url_method : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->can($url_method) ? : 1 0 1
Condition33%1/3
$opts{"request_url"} ||= $self->can($url_method) ? $self->$url_method : undef
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{"request_url"} ||= $self->can($url_method) ? $self->$url_method : undef
$opts{"request_url"} $self->can($url_method) ? $self->$url_method : undef
Truth table33%1/3
A ||= B
A= $opts{"request_url"}B= $self->can($url_method) ? $self->$url_method : undef
ABresult
1X1
011
000
171  
172 # pull any overrides from %opts/@_ everything else is pulled from $self
173 13 ▶ my %req = map{ $_ => ( exists $opts{$_} ) ? delete $opts{$_} : ( $self->can($_) ) ? $self->$_ : undef;
Branch: 2 branches
BranchTrueFalseTotal
exists $opts{$_} ? : 1 12 13
$self->can($_) ? : 8 4 12
174 1 ▶ } @{$self->request_parameters->{ $type } || [] };
Condition50%1/2
$self->request_parameters->{$type} || []
l!l
10
MC/DC0%0/1
$self->request_parameters->{$type} || []
$self->request_parameters->{$type}
Truth table50%1/2
A
A= $self->request_parameters->{$type}
Aresult
11
00
175 # TODO: this is likely not what we really want in cases where you pass Content, NOS builds the URL and then plucks from that, possibly more accurate?
176 1 ▶ $req{extra_params} = \%opts if scalar(keys %opts); # save off anything left from @_ as extra params
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 1 1
177  
178 1 ▶ $req{protocol_version} = ($self->protocol eq '1.0a') ? &Net::OAuth::PROTOCOL_VERSION_1_0A : &Net::OAuth::PROTOCOL_VERSION_1_0 ;
Branch: 1 branch
BranchTrueFalseTotal
$self->protocol eq '1.0a' ? : 1 0 1
179
180 1 return %req;
181 }
182  
183  
184 1 has response => (
185 is => 'rw',
186 isa => 'Object', # TODO: this is too vague
187 predicate => 'has_response',
188 clearer => 'clear_response',
189 );
190  
191  
192 0 ▶sub content {
Subroutine content: not called CC=2 SCAR=17.9
Pod documented
193 0 my $self = shift;
194 0 ▶ ( $self->has_response ) ? $self->response->content : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_response ? : 0 0 0
195 }
196  
197  
198 0 ▶sub success {
Subroutine success: not called CC=2 SCAR=17.9
Pod documented
199 0 my $self = shift;
200 0 ▶ return ( $self->has_response ) ? $self->response->is_success : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_response ? : 0 0 0
201 }
202  
203  
204 0 ▶sub failure { ! shift->success };
Subroutine failure: not called CC=1 SCAR=6.9
Pod documented
205  
206  
207 0 ▶sub error{
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
208 0 my $self = shift;
209 0 ▶ return ($self->failure) ? join qq{\n}, map{$self->response->$_} qw{status_line content} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->failure ? : 0 0 0
210 }
211  
212  
213 0 ▶sub make_request {
Subroutine make_request: not called CC=10 SCAR=47.0
Pod documented
214 0 my $self = shift;
215 0 my $content;
216 # find content if it was passed
217 0 ▶ for (my $i=0; $i<scalar(@_); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @_) 0 0 0
218 0 ▶ if (defined $_[$i] && $_[$i] =~ m/^Content$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[$i] and $_[$i] =~ /^Content$/i) 0 0 0
Condition0%0/3
defined $_[$i] and $_[$i] =~ /^Content$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[$i] and $_[$i] =~ /^Content$/i
defined $_[$i] $_[$i] =~ /^Content$/i
Truth table0%0/3
A and B
A= defined $_[$i]B= $_[$i] =~ /^Content$/i
ABresult
0X0
100
111
219 0 $content = delete $_[$i+1];
220 0 delete $_[$i];
221 0 last;
222 }
223 }
224 0 ▶ $self->clear_response if $self->has_response;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_response 0 0 0
225 0 ▶ my $request = ( ref($_[0]) && $_[0]->isa('Net::OAuth::Message') ) ? $_[0] : $self->build_request(grep { defined }@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && $_[0]->isa('Net::OAuth::Message') ? : 0 0 0
Condition0%0/3
ref $_[0] and $_[0]->isa("Net::OAuth::Message")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0]->isa("Net::OAuth::Message")
ref $_[0] $_[0]->isa("Net::OAuth::Message")
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0]->isa("Net::OAuth::Message")
ABresult
0X0
100
111
226  
227 0 ▶ my $req = HTTP::Request->new( $request->request_method => ( $request->request_method eq 'GET' && !$self->include_auth_header_for_GET )
Branch: 1 branch
BranchTrueFalseTotal
$request->request_method eq 'GET' && !$self->include_auth_header_for_GET ? : 0 0 0
Condition0%0/3
$request->request_method eq "GET" and not $self->include_auth_header_for_GET
!ll&&!rl&&r
000
MC/DC0%0/2
$request->request_method eq "GET" and not $self->include_auth_header_for_GET
$request->request_method eq "GET" not $self->include_auth_header_for_GET
Truth table0%0/3
A and not(B)
A= $request->request_method eq "GET"B= not $self->include_auth_header_for_GET
ABresult
0X0
100
111
228 ? $request->to_url
229 : $request->request_url
230 );
231 0 ▶ $req->content($content) if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 0 0 0
232 0 return $self->add_auth_headers($req, $request);
233 }
234  
235  
236 1 has [qw{oauth_header_realm oauth_header_separator}] => (
237 is => 'rw',
238 isa => 'Maybe[Str]',
239 );
240  
241  
242 1 has include_auth_header_for_GET => (
243 is => 'rw',
244 isa => 'Bool',
245 default => 0,
246 );
247  
248 0 ▶sub build_auth_header {
Subroutine build_auth_header: not called CC=3 SCAR=24.8
Pod undocumented
249 0 my ($self,$oauth_req) = @_;
250 0 ▶ $oauth_req->to_authorization_header(
Branch: 2 branches
BranchTrueFalseTotal
defined $self->oauth_header_realm ? : 0 0 0
defined $self->oauth_header_separator ? : 0 0 0
251 (defined $self->oauth_header_realm) ? $self->oauth_header_realm : undef ,
252 (defined $self->oauth_header_separator) ? $self->oauth_header_separator : undef ,
253 );
254 };
255  
256  
257 0 ▶sub add_auth_headers {
Subroutine add_auth_headers: not called CC=5 SCAR=34.0
Pod documented
258 0 my ($self, $http_req, $oauth_req) = @_;
259 0 ▶ $self->exception_handle( 'HTTP::Request expected as first paramater') unless $http_req->isa('HTTP::Request');
Branch: 1 branch
BranchTrueFalseTotal
unless $http_req->isa("HTTP::Request") 0 0 0
260 0 ▶ $self->exception_handle( 'Net::OAuth::Message expected as second paramater') unless $oauth_req->isa('Net::OAuth::Message');
Branch: 1 branch
BranchTrueFalseTotal
unless $oauth_req->isa("Net::OAuth::Message") 0 0 0
261 0 ▶ $http_req->authorization( $self->build_auth_header($oauth_req)
Branch: 1 branch
BranchTrueFalseTotal
if $http_req->method eq "POST" or $self->include_auth_header_for_GET 0 0 0
Condition0%0/3
$http_req->method eq "POST" or $self->include_auth_header_for_GET
l!l&&r!l&&!r
000
MC/DC0%0/2
$http_req->method eq "POST" or $self->include_auth_header_for_GET
$http_req->method eq "POST" $self->include_auth_header_for_GET
Truth table0%0/3
A or B
A= $http_req->method eq "POST"B= $self->include_auth_header_for_GET
ABresult
1X1
011
000
262 ) if $http_req->method eq 'POST' || $self->include_auth_header_for_GET;
263 0 return $http_req;
264 }
265  
266  
267 0 ▶sub send_request {
Subroutine send_request: not called CC=3 SCAR=24.8
Pod documented
268 0 my $self = shift;
269 0 ▶ my $req = ( ref($_[0]) && $_[0]->isa('HTTP::Request') ) ? $_[0] : $self->make_request(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && $_[0]->isa('HTTP::Request') ? : 0 0 0
Condition0%0/3
ref $_[0] and $_[0]->isa("HTTP::Request")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0]->isa("HTTP::Request")
ref $_[0] $_[0]->isa("HTTP::Request")
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0]->isa("HTTP::Request")
ABresult
0X0
100
111
270 0 $self->response( $self->ua->request( $req ) );
271 }
272  
273  
274 has $_ => (
275 is => 'rw',
276 isa => 'Str',
277 predicate => qq{has_$_},
278 clearer => qq{clear_$_},
279 1 ) for qw{request_token request_token_secret access_token access_token_secret};
280  
281  
282 0 ▶sub get_request_token {
Subroutine get_request_token: not called CC=2 SCAR=17.9
Pod documented
283 0 my $self = shift;
284 0 $self->send_request(request_token => @_);
285 0 ▶ if ($self->success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->success) 0 0 0
286 0 my $resp = Net::OAuth->response('request token')->from_post_body($self->response->content);
287 0 $self->request_token( $resp->token );
288 0 $self->request_token_secret( $resp->token_secret );
289 }
290 0 return $self->success;
291 }
292  
293
294 0 ▶sub get_authorization_url {
Subroutine get_authorization_url: not called CC=3 SCAR=24.8
Pod documented
295 0 my $self = shift;
296 0 my %opts = @_;
297 0 ▶ $opts{oauth_token} ||= $self->request_token;
Condition0%0/3
$opts{"oauth_token"} ||= $self->request_token
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"oauth_token"} ||= $self->request_token
$opts{"oauth_token"} $self->request_token
Truth table0%0/3
A ||= B
A= $opts{"oauth_token"}B= $self->request_token
ABresult
1X1
011
000
298 0 ▶ $opts{callback} ||= $self->callback;
Condition0%0/3
$opts{"callback"} ||= $self->callback
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"callback"} ||= $self->callback
$opts{"callback"} $self->callback
Truth table0%0/3
A ||= B
A= $opts{"callback"}B= $self->callback
ABresult
1X1
011
000
299 0 my $url = URI->new( $self->authorize_token_url );
300 0 $url->query_form( %opts );
301 0 return $url;
302 }
303  
304  
305 0 ▶sub process_authorization_callback {
Subroutine process_authorization_callback: not called CC=3 SCAR=24.8
Pod documented
306 0 my $self = shift;
307 0 ▶ my $url = (ref($_[0]) eq '') ? URI->new($_[0]) : $_[0]; # if we are handed a string build a uri object of it
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq '' ? : 0 0 0
308 0 my %opts = $url->query_form;
309 0 for ( grep{! m/^oauth_/} keys %opts ) {
310 0 delete $opts{$_};
311 }
312 0 return %opts;
313 }
314  
315  
316 has process_access_token_mapping => (
317 is => 'rw',
318 isa => 'HashRef[ArrayRef]',
319 auto_deref => 1,
320 2 ▶ default => sub{{ token => [qw{oauth_token request_token}],
Subroutine __ANON__: called CC=1 SCAR=0.0
321 token_secret => [qw{request_token_secret}],
322 verifier => [qw{oauth_verifier}],
323 }},
324 1 );
325  
326  
327 0 ▶sub process_access_token_input {
Subroutine process_access_token_input: not called CC=7 SCAR=40.3
Pod documented
328 0 my $self = shift;
329 0 my %opts = @_;
330 0 my %mapp = $self->process_access_token_mapping;
331 0 ▶ while ( my ( $key, $map ) = each %mapp ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %mapp) 0 0 0
332 0 ▶ next if exists $opts{$key}; # dont overwrite anything that was passed to us (respect overwrites)
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{$key} 0 0 0
333 0 for my $lookup ( @$map ) {
334 0 ▶ my $value = ( exists $opts{$lookup} ) ? delete $opts{$lookup}
Branch: 2 branches
BranchTrueFalseTotal
exists $opts{$lookup} ? : 0 0 0
$self->can($lookup) ? : 0 0 0
335 : ( $self->can($lookup) ) ? $self->$lookup
336 : undef;
337 0 $opts{$key} = $value;
338 0 ▶ next if $value; # stop looking if we found a value
Branch: 1 branch
BranchTrueFalseTotal
if $value 0 0 0
339 }
340 }
341 0 return %opts;
342 }
343  
344  
345 0 ▶sub get_access_token {
Subroutine get_access_token: not called CC=3 SCAR=24.8
Pod documented
346 0 my $self = shift;
347 0 ▶ my %opts = $self->process_access_token_input( (scalar(@_) == 1)
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 ? : 0 0 0
348 ? $self->process_authorization_callback(@_)
349 : @_
350 );
351  
352 0 $self->send_request(access_token => %opts);
353 0 ▶ if ($self->success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->success) 0 0 0
354 0 my $resp = Net::OAuth->response('access token')->from_post_body($self->response->content);
355 0 $self->access_token( $resp->token );
356 0 $self->access_token_secret( $resp->token_secret );
357 }
358 0 return $self->success;
359 }
360  
361  
362 0 ▶sub get_protected_resource {
Subroutine get_protected_resource: not called CC=4 SCAR=30.0
Pod documented
363 0 my $self = shift;
364 0 ▶ my %opts = (scalar(@_) == 1) ? (request_url => $_[0]) : @_ ; # allow just the requested URL to be pased
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 ? : 0 0 0
365 0 ▶ $opts{token} ||= $self->access_token;
Condition0%0/3
$opts{"token"} ||= $self->access_token
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"token"} ||= $self->access_token
$opts{"token"} $self->access_token
Truth table0%0/3
A ||= B
A= $opts{"token"}B= $self->access_token
ABresult
1X1
011
000
366 0 ▶ $opts{token_secret} ||= $self->access_token_secret;
Condition0%0/3
$opts{"token_secret"} ||= $self->access_token_secret
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"token_secret"} ||= $self->access_token_secret
$opts{"token_secret"} $self->access_token_secret
Truth table0%0/3
A ||= B
A= $opts{"token_secret"}B= $self->access_token_secret
ABresult
1X1
011
000
367 0 $self->send_request(protected_resource => %opts);
368 0 return $self->success;
369 }
370  
371  
372 1 1;
373  
374 __END__
↑ Summary Easy/Roles/Types.pm »