File Coverage

blib/lib/WWW/Foursquare/Response.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition 2 4 50.0
subroutine 5 5 100.0
pod 0 2 0.0
total 30 34 88.2


line stmt bran cond sub pod time code
1             package WWW::Foursquare::Response;
2              
3 4     4   25542 use strict;
  4         16  
  4         135  
4 4     4   21 use warnings;
  4         7  
  4         242  
5              
6 4     4   5186 use JSON;
  4         88490  
  4         27  
7              
8             our %ERROR_TYPE = (
9             invalid_auth => 'OAuth token was not provided or was invalid',
10             param_error => 'A required parameter was missing or a parameter was malformed. This is also used if the resource ID in the path is incorrect',
11             endpoint_error => 'The requested path does not exist',
12             not_authorized => 'Although authentication succeeded, the acting user is not allowed to see this information due to privacy restrictions',
13             rate_limit_exceeded => 'Rate limit for this hour exceeded',
14             deprecated => 'Something about this request is using deprecated functionality, or the response format may be about to change',
15             server_error => 'erver is currently experiencing issues. Check status.foursquare.com for updates',
16             other => 'Some other type of error occurred',
17             unknown => 'Unknown error',
18             );
19              
20             sub new {
21 1     1 0 22160 my ($class, $params) = @_;
22              
23 1         4 my $self = {};
24 1         3 bless $self, $class;
25 1         4 return $self;
26             }
27              
28             sub process {
29 2     2 0 3409 my ($self, $res) = @_;
30              
31 2         9 my $data = decode_json($res->content());
32 2         104 my $code = $res->code();
33              
34             # response is OK
35 2 100       29 return $data->{response} if $code == 200;
36              
37             # need error handling
38 1   50     7 my $error_type = $data->{meta}->{errorType} || 'unkwown';
39 1   50     10 my $error_desc = $ERROR_TYPE{$error_type} || $data->{meta}->{errorDetail} || 'no details';
40 1         6 my $error_text = sprintf "%s %s", $error_type, $error_desc;
41              
42             # raise exception
43 1         12 die $error_text;
44             }
45              
46              
47             1;