File Coverage

blib/lib/REST/Google.pm
Criterion Covered Total %
statement 55 67 82.0
branch 7 16 43.7
condition n/a
subroutine 13 14 92.8
pod 2 2 100.0
total 77 99 77.7


line stmt bran cond sub pod time code
1             #
2             # $Id: Google.pm 9 2008-04-29 21:17:12Z esobchenko $
3              
4             package REST::Google;
5              
6 5     5   49329 use strict;
  5         12  
  5         198  
7 5     5   28 use warnings;
  5         10  
  5         422  
8              
9 5     5   1700 use version; our $VERSION = qv('1.0.8');
  5         5540  
  5         34  
10              
11 5     5   452 use Carp qw/carp croak/;
  5         20  
  5         480  
12              
13 5     5   7617 use JSON::Any;
  5         234347  
  5         39  
14              
15 5     5   138365 use HTTP::Request;
  5         389528  
  5         228  
16 5     5   6929 use LWP::UserAgent;
  5         374362  
  5         372  
17              
18 5     5   63 use URI;
  5         10  
  5         266  
19              
20             require Class::Data::Inheritable;
21             require Class::Accessor;
22              
23 5     5   30 use base qw/Class::Data::Inheritable Class::Accessor/;
  5         9  
  5         9210  
24              
25             __PACKAGE__->mk_classdata("http_referer");
26             __PACKAGE__->mk_classdata("service");
27              
28             __PACKAGE__->mk_accessors(qw/responseDetails responseStatus/);
29              
30 5         602 use constant DEFAULT_ARGS => (
31             'v' => '1.0',
32 5     5   34792 );
  5         331  
33              
34 5     5   28 use constant DEFAULT_REFERER => 'http://example.com';
  5         10  
  5         4576  
35              
36             # private method: used in constructor to get it's arguments
37             sub _get_args {
38 5     5   11 my $proto = shift;
39              
40 5         11 my %args;
41 5 100       29 if ( scalar(@_) > 1 ) {
    50          
42 3 50       16 if ( @_ % 2 ) {
43 0         0 croak "odd number of parameters";
44             }
45 3         19 %args = @_;
46             } elsif ( ref $_[0] ) {
47 0 0       0 unless ( eval { local $SIG{'__DIE__'}; %{ $_[0] } || 1 } ) {
  0 0       0  
  0         0  
  0         0  
48 0         0 croak "not a hashref in args";
49             }
50 0         0 %args = %{ $_[0] };
  0         0  
51             } else {
52 2         8 %args = ( 'q' => shift );
53             }
54              
55 5         65 return { $proto->DEFAULT_ARGS, %args };
56             }
57              
58             sub new {
59 5     5 1 1068 my $class = shift;
60              
61 5         42 my $args = $class->_get_args(@_);
62              
63 5 50       45 croak "attempting to perform request without setting a service URL"
64             unless ( defined $class->service );
65              
66 5         82 my $uri = URI->new( $class->service );
67 5         129697 $uri->query_form( $args );
68              
69 5 50       1534 unless ( defined $class->http_referer ) {
70 0         0 carp "attempting to search without setting a valid http referer header";
71 0         0 $class->http_referer( DEFAULT_REFERER );
72             }
73              
74 5         92 my $request = HTTP::Request->new( GET => $uri, [ 'Referer', $class->http_referer ] );
75              
76 5         1139 my $ua = LWP::UserAgent->new();
77 5         35200 $ua->env_proxy;
78              
79 5         91275 my $response = $ua->request( $request );
80              
81 5 50       1410569 croak sprintf qq/HTTP request failed: %s/, $response->status_line
82             unless $response->is_success;
83              
84 5         133 my $content = $response->content;
85              
86 5         340 my $json = JSON::Any->new();
87 5         1277 my $self = $json->decode($content);
88              
89 5         1061 return bless $self, $class;
90             }
91              
92             sub responseData {
93 0     0 1   my $self = shift;
94 0           return $self->{responseData};
95             }
96              
97             1;