lib/eBay/API/Simple/HTML.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/eBay/API/Simple/Finding.pm ↑ Summary lib/eBay/API/Simple/JSON.pm »
1 package eBay::API::Simple::HTML;
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::HTML - Support for grabbing an HTML page via API call
19  
20 =head1 USAGE
21  
22 my $call = eBay::API::Simple::HTML->new();
23 $call->execute( 'http://en.wikipedia.org/wiki/Main_Page', { a => 'b' } );
24  
25 if ( $call->has_error() ) {
26 die "Call Failed:" . $call->errors_as_string();
27 }
28  
29 # getters for the response DOM or Hash
30 my $dom = $call->response_dom();
31 my $hash = $call->response_hash();
32  
33 # collect all h2 nodes
34 my @h2 = $dom->getElementsByTagName('h2');
35  
36 foreach my $n ( @h2 ) {
37 print $n->findvalue('text()') . "\n";
38 }
39  
40 =head1 PUBLIC METHODS
41  
42 =head2 new( { %options } }
43  
44 my $call = ebay::API::Simple::HTML->new();
45  
46 =cut
47  
48 sub new {
49 my $class = shift;
50 my $self = $class->SUPER::new(@_);
51  
52 $self->api_config->{request_method} ||= 'GET';
53
54 return $self;
55 }
56  
57 =head2 prepare( $url, $%args )
58  
59 $call->prepare( 'http://en.wikipedia.org/wiki/Main_Page', { a => 'b' } );
60
61 This method will construct the API request based on the $verb and
62 the $call_data.
63  
64 =head3 Options
65  
66 =over 4
67  
68 =item $url (required)
69  
70 URL for page to fetch
71  
72 =item %$args (optional)
73  
74 The supplied args will be encoded and appended to the URL
75  
76 =back
77  
78 =cut
79  
80 sub prepare {
81 my $self = shift;
82
83 $self->{url} = shift;
84
85 if ( ! defined $self->{url} ) {
86 die "missing url";
87 }
88  
89 # collect the optional args
90 $self->{args} = shift;
91 }
92  
93 =head2 response_hash
94  
95 Custom response_hash method, uses the output from LibXML to generate the
96 hash instead of the raw response body.
97  
98 =cut
99  
100 sub response_hash {
101 my $self = shift;
102  
103 if ( ! defined $self->{response_hash} ) {
104 $self->{response_hash} = XMLin( $self->response_dom->toString(),
105 forcearray => [],
106 keyattr => []
107 );
108 }
109  
110 return $self->{response_hash};
111 }
112  
113 =head2 response_dom
114  
115 Custom response_dom method, provides a more relaxed parsing to better handle HTML.
116  
117 =cut
118  
119 sub response_dom {
120 my $self = shift;
121  
122 if ( ! defined $self->{response_dom} ) {
123 require XML::LibXML;
124 my $parser = XML::LibXML->new();
125 $parser->recover(1);
126 $parser->recover_silently(1);
127  
128 eval {
129 $self->{response_dom} =
130 $parser->parse_html_string( $self->response_content );
131 };
132 if ( $@ ) {
133 $self->errors_append( { 'parsing_error' => $@ } );
134 }
135 }
136  
137 return $self->{response_dom};
138 }
139  
140 =head1 BASECLASS METHODS
141  
142 =head2 request_agent
143  
144 Accessor for the LWP::UserAgent request agent
145  
146 =head2 request_object
147  
148 Accessor for the HTTP::Request request object
149  
150 =head2 request_content
151  
152 Accessor for the complete request body from the HTTP::Request object
153  
154 =head2 response_content
155  
156 Accessor for the HTTP response body content
157  
158 =head2 response_object
159  
160 Accessor for the HTTP::Request response object
161  
162 =head2 nodeContent( $tag, [ $dom ] )
163  
164 Helper for LibXML that retrieves node content
165  
166 =head2 errors
167  
168 Accessor to the hashref of errors
169  
170 =head2 has_error
171  
172 Returns true if the call contains errors
173  
174 =head2 errors_as_string
175  
176 Returns a string of API errors if there are any.
177  
178 =head1 PRIVATE METHODS
179  
180 =head2 _get_request_body
181  
182 This method supplies the XML body for the web service request
183  
184 =cut
185  
186 sub _get_request_body {
187 my $self = shift;
188 my @p;
189
190 if ( $self->api_config->{request_method} ne 'GET' ) {
191 for my $k ( keys %{ $self->{args} } ) {
192 push( @p, ( $k . '=' . uri_escape( $self->{args}{$k} ) ) );
193 }
194 }
195
196 return join( '&', @p ) or "";
197 }
198  
199 =head2 _get_request_headers
200  
201 This methods supplies the headers for the HTML API call
202  
203 =cut
204  
205 sub _get_request_headers {
206 my $self = shift;
207
208 my $obj = HTTP::Headers->new();
209 return $obj;
210 }
211  
212 =head2 _get_request_object
213  
214 This method creates the request object and returns to the parent class
215  
216 =cut
217  
218 sub _get_request_object {
219 my $self = shift;
220
221 my $req_url = undef;
222
223 # put the args in the url for a GET request only
224 if ( $self->api_config->{request_method} eq 'GET'
225 && defined $self->{args} ) {
226
227 $req_url = $self->_build_url( $self->{url}, $self->{args} );
228 }
229 else {
230 $req_url = $self->{url};
231 }
232
233 my $request_obj = HTTP::Request->new(
234 ( $self->api_config->{request_method} || 'GET' ),
235 $req_url,
236 $self->_get_request_headers,
237 $self->_get_request_body,
238 );
239  
240 if( $self->api_config->{authorization_basic}{enabled} ) {
241 $request_obj->authorization_basic(
242 $self->api_config->{authorization_basic}{username},
243 $self->api_config->{authorization_basic}{password}
244 );
245 }
246  
247 return $request_obj;
248 }
249  
250 1;
251  
252 =head1 AUTHOR
253  
254 Tim Keefer <tim@timkeefer.com>
255  
256 =head1 COPYRIGHT
257  
258 Tim Keefer 2009
259  
260 =cut
« lib/eBay/API/Simple/Finding.pm ↑ Summary lib/eBay/API/Simple/JSON.pm »