OAuth/Util.pm

filter: Statementstmt 93.1% 54 / 58 Branchbran 78.5% 22 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 90.0% 90 / 100 SCAR 28.0
CC
16
Cov
88%
oauth_request
11.7
form_urlencode
4.2
form_urldecode
3.0
CRAP 16.4
« OAuth.pm ↑ Summary OAuth/Request/HTTP_Request.pm »
1 package WWW::OAuth::Util;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Module::Runtime 'require_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Role::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use WWW::Form::UrlEncoded 'build_urlencoded_utf8', 'parse_urlencoded_arrayref';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 our $VERSION = '0.006';
13  
14 5 our @EXPORT_OK = qw(form_urlencode form_urldecode oauth_request);
15  
16 sub form_urldecode {
17 15 ▶ my $string = shift;
Subroutine form_urldecode: called CC=3 SCAR=11.0
Pod documented
18 15 ▶ return [] unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 1 14 15
19 14 my $form = parse_urlencoded_arrayref $string;
20 14 utf8::decode $_ for @$form;
21 14 return $form;
22 }
23  
24 sub form_urlencode {
25 13 ▶ my $form = shift;
Subroutine form_urlencode: called CC=4 SCAR=14.3
Pod documented
26 13 ▶ if (ref $form eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $form eq 'ARRAY') { } 11 2 13
elsif (ref $form eq 'HASH') { } 2 0 2
27 11 ▶ croak 'Form to urlencode must be even-sized' if @$form % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$form % 2 0 11 11
28 } elsif (ref $form eq 'HASH') {
29 2 $form = [map { ($_ => $form->{$_}) } sort keys %$form];
30 } else {
31 0 croak 'Form to urlencode must be hash or array reference';
32 }
33 13 return build_urlencoded_utf8 $form, '&';
34 }
35  
36 sub oauth_request {
37 12 ▶ my $class = ref $_[0] ? undef : shift;
Subroutine oauth_request: called CC=11 SCAR=24.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 7 5 12
38 12 my $proto = shift;
39 12 my %args;
40 12 ▶ if (blessed $proto) { # Request object
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($proto)) { } 8 4 12
elsif (ref $proto eq 'HASH') { } 4 0 4
41 8 ▶ return $proto if Role::Tiny::does_role($proto, 'WWW::OAuth::Request'); # already in container
Branch: 1 branch
BranchTrueFalseTotal
if Role::Tiny::does_role($proto, "WWW::OAuth::Request") 4 4 8
42 4 ▶ unless (defined $class) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class) 2 2 4
43 2 ▶ if ($proto->isa('HTTP::Request')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($proto->isa('HTTP::Request')) { } 1 1 2
elsif ($proto->isa('Mojo::Message::Request')) { } 1 0 1
44 1 $class = 'HTTP_Request';
45 } elsif ($proto->isa('Mojo::Message::Request')) {
46 1 $class = 'Mojo';
47 } else {
48 0 $class = blessed $proto;
49 0 $class =~ s/::/_/g;
50 }
51 }
52 4 %args = (request => $proto);
53 } elsif (ref $proto eq 'HASH') { # Hashref
54 4 ▶ $class = 'Basic' unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 1 3 4
55 4 %args = %$proto;
56 } else {
57 0 croak 'No request or request parameters passed';
58 }
59
60 8 ▶ $class = "WWW::OAuth::Request::$class" unless $class =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /::/ 8 0 8
61 8 require_module $class;
62 8 ▶ croak "Class $class does not perform the role WWW::OAuth::Request"
Branch: 1 branch
BranchTrueFalseTotal
unless Role::Tiny::does_role($class, "WWW::OAuth::Request") 0 8 8
63 unless Role::Tiny::does_role($class, 'WWW::OAuth::Request');
64
65 8 return $class->new(%args);
66 }
67  
68 5 1;
69  
70 =head1 NAME
71  
72 WWW::OAuth::Util - Utility functions for WWW::OAuth
73  
74 =head1 SYNOPSIS
75  
76 use WWW::OAuth::Util 'form_urldecode', 'form_urlencode';
77 my $body_string = form_urlencode({foo => 'a b c', bar => [1, 2, 3]});
78 # bar=1&bar=2&bar=3&foo=a+b+c
79 my $ordered_pairs = form_urldecode($body_string);
80 # ['bar', '1', 'bar', '2', 'bar', '3', 'foo', 'a b c']
81
82 use WWW::OAuth::Util 'oauth_request';
83 my $container = oauth_request($http_request);
84  
85 =head1 DESCRIPTION
86  
87 L<WWW::OAuth::Util> contains utility functions for use with L<WWW::OAuth>. All
88 functions are exportable on demand.
89  
90 =head1 FUNCTIONS
91  
92 =head2 form_urldecode
93  
94 my $param_pairs = form_urldecode($body_string);
95  
96 Decodes an C<application/x-www-form-urlencoded> string and returns an
97 even-sized arrayref of key-value pairs. Order is preserved and repeated keys
98 are not combined.
99  
100 =head2 form_urlencode
101  
102 my $body_string = form_urlencode([foo => 2, bar => 'baz', foo => 1]);
103 # foo=2&bar=baz&foo=1
104 my $body_string = form_urlencode({foo => [2, 1], bar => 'baz'});
105 # bar=baz&foo=2&foo=1
106  
107 Converts a hash or array reference into an C<application/x-www-form-urlencoded>
108 string suitable for a query string or request body. If a value is an array
109 reference, the key is repeated with each value. Order is preserved if
110 parameters are passed in an array reference; the parameters are sorted by key
111 for consistency if passed in a hash reference.
112  
113 =head2 oauth_request
114  
115 my $container = oauth_request($http_request);
116 my $container = oauth_request({ method => 'GET', url => $url });
117 my $container = oauth_request(Basic => { method => 'POST', url => $url, content => $content });
118  
119 Constructs an HTTP request container performing the L<WWW::OAuth::Request>
120 role. The input should be a recognized request object or hashref of arguments
121 optionally preceded by a container class name. The class name is appended to
122 C<WWW::OAuth::Request::> if it does not contain C<::>. Currently,
123 L<HTTP::Request> and L<Mojo::Message::Request> objects are recognized, and
124 hashrefs are used to construct a L<WWW::OAuth::Request::Basic> object if no
125 container class is specified.
126  
127 # Longer forms to construct WWW::OAuth::Request::HTTP_Request
128 my $container = oauth_request(HTTP_Request => $http_request);
129 my $container = oauth_request(HTTP_Request => { request => $http_request });
130  
131 =head1 BUGS
132  
133 Report any issues on the public bugtracker.
134  
135 =head1 AUTHOR
136  
137 Dan Book <dbook@cpan.org>
138  
139 =head1 COPYRIGHT AND LICENSE
140  
141 This software is Copyright (c) 2015 by Dan Book.
142  
143 This is free software, licensed under:
144  
145 The Artistic License 2.0 (GPL Compatible)
146  
147 =head1 SEE ALSO
148  
149 L<URI>, L<URL Living Standard|https://url.spec.whatwg.org/#application/x-www-form-urlencoded>
« OAuth.pm ↑ Summary OAuth/Request/HTTP_Request.pm »