Foursquare/Request.pm

filter: Statementstmt 79.5% 78 / 98 Branchbran 57.6% 15 / 26 Conditioncond 25.0% 2 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 4 totaltotal 69.8% 109 / 156 CC 19 SCAR 20.1
CC
19
Cov
72%
_debug
30.0
GET
5.5
POST
5.1
CRAP 7.5
↑ Summary Foursquare/Response.pm »
1 package WWW::Foursquare::Request;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use WWW::Foursquare::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use WWW::Foursquare::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12 2 my ($class, $params) = @_;
13  
14 2 my $self = {};
15 2 bless $self, $class;
16 2 $self->{access_token} = $params->{access_token};
17 2 $self->{client_id} = $params->{client_id};
18 2 $self->{client_secret} = $params->{client_secret};
19 2 $self->{debug} = $params->{debug};
20 2 $self->{GET} = [];
21 2 $self->{ua} = LWP::UserAgent->new();
22  
23 2 return $self;
24 }
25  
26 7 ▶sub GET {
Subroutine GET: called CC=5 SCAR=17.1
Pod undocumented
27 7 my ($self, $path, $params) = @_;
28  
29 # add request to multi list
30 7 ▶ return $self->MULTI('GET', $path, $params) if delete $params->{multi};
Branch: 1 branch
BranchTrueFalseTotal
if delete $params->{"multi"} 5 2 7
31 2 ▶ my $is_show_request = 1 if delete $params->{show_request};
Branch: 1 branch
BranchTrueFalseTotal
if delete $params->{"show_request"} 2 0 2
32  
33 # add auth params
34 2 $self->_add_auth_to_params($params);
35  
36 2 my $query = $self->_params_to_str($params);
37 2 my $result_url = sprintf "%s%s?%s", $API_ENDPOINT, $path, $query;
38  
39 # debug request
40 2 ▶ $self->_debug('GET', $result_url) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 2 2
41  
42 # for testing url request
43 2 ▶ return $result_url if $is_show_request;
Branch: 1 branch
BranchTrueFalseTotal
if $is_show_request 2 0 2
44  
45 0 my $res = $self->{ua}->get($result_url);
46 0 return $self->_response($res);
47 }
48  
49 1 ▶sub POST {
Subroutine POST: called CC=4 SCAR=16.3
Pod undocumented
50 1 my ($self, $path, $params) = @_;
51  
52 1 ▶ my $is_show_request = 1 if delete $params->{show_request};
Branch: 1 branch
BranchTrueFalseTotal
if delete $params->{"show_request"} 1 0 1
53  
54 # add auth params
55 1 $self->_add_auth_to_params($params);
56  
57 1 my $query = $self->_params_to_auth($params);
58 1 my $result_url = sprintf "%s%s?%s", $API_ENDPOINT, $path, $query;
59
60 # debug request
61 1 ▶ $self->_debug('POST', $result_url, $params) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
62  
63 # for testing url request
64 1 ▶ return $result_url if $is_show_request;
Branch: 1 branch
BranchTrueFalseTotal
if $is_show_request 1 0 1
65  
66 # convert hash to array (because of LWP)
67 0 my @params = map { $_ => $params->{$_} } keys %$params;
68  
69 0 my $res = $self->{ua}->post($result_url, Content_Type => 'form-data', Content => [ @params ]);
70 0 return $self->_response($res);
71 }
72  
73 5 ▶sub MULTI {
Subroutine MULTI: called CC=5 SCAR=16.2
Pod undocumented
74 5 my ($self, $method, $path, $params) = @_;
75  
76 # internal type error
77 5 ▶ return 'error' if ($method !~ /^GET|POST$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $method =~ /^GET|POST$/ 0 5 5
78  
79 5 my $force = delete $params->{force};
80 5 my $query = $self->_params_to_str($params);
81 5 ▶ my $url = $query
Branch: 1 branch
BranchTrueFalseTotal
$query ? : 0 5 5
82 ? sprintf("/%s?%s", $path, $query)
83 : sprintf("/%s", $path);
84  
85 5 push @{$self->{$method}}, $url;
86
87 # send multi request
88 5 ▶ if (@{ $self->{$method} } >= 5 || $force) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{$method};} >= 5 or $force) { } 1 4 5
Condition66%2/3
@{$self->{$method};} >= 5 or $force
l!l&&r!l&&!r
104
MC/DC50%1/2
@{$self->{$method};} >= 5 or $force
@{$self->{$method};} >= 5 $force
Truth table66%2/3
A or B
A= @{$self->{$method};} >= 5B= $force
ABresult
1X1
011
000
89  
90 1 my $request = join ',', @{$self->{$method}};
91 1 $params->{requests} = $request;
92 1 delete $self->{$method};
93  
94 1 return $self->$method('multi', $params);
95 }
96 # put request to queue
97 else {
98  
99 4 return scalar(@{ $self->{$method} });
100 }
101 }
102  
103 0 ▶sub _response {
Subroutine _response: not called CC=1 SCAR=6.9
104 0 my ($self, $res) = @_;
105  
106 0 return WWW::Foursquare::Response->new()->process($res);
107 }
108  
109 3 ▶sub _add_auth_to_params {
Subroutine _add_auth_to_params: called CC=2 SCAR=7.9
110 3 my ($self, $params) = @_;
111  
112 3 ▶ if ($self->{userless}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'userless'}) { } 0 3 3
113  
114 0 $params->{client_id} = $self->{client_id};
115 0 $params->{client_secret} = $self->{client_secret};
116 }
117 else {
118  
119 3 $params->{oauth_token} = $self->{access_token};
120 }
121 3 $params->{v} = $API_VERSION;
122 }
123  
124 0 ▶sub _debug {
Subroutine _debug: not called CC=5 SCAR=34.0
125 0 my ($self, $type, $url, $params) = @_;
126  
127 0 my $param_text;
128 0 for my $key ($params) {
129  
130 0 ▶ my $value = $params->{$key} || '';
Condition0%0/2
$params->{$key} || ''
l!l
00
MC/DC0%0/1
$params->{$key} || ''
$params->{$key}
Truth table0%0/2
A
A= $params->{$key}
Aresult
11
00
131 0 $param_text .= sprintf "[%s] = [%s]\n", $key, $value;
132 }
133
134 0 warn "Request: $type";
135 0 warn "Url: $url";
136  
137 0 ▶ if ($type =~ /post/ && $param_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /post/ and $param_text) 0 0 0
Condition0%0/3
$type =~ /post/ and $param_text
!ll&&!rl&&r
000
MC/DC0%0/2
$type =~ /post/ and $param_text
$type =~ /post/ $param_text
Truth table0%0/3
A and B
A= $type =~ /post/B= $param_text
ABresult
0X0
100
111
138  
139 0 warn "Params: ";
140 0 warn $param_text;
141 }
142 }
143  
144 7 ▶sub _params_to_str {
Subroutine _params_to_str: called CC=1 SCAR=0.0
145 7 my ($self, $params) = @_;
146  
147 7 my %copy_params = %$params;
148 7 delete $copy_params{show_request};
149  
150 7 my $query = join '&', map { $_.'='.uri_escape($copy_params{$_}) } sort keys %copy_params;
151 7 return $query;
152 }
153  
154 1 ▶sub _params_to_auth {
Subroutine _params_to_auth: called CC=3 SCAR=11.0
155 1 my ($self, $params) = @_;
156  
157 1 my @auth = qw(client_id client_secret oauth_token v);
158 1 my @exists;
159 PARAM:
160 1 for my $param (@auth) {
161
162 4 ▶ next PARAM if not exists $params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$param} 2 2 4
163 2 push @exists, $param.'='.uri_escape($params->{$param});
164 }
165 1 my $query = join '&', @exists;
166 1 return $query;
167 }
168  
169  
170 4 1;
↑ Summary Foursquare/Response.pm »