WebService::Nestoria::Search::Request - Container object for a WebService::Nestoria::Search request.


WebService-Nestoria-Search documentation Contained in the WebService-Nestoria-Search distribution.

Index


Code Index:

NAME

Top

WebService::Nestoria::Search::Request - Container object for a WebService::Nestoria::Search request.

VERSION

Top

version 1.020001

This package is used by WebService::Nestoria::Search and a Request object should never need to be explicitly created by the user.

Functions

Top

uri

Returns a URI object representing the URL that will be fetched by this Request object.

url

Returns the URL that will be fetched by this request object as a string.

fetch

Contact the Nestoria servers and return a WebService::Nestoria::Search::Response object.

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.

If the encoding is not 'json', the object returned will contain no WebService::Nestoria::Search::Result objects, and only the get_raw function can be used effectively.

Copyright

Top

Author

Top

Alex Balhatchet (alex@lokku.com)

Patches supplied by Yoav Felberbaum and Alistair Francis.


WebService-Nestoria-Search documentation Contained in the WebService-Nestoria-Search distribution.
use strict;
use warnings;

package WebService::Nestoria::Search::Request;
BEGIN {
  $WebService::Nestoria::Search::Request::VERSION = '1.020001';
}

use WebService::Nestoria::Search::Response;
use JSON;
use XML::Simple;
use LWP::UserAgent;
use HTTP::Request;
use URI;

sub new {
    my $class = shift;
    my $self = shift;

    return bless $self, $class;
}

sub uri {
    my $self = shift;

    unless ( $self->{_uri} ) {
        $self->{_uri} = new URI ($self->{ActionUrl}, 'http');
        $self->{_uri}->query_form( %{ $self->{Params} } );
    }
    return $self->{_uri};

}

sub url {
    my $self = shift;
    return $self->uri->as_string;
}

my $UA;
sub fetch {
    my $self = shift;

    $WebService::Nestoria::Search::RecentRequestUrl = $self->url;

    $UA ||= new LWP::UserAgent (agent => $self->{AppId});

    my $response = $UA->get($WebService::Nestoria::Search::RecentRequestUrl);
    sleep 2;

    unless ( $response && $response->is_success ) {
        $@ = "couldn't make request";
        return;
    }

    my $raw = $response->content;

    if ($self->{Params}{encoding} eq 'json') {
        my $response_obj = from_json($raw);

        if ( ref $response_obj ) {
            return new WebService::Nestoria::Search::Response ($response_obj, $raw);
        }
        else {
            return;
        }
    }
    elsif ($self->{Params}{encoding} eq 'xml') {
        my $response_obj = XMLin($raw);
     
        # Returning a listings result in XML will create a hash ref
        # so it must be forced into an array.
        if(ref($response_obj->{response}{listings}) eq 'HASH') {
            $response_obj->{response}{listings} = [ $response_obj->{response}{listings} ];
        }
     
        if (ref($response_obj)) {
            return new WebService::Nestoria::Search::Response ($response_obj, $raw);
        }
        else {
            return;
        }
    }
    else {
        return new WebService::Nestoria::Search::Response ({}, $raw);
    }
}

1;