File Coverage

blib/lib/WWW/OAuth/Request/Mojo.pm
Criterion Covered Total %
statement 18 62 29.0
branch 0 22 0.0
condition 0 9 0.0
subroutine 6 16 37.5
pod 9 9 100.0
total 33 118 27.9


line stmt bran cond sub pod time code
1             package WWW::OAuth::Request::Mojo;
2              
3 1     1   494 use strict;
  1         6  
  1         30  
4 1     1   8 use warnings;
  1         3  
  1         28  
5 1     1   6 use Class::Tiny::Chained 'request';
  1         2  
  1         5  
6              
7 1     1   244 use Carp 'croak';
  1         2  
  1         117  
8 1     1   8 use Scalar::Util 'blessed';
  1         2  
  1         44  
9              
10 1     1   21 use Role::Tiny::With;
  1         3  
  1         767  
11             with 'WWW::OAuth::Request';
12              
13             our $VERSION = '1.000';
14              
15             sub method {
16 0     0 1   my $self = shift;
17 0 0         return $self->request->method unless @_;
18 0           $self->request->method(shift);
19 0           return $self;
20             }
21              
22             sub url {
23 0     0 1   my $self = shift;
24 0 0         return $self->request->url->to_string unless @_;
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     0 1   my $self = shift;
32 0 0         return $self->request->body unless @_;
33 0           $self->request->body(shift);
34 0           return $self;
35             }
36              
37             sub content_is_form {
38 0     0 1   my $self = shift;
39 0 0         return 0 if $self->request->content->is_multipart;
40 0           my $content_type = $self->request->headers->content_type;
41 0 0 0       return 0 unless defined $content_type and $content_type =~ m!application/x-www-form-urlencoded!i;
42 0           return 1;
43             }
44              
45 0     0 1   sub query_pairs { shift->request->query_params->pairs }
46              
47 0     0 1   sub body_pairs { require Mojo::Parameters; Mojo::Parameters->new(shift->request->body)->pairs }
  0            
48              
49             sub header {
50 0     0 1   my $self = shift;
51 0           my $name = shift;
52 0 0         croak 'No header to set/retrieve' unless defined $name;
53 0 0         return $self->request->headers->header($name) unless @_;
54 0 0         my @values = 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     0 1   my ($self, $ua, $cb) = @_;
61 0 0 0       croak 'Unknown user-agent object' unless blessed $ua and $ua->isa('Mojo::UserAgent');
62 0           return $ua->start($self->_build_tx($ua), $cb);
63             }
64              
65             sub request_with_p {
66 0     0 1   my ($self, $ua) = @_;
67 0 0 0       croak 'Unknown user-agent object' unless blessed $ua and $ua->isa('Mojo::UserAgent');
68 0           my $has_promises = do { local $@; eval { require Mojolicious; Mojolicious->VERSION('7.54'); 1 } };
  0            
  0            
  0            
  0            
  0            
69 0 0         croak 'Mojolicious 7.54 required for request_with_p' unless $has_promises;
70 0           return $ua->start_p($self->_build_tx($ua));
71             }
72              
73             sub _build_tx {
74 0     0     my ($self, $ua) = @_;
75 0           return $ua->build_tx($self->method, $self->url, $self->request->headers->to_hash, $self->content);
76             }
77              
78             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 is a request container for L that
96             wraps a L object, which is used by L.
97             It performs the role L.
98              
99             =head1 ATTRIBUTES
100              
101             L implements the following attributes.
102              
103             =head2 request
104              
105             my $mojo_request = $req->request;
106             $req = $req->request($mojo_request);
107              
108             L object to authenticate.
109              
110             =head1 METHODS
111              
112             L composes all methods from L,
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 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.
128              
129             =head2 content_is_form
130              
131             my $bool = $req->content_is_form;
132              
133             Check whether L has single-part content and a C
134             header of C.
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.
142              
143             =head2 method
144              
145             my $method = $req->method;
146             $req = $req->method('GET');
147              
148             Set or return request method from L.
149              
150             =head2 query_pairs
151              
152             my $pairs = $req->query_pairs;
153              
154             Return query parameters from L 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 user-agent object, and return
166             L object, as in L. 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 user-agent object, and
177             return a L which will be resolved with the successful
178             transaction or rejected on a connection error, as in
179             L.
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.
187              
188             =head1 BUGS
189              
190             Report any issues on the public bugtracker.
191              
192             =head1 AUTHOR
193              
194             Dan Book
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