| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 9 |  |  | 9 |  | 44 | use strict; | 
|  | 9 |  |  |  |  | 14 |  | 
|  | 9 |  |  |  |  | 300 |  | 
| 2 | 9 |  |  | 9 |  | 43 | use warnings; | 
|  | 9 |  |  |  |  | 24 |  | 
|  | 9 |  |  |  |  | 475 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | package WebService::Nestoria::Search::Request; | 
| 5 |  |  |  |  |  |  | $WebService::Nestoria::Search::Request::VERSION = '1.022012'; | 
| 6 | 9 |  |  | 9 |  | 5043 | use WebService::Nestoria::Search::Response; | 
|  | 9 |  |  |  |  | 19 |  | 
|  | 9 |  |  |  |  | 245 |  | 
| 7 | 9 |  |  | 9 |  | 8088 | use JSON; | 
|  | 9 |  |  |  |  | 101976 |  | 
|  | 9 |  |  |  |  | 46 |  | 
| 8 | 9 |  |  | 9 |  | 12763 | use XML::Simple; | 
|  | 9 |  |  |  |  | 86008 |  | 
|  | 9 |  |  |  |  | 84 |  | 
| 9 | 9 |  |  | 9 |  | 10392 | use LWP::UserAgent; | 
|  | 9 |  |  |  |  | 382273 |  | 
|  | 9 |  |  |  |  | 327 |  | 
| 10 | 9 |  |  | 9 |  | 80 | use HTTP::Request; | 
|  | 9 |  |  |  |  | 17 |  | 
|  | 9 |  |  |  |  | 232 |  | 
| 11 | 9 |  |  | 9 |  | 48 | use URI; | 
|  | 9 |  |  |  |  | 21 |  | 
|  | 9 |  |  |  |  | 3971 |  | 
| 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.022012 | 
| 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 | 162 | my $class = shift; | 
| 27 | 111 |  |  |  |  | 142 | my $self = shift; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 111 |  |  |  |  | 544 | 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 | 355 | my $self = shift; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 55 | 100 |  |  |  | 153 | unless ( $self->{_uri} ) { | 
| 44 | 54 |  |  |  |  | 287 | $self->{_uri} = URI->new($self->{ActionUrl}, 'http'); | 
| 45 | 54 |  |  |  |  | 64151 | $self->{_uri}->query_form( %{ $self->{Params} } ); | 
|  | 54 |  |  |  |  | 353 |  | 
| 46 |  |  |  |  |  |  | } | 
| 47 | 55 |  |  |  |  | 7621 | 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 | 907 | my $self = shift; | 
| 59 | 54 |  |  |  |  | 155 | 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 | 150 | my $self = shift; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 53 |  |  |  |  | 137 | $WebService::Nestoria::Search::RecentRequestUrl = $self->url; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 53 |  | 33 |  |  | 340 | $UA ||= LWP::UserAgent->new(agent => $self->{AppId}); | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 53 |  |  |  |  | 240 | my $response = $UA->get($WebService::Nestoria::Search::RecentRequestUrl); | 
| 81 | 53 |  |  |  |  | 26368 | sleep $WebService::Nestoria::Search::SleepTime; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 53 | 50 | 33 |  |  | 280 | unless ( $response && $response->is_success ) { | 
| 84 | 0 |  |  |  |  | 0 | $@ = "couldn't make request"; | 
| 85 | 0 |  |  |  |  | 0 | return; | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 53 |  |  |  |  | 712 | my $raw = $response->content; | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 53 | 100 |  |  |  | 669 | if ($self->{Params}{encoding} eq 'json') { | 
|  |  | 50 |  |  |  |  |  | 
| 91 | 52 |  |  |  |  | 193 | my $response_obj = from_json($raw); | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 52 | 50 |  |  |  | 22203 | if ( ref $response_obj ) { | 
| 94 | 52 |  |  |  |  | 485 | 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 |  |  |  | 283940 | if(ref($response_obj->{response}{listings}) eq 'HASH') { | 
| 106 | 0 |  |  |  |  | 0 | $response_obj->{response}{listings} = [ $response_obj->{response}{listings} ]; | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 1 | 50 |  |  |  | 8 | if (ref($response_obj)) { | 
| 110 | 1 |  |  |  |  | 12 | 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; |