File Coverage

blib/lib/WebService/Nestoria/Search/Request.pm
Criterion Covered Total %
statement 50 56 89.2
branch 9 14 64.2
condition 2 6 33.3
subroutine 12 12 100.0
pod 3 4 75.0
total 76 92 82.6


line stmt bran cond sub pod time code
1 9     9   47 use strict;
  9         15  
  9         254  
2 9     9   44 use warnings;
  9         17  
  9         551  
3              
4             package WebService::Nestoria::Search::Request;
5             $WebService::Nestoria::Search::Request::VERSION = '1.022011';
6 9     9   5393 use WebService::Nestoria::Search::Response;
  9         22  
  9         252  
7 9     9   8364 use JSON;
  9         106440  
  9         46  
8 9     9   12473 use XML::Simple;
  9         94393  
  9         67  
9 9     9   9953 use LWP::UserAgent;
  9         402330  
  9         336  
10 9     9   89 use HTTP::Request;
  9         19  
  9         248  
11 9     9   50 use URI;
  9         19  
  9         3890  
12              
13             =head1 NAME
14              
15             WebService::Nestoria::Search::Request - Container object for a WebService::Nestoria::Search request.
16              
17             =head1 VERSION
18              
19             version 1.022011
20              
21             This package is used by WebService::Nestoria::Search and a C object should never need to be explicitly created by the user.
22              
23             =cut
24              
25             sub new {
26 111     111 0 164 my $class = shift;
27 111         154 my $self = shift;
28              
29 111         539 return bless $self, $class;
30             }
31              
32             =head1 Functions
33              
34             =head2 uri
35              
36             Returns a URI object representing the URL that will be fetched by this Request object.
37              
38             =cut
39              
40             sub uri {
41 55     55 1 461 my $self = shift;
42              
43 55 100       168 unless ( $self->{_uri} ) {
44 54         308 $self->{_uri} = URI->new($self->{ActionUrl}, 'http');
45 54         64071 $self->{_uri}->query_form( %{ $self->{Params} } );
  54         361  
46             }
47 55         7505 return $self->{_uri};
48              
49             }
50              
51             =head2 url
52              
53             Returns the URL that will be fetched by this request object as a string.
54              
55             =cut
56              
57             sub url {
58 54     54 1 1234 my $self = shift;
59 54         140 return $self->uri->as_string;
60             }
61              
62             =head2 fetch
63              
64             Contact the Nestoria servers and return a WebService::Nestoria::Search::Response object.
65              
66             If encoding is set to 'json', a WebService::Nestoria::Search::Result object is created for each listing. These can be accessed via the returned WebService::Nestoria::Search::Response object.
67              
68             If the encoding is not 'json', the object returned will contain no WebService::Nestoria::Search::Result objects, and only the C function can be used effectively.
69              
70             =cut
71              
72             our $UA;
73             sub fetch {
74 53     53 1 144 my $self = shift;
75              
76 53         131 $WebService::Nestoria::Search::RecentRequestUrl = $self->url;
77              
78 53   33     381 $UA ||= LWP::UserAgent->new(agent => $self->{AppId});
79              
80 53         247 my $response = $UA->get($WebService::Nestoria::Search::RecentRequestUrl);
81 53         25857 sleep $WebService::Nestoria::Search::SleepTime;
82              
83 53 50 33     282 unless ( $response && $response->is_success ) {
84 0         0 $@ = "couldn't make request";
85 0         0 return;
86             }
87              
88 53         717 my $raw = $response->content;
89              
90 53 100       706 if ($self->{Params}{encoding} eq 'json') {
    50          
91 52         172 my $response_obj = from_json($raw);
92              
93 52 50       39024 if ( ref $response_obj ) {
94 52         438 return WebService::Nestoria::Search::Response->new($response_obj, $raw);
95             }
96             else {
97 0         0 return;
98             }
99             }
100             elsif ($self->{Params}{encoding} eq 'xml') {
101 1         7 my $response_obj = XMLin($raw);
102            
103             # Returning a listings result in XML will create a hash ref
104             # so it must be forced into an array.
105 1 50       278987 if(ref($response_obj->{response}{listings}) eq 'HASH') {
106 0         0 $response_obj->{response}{listings} = [ $response_obj->{response}{listings} ];
107             }
108            
109 1 50       7 if (ref($response_obj)) {
110 1         15 return WebService::Nestoria::Search::Response->new($response_obj, $raw);
111             }
112             else {
113 0           return;
114             }
115             }
116             else {
117 0           return WebService::Nestoria::Search::Response->new({}, $raw);
118             }
119             }
120              
121             =head1 Copyright
122              
123             Copyright (C) 2009 Lokku Ltd.
124              
125             =head1 Author
126              
127             Alex Balhatchet (alex@lokku.com)
128              
129             Patches supplied by Yoav Felberbaum and Alistair Francis.
130              
131             =cut
132              
133             1;