| 1 | # | ||||||||||||||||||||
| 2 | # $Id: Google.pm 9 2008-04-29 21:17:12Z esobchenko $ | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | package REST::Google; | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 5 | ▶ | use version; our $VERSION = qv('1.0.8'); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 5 | ▶ | use Carp qw/carp croak/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 5 | ▶ | use JSON::Any; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 5 | ▶ | use HTTP::Request; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 5 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 5 | ▶ | use URI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 5 | require Class::Data::Inheritable; | |||||||||||||||||||
| 21 | 5 | require Class::Accessor; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 5 | ▶ | use base qw/Class::Data::Inheritable Class::Accessor/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 5 | __PACKAGE__->mk_classdata("http_referer"); | |||||||||||||||||||
| 26 | 5 | __PACKAGE__->mk_classdata("service"); | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 5 | __PACKAGE__->mk_accessors(qw/responseDetails responseStatus/); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 5 | ▶ | use constant DEFAULT_ARGS => ( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | 'v' => '1.0', | ||||||||||||||||||||
| 32 | 5 | ); | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 5 | ▶ | use constant DEFAULT_REFERER => 'http://example.com'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | # private method: used in constructor to get it's arguments | ||||||||||||||||||||
| 37 | 4 | ▶ | sub _get_args { | ||||||||||||||||||
|
Subroutine
_get_args: called CC=6 SCAR=25.1
| |||||||||||||||||||||
| 38 | 4 | my $proto = shift; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 4 | my %args; | |||||||||||||||||||
| 41 | 4 | ▶ | if ( scalar(@_) > 1 ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 42 | 2 | ▶ | if ( @_ % 2 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 0 | croak "odd number of parameters"; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | 2 | %args = @_; | |||||||||||||||||||
| 46 | } elsif ( ref $_[0] ) { | ||||||||||||||||||||
| 47 | 0 | ▶ | unless ( eval { local $SIG{'__DIE__'}; %{ $_[0] } || 1 } ) { | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
%{$_[0];} || 1
MC/DC0%0/1
%{$_[0];} || 1
%{$_[0];}
Truth table0%0/2
A
A= %{$_[0];}
| |||||||||||||||||||||
| 48 | 0 | croak "not a hashref in args"; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | 0 | %args = %{ $_[0] }; | |||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||
| 52 | 2 | %args = ( 'q' => shift ); | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 4 | return { $proto->DEFAULT_ARGS, %args }; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 4 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=16.0
Pod
documented
| |||||||||||||||||||||
| 59 | 4 | my $class = shift; | |||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 4 | my $args = $class->_get_args(@_); | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 4 | ▶ | croak "attempting to perform request without setting a service URL" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | unless ( defined $class->service ); | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 4 | my $uri = URI->new( $class->service ); | |||||||||||||||||||
| 67 | 4 | $uri->query_form( $args ); | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 4 | ▶ | unless ( defined $class->http_referer ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | carp "attempting to search without setting a valid http referer header"; | |||||||||||||||||||
| 71 | 0 | $class->http_referer( DEFAULT_REFERER ); | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 4 | my $request = HTTP::Request->new( GET => $uri, [ 'Referer', $class->http_referer ] ); | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 4 | my $ua = LWP::UserAgent->new(); | |||||||||||||||||||
| 77 | 4 | $ua->env_proxy; | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 4 | my $response = $ua->request( $request ); | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 4 | ▶ | croak sprintf qq/HTTP request failed: %s/, $response->status_line | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | unless $response->is_success; | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | my $content = $response->content; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | my $json = JSON::Any->new(); | |||||||||||||||||||
| 87 | 0 | my $self = $json->decode($content); | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | return bless $self, $class; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | sub responseData { | ||||||||||||||||||
|
Subroutine
responseData: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 93 | 0 | my $self = shift; | |||||||||||||||||||
| 94 | 0 | return $self->{responseData}; | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 5 | 1; | |||||||||||||||||||