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   17603 use strict;
  3         4  
  3         84  
4 3     3   11 use warnings;
  3         4  
  3         149  
5 3     3   1036 use Class::Tiny::Chained 'method', 'url', 'content', { headers => sub { {} } };
  3         6857  
  3         41  
  3         185  
6              
7 3     3   1712 use Carp 'croak';
  3         3  
  3         190  
8 3     3   14 use List::Util 'first';
  3         3  
  3         232  
9 3     3   13 use Scalar::Util 'blessed';
  3         4  
  3         122  
10 3     3   395 use WWW::OAuth::Util 'form_urlencode';
  3         5  
  3         126  
11              
12 3     3   1372 use Role::Tiny::With;
  3         675  
  3         1291  
13             with 'WWW::OAuth::Request';
14              
15             our $VERSION = '0.006';
16              
17             sub content_is_form {
18 6     6 1 497 my $self = shift;
19 6         12 my $content_type = $self->header('Content-Type');
20 6 100 100     50 return 0 unless defined $content_type and $content_type =~ m!application/x-www-form-urlencoded!i;
21 2         8 return 1;
22             }
23              
24             sub header {
25 24     24 1 711 my $self = shift;
26 24         28 my $name = shift;
27 24 50       55 croak 'No header to set/retrieve' unless defined $name;
28 24         555 my $headers = $self->headers;
29 24 100       130 unless (@_) {
30             # workaround for TEMP bug in first/lc
31 15         37 my @names = keys %$headers;
32 15     16   86 my $key = first { lc $_ eq lc $name } @names;
  16         31  
33 15 100       69 return undef unless defined $key;
34 10 100       33 my @values = ref $headers->{$key} eq 'ARRAY' ? @{$headers->{$key}} : $headers->{$key};
  1         3  
35 10         16 return join ', ', grep { defined } @values;
  12         63  
36             }
37 9         12 my $value = shift;
38 9         26 my @existing = grep { lc $_ eq lc $name } keys %$headers;
  14         33  
39 9 100       29 delete @$headers{@existing} if @existing;
40 9         20 $headers->{$name} = $value;
41 9         18 return $self;
42             }
43              
44             sub set_form {
45 1     1 1 11 my ($self, $form) = @_;
46 1         2 $self->header('Content-Type' => 'application/x-www-form-urlencoded');
47 1         4 $self->content(form_urlencode $form);
48 1         5 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