| 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
| |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | my $is_show_request = 1 if delete $params->{show_request}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # for testing url request | ||||||||||||||||||||||||||||
| 43 | 2 | ▶ | return $result_url if $is_show_request; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # for testing url request | ||||||||||||||||||||||||||||
| 64 | 1 | ▶ | return $result_url if $is_show_request; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
@{$self->{$method};} >= 5 or $force
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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} || ''
MC/DC0%0/1
$params->{$key} || ''
$params->{$key}
Truth table0%0/2
A
A= $params->{$key}
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$type =~ /post/ and $param_text
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||