OAuth/Request/Mojo.pm

filter: Statementstmt 32.3% 21 / 65 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 37.5% 6 / 16 Podpod 100% 9 / 9 totaltotal 28.3% 36 / 127 SCAR 48.1
CC
15
Cov
22%
content_is_form
20.0
header
20.0
request_with_p
20.0
CRAP 122.3
↑ Summary OAuth.pm »
1 package WWW::OAuth::Request::Mojo;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Class::Tiny::Chained 'request';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 with 'WWW::OAuth::Request';
12  
13 1 our $VERSION = '1.000';
14  
15 sub method {
16 0 ▶ my $self = shift;
Subroutine method: not called CC=2 SCAR=17.9
Pod documented
17 0 ▶ return $self->request->method unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
18 0 $self->request->method(shift);
19 0 return $self;
20 }
21  
22 sub url {
23 0 ▶ my $self = shift;
Subroutine url: not called CC=2 SCAR=17.9
Pod documented
24 0 ▶ return $self->request->url->to_string unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
25 0 require Mojo::URL;
26 0 $self->request->url(Mojo::URL->new(shift));
27 0 return $self;
28 }
29  
30 sub content {
31 0 ▶ my $self = shift;
Subroutine content: not called CC=2 SCAR=17.9
Pod documented
32 0 ▶ return $self->request->body unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
33 0 $self->request->body(shift);
34 0 return $self;
35 }
36  
37 sub content_is_form {
38 0 ▶ my $self = shift;
Subroutine content_is_form: not called CC=4 SCAR=30.0
Pod documented
39 0 ▶ return 0 if $self->request->content->is_multipart;
Branch: 1 branch
BranchTrueFalseTotal
if $self->request->content->is_multipart 0 0 0
40 0 my $content_type = $self->request->headers->content_type;
41 0 ▶ return 0 unless defined $content_type and $content_type =~ m!application/x-www-form-urlencoded!i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content_type and $content_type =~ m[application/x-www-form-urlencoded]i 0 0 0
Condition0%0/3
defined $content_type and $content_type =~ m[application/x-www-form-urlencoded]i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content_type and $content_type =~ m[application/x-www-form-urlencoded]i
defined $content_type $content_type =~ m[application/x-www-form-urlencoded]i
Truth table0%0/3
A and B
A= defined $content_typeB= $content_type =~ m[application/x-www-form-urlencoded]i
ABresult
0X0
100
111
42 0 return 1;
43 }
44  
45 0 ▶sub query_pairs { shift->request->query_params->pairs }
Subroutine query_pairs: not called CC=1 SCAR=6.9
Pod documented
46  
47 0 ▶sub body_pairs { require Mojo::Parameters; Mojo::Parameters->new(shift->request->body)->pairs }
Subroutine body_pairs: not called CC=1 SCAR=6.9
Pod documented
48  
49 sub header {
50 0 ▶ my $self = shift;
Subroutine header: not called CC=4 SCAR=30.0
Pod documented
51 0 my $name = shift;
52 0 ▶ croak 'No header to set/retrieve' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
53 0 ▶ return $self->request->headers->header($name) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
54 0 ▶ my @values = ref $_[0] eq 'ARRAY' ? @{$_[0]} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
55 0 $self->request->headers->header($name => @values);
56 0 return $self;
57 }
58  
59 sub request_with {
60 0 ▶ my ($self, $ua, $cb) = @_;
Subroutine request_with: not called CC=3 SCAR=24.8
Pod documented
61 0 ▶ croak 'Unknown user-agent object' unless blessed $ua and $ua->isa('Mojo::UserAgent');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($ua) and $ua->isa("Mojo::UserAgent") 0 0 0
Condition0%0/3
builtin::blessed($ua) and $ua->isa("Mojo::UserAgent")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($ua) and $ua->isa("Mojo::UserAgent")
builtin::blessed($ua) $ua->isa("Mojo::UserAgent")
Truth table0%0/3
A and B
A= builtin::blessed($ua)B= $ua->isa("Mojo::UserAgent")
ABresult
0X0
100
111
62 0 return $ua->start($self->_build_tx($ua), $cb);
63 }
64  
65 sub request_with_p {
66 0 ▶ my ($self, $ua) = @_;
Subroutine request_with_p: not called CC=4 SCAR=30.0
Pod documented
67 0 ▶ croak 'Unknown user-agent object' unless blessed $ua and $ua->isa('Mojo::UserAgent');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($ua) and $ua->isa("Mojo::UserAgent") 0 0 0
Condition0%0/3
builtin::blessed($ua) and $ua->isa("Mojo::UserAgent")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($ua) and $ua->isa("Mojo::UserAgent")
builtin::blessed($ua) $ua->isa("Mojo::UserAgent")
Truth table0%0/3
A and B
A= builtin::blessed($ua)B= $ua->isa("Mojo::UserAgent")
ABresult
0X0
100
111
68 0 my $has_promises = do { local $@; eval { require Mojolicious; Mojolicious->VERSION('7.54'); 1 } };
69 0 ▶ croak 'Mojolicious 7.54 required for request_with_p' unless $has_promises;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_promises 0 0 0
70 0 return $ua->start_p($self->_build_tx($ua));
71 }
72  
73 sub _build_tx {
74 0 ▶ my ($self, $ua) = @_;
Subroutine _build_tx: not called CC=1 SCAR=6.9
75 0 return $ua->build_tx($self->method, $self->url, $self->request->headers->to_hash, $self->content);
76 }
77  
78 1 1;
79  
80 =head1 NAME
81  
82 WWW::OAuth::Request::Mojo - HTTP Request container for Mojo::Message::Request
83  
84 =head1 SYNOPSIS
85  
86 my $req = WWW::OAuth::Request::Mojo->new(request => $mojo_request);
87 my $ua = Mojo::UserAgent->new;
88 my $tx = $req->request_with($ua);
89 $req->request_with_p($ua)->then(sub {
90 my $tx = shift;
91 });
92  
93 =head1 DESCRIPTION
94  
95 L<WWW::OAuth::Request::Mojo> is a request container for L<WWW::OAuth> that
96 wraps a L<Mojo::Message::Request> object, which is used by L<Mojo::UserAgent>.
97 It performs the role L<WWW::OAuth::Request>.
98  
99 =head1 ATTRIBUTES
100  
101 L<WWW::OAuth::Request::Mojo> implements the following attributes.
102  
103 =head2 request
104  
105 my $mojo_request = $req->request;
106 $req = $req->request($mojo_request);
107  
108 L<Mojo::Message::Request> object to authenticate.
109  
110 =head1 METHODS
111  
112 L<WWW::OAuth::Request::Mojo> composes all methods from L<WWW::OAuth::Request>,
113 and implements the following new ones.
114  
115 =head2 body_pairs
116  
117 my $pairs = $req->body_pairs;
118  
119 Return body parameters from L</"request"> as an even-sized arrayref of keys and
120 values.
121  
122 =head2 content
123  
124 my $content = $req->content;
125 $req = $req->content('foo=1&bar=2');
126  
127 Set or return request content from L</"request">.
128  
129 =head2 content_is_form
130  
131 my $bool = $req->content_is_form;
132  
133 Check whether L</"request"> has single-part content and a C<Content-Type>
134 header of C<application/x-www-form-urlencoded>.
135  
136 =head2 header
137  
138 my $header = $req->header('Content-Type');
139 $req = $req->header(Authorization => 'foo bar');
140  
141 Set or return a request header from L</"request">.
142  
143 =head2 method
144  
145 my $method = $req->method;
146 $req = $req->method('GET');
147  
148 Set or return request method from L</"request">.
149  
150 =head2 query_pairs
151  
152 my $pairs = $req->query_pairs;
153  
154 Return query parameters from L</"request"> as an even-sized arrayref of keys
155 and values.
156  
157 =head2 request_with
158  
159 my $tx = $req->request_with($ua);
160 $req->request_with($ua, sub {
161 my ($ua, $tx) = @_;
162 ...
163 });
164  
165 Run request with passed L<Mojo::UserAgent> user-agent object, and return
166 L<Mojo::Transaction> object, as in L<Mojo::UserAgent/"start">. A callback can
167 be passed to perform the request non-blocking.
168  
169 =head2 request_with_p
170  
171 my $p = $req->request_with_p($ua)->then(sub {
172 my $tx = shift;
173 ...
174 });
175  
176 Run non-blocking request with passed L<Mojo::UserAgent> user-agent object, and
177 return a L<Mojo::Promise> which will be resolved with the successful
178 transaction or rejected on a connection error, as in
179 L<Mojo::UserAgent/"start_p">.
180  
181 =head2 url
182  
183 my $url = $req->url;
184 $req = $req->url('http://example.com/api/');
185  
186 Set or return request URL from L</"request">.
187  
188 =head1 BUGS
189  
190 Report any issues on the public bugtracker.
191  
192 =head1 AUTHOR
193  
194 Dan Book <dbook@cpan.org>
195  
196 =head1 COPYRIGHT AND LICENSE
197  
198 This software is Copyright (c) 2015 by Dan Book.
199  
200 This is free software, licensed under:
201  
202 The Artistic License 2.0 (GPL Compatible)
203  
204 =head1 SEE ALSO
205  
206 L<Mojo::UserAgent>
↑ Summary OAuth.pm »