OAuth/Request/Basic.pm

filter: Statementstmt 94.8% 55 / 58 Branchbran 78.5% 11 / 14 Conditioncond 50.0% 3 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 92.8% 13 / 14 Podpod 100% 4 / 4 totaltotal 88.0% 88 / 100 SCAR 23.2
CC
10
Cov
88%
request_with
12.0
header
6.0
content_is_form
3.0
CRAP 10.2
« OAuth/Request/HTTP_Request.pm ↑ Summary OAuth/Request.pm »
1 package WWW::OAuth::Request::Basic;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Class::Tiny::Chained 'method', 'url', 'content', { headers => sub { {} } };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use WWW::OAuth::Util 'form_urlencode';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 with 'WWW::OAuth::Request';
14  
15 3 our $VERSION = '1.000';
16  
17 sub content_is_form {
18 6 ▶ my $self = shift;
Subroutine content_is_form: called CC=3 SCAR=11.0
Pod documented
19 6 my $content_type = $self->header('Content-Type');
20 6 ▶ 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 4 2 6
Condition100%3/3
defined $content_type and $content_type =~ m[application/x-www-form-urlencoded]i
!ll&&!rl&&r
312
MC/DC100%2/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 table100%3/3
A and B
A= defined $content_typeB= $content_type =~ m[application/x-www-form-urlencoded]i
ABresult
0X0
100
111
21 2 return 1;
22 }
23  
24 sub header {
25 24 ▶ my $self = shift;
Subroutine header: called CC=6 SCAR=17.9
Pod documented
26 24 my $name = shift;
27 24 ▶ croak 'No header to set/retrieve' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 24 24
28 24 my $headers = $self->headers;
29 24 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 15 9 24
30 # workaround for TEMP bug in first/lc
31 15 my @names = keys %$headers;
32 15 ▶ my $key = first { lc $_ eq lc $name } @names;
Subroutine __ANON__: called CC=1 SCAR=0.0
33 15 ▶ return undef unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 5 10 15
34 10 ▶ my @values = ref $headers->{$key} eq 'ARRAY' ? @{$headers->{$key}} : $headers->{$key};
Branch: 1 branch
BranchTrueFalseTotal
ref $headers->{$key} eq 'ARRAY' ? : 1 9 10
35 10 return join ', ', grep { defined } @values;
36 }
37 9 my $value = shift;
38 9 my @existing = grep { lc $_ eq lc $name } keys %$headers;
39 9 ▶ delete @$headers{@existing} if @existing;
Branch: 1 branch
BranchTrueFalseTotal
if @existing 4 5 9
40 9 $headers->{$name} = $value;
41 9 return $self;
42 }
43  
44 sub set_form {
45 1 ▶ my ($self, $form) = @_;
Subroutine set_form: called CC=1 SCAR=0.0
Pod documented
46 1 $self->header('Content-Type' => 'application/x-www-form-urlencoded');
47 1 $self->content(form_urlencode $form);
48 1 return $self;
49 }
50  
51 sub request_with {
52 0 ▶ my ($self, $ua) = @_;
Subroutine request_with: not called CC=3 SCAR=24.8
Pod documented
53 0 ▶ croak 'Unknown user-agent object' unless blessed $ua and $ua->isa('HTTP::Tiny');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($ua) and $ua->isa("HTTP::Tiny") 0 0 0
Condition0%0/3
builtin::blessed($ua) and $ua->isa("HTTP::Tiny")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($ua) and $ua->isa("HTTP::Tiny")
builtin::blessed($ua) $ua->isa("HTTP::Tiny")
Truth table0%0/3
A and B
A= builtin::blessed($ua)B= $ua->isa("HTTP::Tiny")
ABresult
0X0
100
111
54 0 return $ua->request($self->method, $self->url, { headers => $self->headers, content => $self->content });
55 }
56  
57 3 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<WWW::OAuth::Request::Basic> is a request container for L<WWW::OAuth> that
71 stores the request parameters directly, for use with user-agents that do not
72 use request objects like L<HTTP::Tiny>. It performs the role
73 L<WWW::OAuth::Request>.
74  
75 =head1 ATTRIBUTES
76  
77 L<WWW::OAuth::Request::Basic> 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<WWW::OAuth::Request::Basic> composes all methods from L<WWW::OAuth::Request>,
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</"headers"> contains a C<Content-Type> header set to
119 C<application/x-www-form-urlencoded>.
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</"headers">.
127  
128 =head2 set_form
129  
130 $req = $req->set_form({foo => 'bar'});
131  
132 Convenience method to set L</"content"> 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<HTTP::Tiny> user-agent object, and return response
143 hashref, as in L<HTTP::Tiny/"request">.
144  
145 =head1 BUGS
146  
147 Report any issues on the public bugtracker.
148  
149 =head1 AUTHOR
150  
151 Dan Book <dbook@cpan.org>
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<HTTP::Tiny>
« OAuth/Request/HTTP_Request.pm ↑ Summary OAuth/Request.pm »