File Coverage

blib/lib/WWW/OAuth/Request/Basic.pm
Criterion Covered Total %
statement 52 55 94.5
branch 11 14 78.5
condition 3 6 50.0
subroutine 12 13 92.3
pod 4 4 100.0
total 82 92 89.1


line stmt bran cond sub pod time code
1             package WWW::OAuth::Request::Basic;
2              
3 3     3   68181 use strict;
  3         16  
  3         92  
4 3     3   18 use warnings;
  3         6  
  3         151  
5 3     3   905 use Class::Tiny::Chained 'method', 'url', 'content', { headers => sub { {} } };
  3         4975  
  3         40  
  3         301  
6              
7 3     3   2147 use Carp 'croak';
  3         8  
  3         192  
8 3     3   21 use List::Util 'first';
  3         5  
  3         304  
9 3     3   21 use Scalar::Util 'blessed';
  3         8  
  3         133  
10 3     3   430 use WWW::OAuth::Util 'form_urlencode';
  3         7  
  3         153  
11              
12 3     3   1434 use Role::Tiny::With;
  3         818  
  3         1479  
13             with 'WWW::OAuth::Request';
14              
15             our $VERSION = '1.000';
16              
17             sub content_is_form {
18 6     6 1 631 my $self = shift;
19 6         21 my $content_type = $self->header('Content-Type');
20 6 100 100     55 return 0 unless defined $content_type and $content_type =~ m!application/x-www-form-urlencoded!i;
21 2         12 return 1;
22             }
23              
24             sub header {
25 24     24 1 961 my $self = shift;
26 24         41 my $name = shift;
27 24 50       58 croak 'No header to set/retrieve' unless defined $name;
28 24         548 my $headers = $self->headers;
29 24 100       143 unless (@_) {
30             # workaround for TEMP bug in first/lc
31 15         43 my @names = keys %$headers;
32 15     16   100 my $key = first { lc $_ eq lc $name } @names;
  16         40  
33 15 100       74 return undef unless defined $key;
34 10 100       39 my @values = ref $headers->{$key} eq 'ARRAY' ? @{$headers->{$key}} : $headers->{$key};
  1         3  
35 10         23 return join ', ', grep { defined } @values;
  12         75  
36             }
37 9         25 my $value = shift;
38 9         27 my @existing = grep { lc $_ eq lc $name } keys %$headers;
  14         37  
39 9 100       31 delete @$headers{@existing} if @existing;
40 9         22 $headers->{$name} = $value;
41 9         23 return $self;
42             }
43              
44             sub set_form {
45 1     1 1 5 my ($self, $form) = @_;
46 1         6 $self->header('Content-Type' => 'application/x-www-form-urlencoded');
47 1         9 $self->content(form_urlencode $form);
48 1         6 return $self;
49             }
50              
51             sub request_with {
52 0     0 1   my ($self, $ua) = @_;
53 0 0 0       croak 'Unknown user-agent object' unless blessed $ua and $ua->isa('HTTP::Tiny');
54 0           return $ua->request($self->method, $self->url, { headers => $self->headers, content => $self->content });
55             }
56              
57             1;
58              
59             =head1 NAME
60              
61             WWW::OAuth::Request::Basic - HTTP Request container for HTTP::Tiny
62              
63             =head1 SYNOPSIS
64              
65             my $req = WWW::OAuth::Request::Basic->new(method => 'POST', url => $url, content => $content);
66             $req->request_with(HTTP::Tiny->new);
67              
68             =head1 DESCRIPTION
69              
70             L is a request container for L that
71             stores the request parameters directly, for use with user-agents that do not
72             use request objects like L. It performs the role
73             L.
74              
75             =head1 ATTRIBUTES
76              
77             L implements the following attributes.
78              
79             =head2 content
80              
81             my $content = $req->content;
82             $req = $req->content('foo=1&bar=2');
83              
84             Request content string.
85              
86             =head2 headers
87              
88             my $headers = $req->headers;
89             $req = $req->headers({});
90              
91             Hashref of request headers. Must be updated carefully as headers are
92             case-insensitive. Values can be array references to specify multi-value
93             headers.
94              
95             =head2 method
96              
97             my $method = $req->method;
98             $req = $req->method('GET');
99              
100             Request method.
101              
102             =head2 url
103              
104             my $url = $req->url;
105             $req = $req->url('http://example.com/api/');
106              
107             Request URL.
108              
109             =head1 METHODS
110              
111             L composes all methods from L,
112             and implements the following new ones.
113              
114             =head2 content_is_form
115              
116             my $bool = $req->content_is_form;
117              
118             Check whether L contains a C header set to
119             C.
120              
121             =head2 header
122              
123             my $header = $req->header('Content-Type');
124             $req = $req->header(Authorization => 'Basic foobar');
125              
126             Set or return a request header in L.
127              
128             =head2 set_form
129              
130             $req = $req->set_form({foo => 'bar'});
131              
132             Convenience method to set L to a urlencoded form. Equivalent to:
133              
134             use WWW::OAuth::Util 'form_urlencode';
135             $req->header('Content-Type' => 'application/x-www-form-urlencoded');
136             $req->content(form_urlencode $form);
137              
138             =head2 request_with
139              
140             my $res = $req->request_with(HTTP::Tiny->new);
141              
142             Run request with passed L user-agent object, and return response
143             hashref, as in L.
144              
145             =head1 BUGS
146              
147             Report any issues on the public bugtracker.
148              
149             =head1 AUTHOR
150              
151             Dan Book
152              
153             =head1 COPYRIGHT AND LICENSE
154              
155             This software is Copyright (c) 2015 by Dan Book.
156              
157             This is free software, licensed under:
158              
159             The Artistic License 2.0 (GPL Compatible)
160              
161             =head1 SEE ALSO
162              
163             L