File Coverage

blib/lib/WebService/Nestoria/Search/Request.pm
Criterion Covered Total %
statement 52 56 92.8
branch 10 14 71.4
condition 4 6 66.6
subroutine 12 12 100.0
pod 3 4 75.0
total 81 92 88.0


line stmt bran cond sub pod time code
1 9     9   46 use strict;
  9         16  
  9         240  
2 9     9   45 use warnings;
  9         33  
  9         497  
3              
4             package WebService::Nestoria::Search::Request;
5             $WebService::Nestoria::Search::Request::VERSION = '1.022010';
6 9     9   5243 use WebService::Nestoria::Search::Response;
  9         22  
  9         253  
7 9     9   8258 use JSON;
  9         110875  
  9         48  
8 9     9   12784 use XML::Simple;
  9         94289  
  9         63  
9 9     9   10097 use LWP::UserAgent;
  9         387086  
  9         338  
10 9     9   85 use HTTP::Request;
  9         20  
  9         248  
11 9     9   48 use URI;
  9         17  
  9         3835  
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.022010
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 104     104 0 199 my $class = shift;
27 104         179 my $self = shift;
28              
29 104         609 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 48     48 1 915 my $self = shift;
42              
43 48 100       261 unless ( $self->{_uri} ) {
44 47         441 $self->{_uri} = URI->new($self->{ActionUrl}, 'http');
45 47         67955 $self->{_uri}->query_form( %{ $self->{Params} } );
  47         538  
46             }
47 48         9187 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 47     47 1 2027 my $self = shift;
59 47         275 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             my $UA;
73             sub fetch {
74 46     46 1 207 my $self = shift;
75              
76 46         199 $WebService::Nestoria::Search::RecentRequestUrl = $self->url;
77              
78 46   66     550 $UA ||= LWP::UserAgent->new(agent => $self->{AppId});
79              
80 46         21917 my $response = $UA->get($WebService::Nestoria::Search::RecentRequestUrl);
81 46         150877314 sleep 2;
82              
83 46 100 66     1741 unless ( $response && $response->is_success ) {
84 1         42 $@ = "couldn't make request";
85 1         149 return;
86             }
87              
88 45         2583 my $raw = $response->content;
89              
90 45 100       3473 if ($self->{Params}{encoding} eq 'json') {
    50          
91 44         424 my $response_obj = from_json($raw);
92              
93 44 50       25798 if ( ref $response_obj ) {
94 44         838 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         21 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       269309 if(ref($response_obj->{response}{listings}) eq 'HASH') {
106 0         0 $response_obj->{response}{listings} = [ $response_obj->{response}{listings} ];
107             }
108            
109 1 50       6 if (ref($response_obj)) {
110 1         14 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;