| 1 | package eBay::API::Simple::RSS; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use base 'eBay::API::SimpleBase'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | use HTTP::Request; | ||
| 9 | use HTTP::Headers; | ||
| 10 | use XML::Simple; | ||
| 11 | use URI::Escape; | ||
| 12 | use utf8; | ||
| 13 | |||
| 14 | our $DEBUG = 0; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | eBay::API::Simple::RSS - Support for grabbing an RSS feed via API call | ||
| 19 | |||
| 20 | =head1 USAGE | ||
| 21 | |||
| 22 | my $call = eBay::API::Simple::RSS->new(); | ||
| 23 | $call->execute( | ||
| 24 | 'http://sfbay.craigslist.org/search/sss', | ||
| 25 | { | ||
| 26 | query => 'shirt', | ||
| 27 | format => 'rss', | ||
| 28 | } | ||
| 29 | ); | ||
| 30 | |||
| 31 | if ( $call->has_error() ) { | ||
| 32 | die "Call Failed:" . $call->errors_as_string(); | ||
| 33 | } | ||
| 34 | |||
| 35 | # getters for the response DOM or Hash | ||
| 36 | my $dom = $call->response_dom(); | ||
| 37 | my $hash = $call->response_hash(); | ||
| 38 | |||
| 39 | # collect all item nodes | ||
| 40 | my @items = $dom->getElementsByTagName('item'); | ||
| 41 | |||
| 42 | foreach my $n ( @items ) { | ||
| 43 | print $n->findvalue('title/text()') . "\n"; | ||
| 44 | } | ||
| 45 | |||
| 46 | =head1 PUBLIC METHODS | ||
| 47 | |||
| 48 | =head2 new( { %options } } | ||
| 49 | |||
| 50 | my $call = ebay::API::Simple::RSS->new(); | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my $class = shift; | ||
| 56 | my $self = $class->SUPER::new(@_); | ||
| 57 | |||
| 58 | $self->api_config->{request_method} ||= 'GET'; | ||
| 59 | |||
| 60 | return $self; | ||
| 61 | } | ||
| 62 | |||
| 63 | =head2 prepare( $url, $%args ) | ||
| 64 | |||
| 65 | $call->prepare( | ||
| 66 | 'http://sfbay.craigslist.org/search/sss', | ||
| 67 | { query => 'shirt', format => 'rss', } | ||
| 68 | ); | ||
| 69 | |||
| 70 | This method will construct the API request using the supplied URL. | ||
| 71 | |||
| 72 | =head3 Options | ||
| 73 | |||
| 74 | =over 4 | ||
| 75 | |||
| 76 | =item $url (required) | ||
| 77 | |||
| 78 | Feed URL to fetch | ||
| 79 | |||
| 80 | =item %$args (optional) | ||
| 81 | |||
| 82 | The supplied args will be encoded and appended to the URL | ||
| 83 | |||
| 84 | =back | ||
| 85 | |||
| 86 | =cut | ||
| 87 | sub prepare { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | $self->{url} = shift; | ||
| 91 | |||
| 92 | if ( ! defined $self->{url} ) { | ||
| 93 | die "missing url"; | ||
| 94 | } | ||
| 95 | |||
| 96 | # collect the optional args | ||
| 97 | $self->{args} = shift; | ||
| 98 | } | ||
| 99 | |||
| 100 | =head1 BASECLASS METHODS | ||
| 101 | |||
| 102 | =head2 request_agent | ||
| 103 | |||
| 104 | Accessor for the LWP::UserAgent request agent | ||
| 105 | |||
| 106 | =head2 request_object | ||
| 107 | |||
| 108 | Accessor for the HTTP::Request request object | ||
| 109 | |||
| 110 | =head2 request_content | ||
| 111 | |||
| 112 | Accessor for the complete request body from the HTTP::Request object | ||
| 113 | |||
| 114 | =head2 response_content | ||
| 115 | |||
| 116 | Accessor for the HTTP response body content | ||
| 117 | |||
| 118 | =head2 response_object | ||
| 119 | |||
| 120 | Accessor for the HTTP::Request response object | ||
| 121 | |||
| 122 | =head2 response_dom | ||
| 123 | |||
| 124 | Accessor for the LibXML response DOM | ||
| 125 | |||
| 126 | =head2 response_hash | ||
| 127 | |||
| 128 | Accessor for the hashified response content | ||
| 129 | |||
| 130 | =head2 nodeContent( $tag, [ $dom ] ) | ||
| 131 | |||
| 132 | Helper for LibXML that retrieves node content | ||
| 133 | |||
| 134 | =head2 errors | ||
| 135 | |||
| 136 | Accessor to the hashref of errors | ||
| 137 | |||
| 138 | =head2 has_error | ||
| 139 | |||
| 140 | Returns true if the call contains errors | ||
| 141 | |||
| 142 | =head2 errors_as_string | ||
| 143 | |||
| 144 | Returns a string of API errors if there are any. | ||
| 145 | |||
| 146 | =head1 PRIVATE METHODS | ||
| 147 | |||
| 148 | =head2 _get_request_body | ||
| 149 | |||
| 150 | This method supplies the XML body for the web service request | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub _get_request_body { | ||
| 155 | my $self = shift; | ||
| 156 | my @p; | ||
| 157 | |||
| 158 | if ( $self->api_config->{request_method} ne 'GET' ) { | ||
| 159 | for my $k ( keys %{ $self->{args} } ) { | ||
| 160 | if ( ref( $self->{args}{$k} ) eq 'ARRAY' ) { | ||
| 161 | for my $ap ( @{ $self->{args}{$k} } ) { | ||
| 162 | push( @p, | ||
| 163 | ( $k . '=' . uri_escape_utf8( $ap ) ) | ||
| 164 | ); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | else { | ||
| 168 | push( @p, ( $k . '=' . uri_escape_utf8( $self->{args}{$k} ) ) ); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | return join( '&', @p ) or ""; | ||
| 174 | } | ||
| 175 | |||
| 176 | =head2 _get_request_headers | ||
| 177 | |||
| 178 | This methods supplies the headers for the RSS API call | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | sub _get_request_headers { | ||
| 183 | my $self = shift; | ||
| 184 | |||
| 185 | my $obj = HTTP::Headers->new(); | ||
| 186 | return $obj; | ||
| 187 | } | ||
| 188 | |||
| 189 | =head2 _get_request_object | ||
| 190 | |||
| 191 | This method creates the request object and returns to the parent class | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | sub _get_request_object { | ||
| 196 | my $self = shift; | ||
| 197 | |||
| 198 | my $req_url = undef; | ||
| 199 | |||
| 200 | # put the args in the url for a GET request only | ||
| 201 | if ( $self->api_config->{request_method} eq 'GET' | ||
| 202 | && defined $self->{args} ) { | ||
| 203 | |||
| 204 | $req_url = $self->_build_url( $self->{url}, $self->{args} ); | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | $req_url = $self->{url}; | ||
| 208 | } | ||
| 209 | |||
| 210 | my $request_obj = HTTP::Request->new( | ||
| 211 | ( $self->api_config->{request_method} || 'GET' ), | ||
| 212 | $req_url, | ||
| 213 | $self->_get_request_headers, | ||
| 214 | $self->_get_request_body, | ||
| 215 | ); | ||
| 216 | |||
| 217 | if( $self->api_config->{authorization_basic}{enabled} ) { | ||
| 218 | $request_obj->authorization_basic( | ||
| 219 | $self->api_config->{authorization_basic}{username}, | ||
| 220 | $self->api_config->{authorization_basic}{password} | ||
| 221 | ); | ||
| 222 | } | ||
| 223 | |||
| 224 | return $request_obj; | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | 1; | ||
| 229 | |||
| 230 | =head1 AUTHOR | ||
| 231 | |||
| 232 | Tim Keefer <tim@timkeefer.com> | ||
| 233 | |||
| 234 | =head1 COPYRIGHT | ||
| 235 | |||
| 236 | Tim Keefer 2009 | ||
| 237 | |||
| 238 | =cut | ||