Technorati/ApiQuery.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Technorati/SearchApiQuery.pm ↑ Summary
1 package WebService::Technorati::ApiQuery;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use XML::XPath;
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 use WebService::Technorati::Exception;
10  
11 use constant DEFAULT_API_HOST_URL => 'http://api.technorati.com';
12  
13  
14 BEGIN {
15 use vars qw ($VERSION $DEBUG);
16 $VERSION = 0.04;
17 $DEBUG = 0;
18 }
19  
20 my $api_host_url = '';
21  
22 =head1 NAME
23  
24 WebService::Technorati::ApiQuery - a base class for web services client queries
25  
26 =head1 SYNOPSIS
27  
28 This class has no constructor, as there's little use instantiating one. The fun
29 is in the derived classes.
30  
31 =head1 DESCRIPTION
32  
33 When adding a new API call client, this class provides a lot scaffolding such as
34 query string building, HTTP protocol stuff, XML::XPath object creation, and other
35 common behaviors.
36  
37 =head1 USAGE
38  
39 This class is mostly utility functions that are inherited by ApiQuery derivations.
40  
41  
42  
43  
44 =head1 BUGS
45  
46 No bugs currently open
47  
48 =head1 SUPPORT
49  
50 Join the Technorati developers mailing list at
51 http://mail.technorati.com/mailman/listinfo/developers
52  
53 =head1 AUTHOR
54  
55 Ian Kallen
56 ikallen _at_ technorati.com
57 http://developers.technorati.com
58  
59 =head1 COPYRIGHT
60  
61 This program is free software; you can redistribute
62 it and/or modify it under the terms of the following
63 Creative Commons License:
64 http://creativecommons.org/licenses/by/2.0
65 as well as the indemnification provisions of the
66 Apache 2.0 style license, the full text of which can be
67 found in the LICENSE file included with this module.
68  
69 =head1 SEE ALSO
70  
71 perl(1).
72  
73 =cut
74  
75 =head2 apiHostUrl
76  
77 Usage : apiHostUrl('http://developers.technorati.com')
78 Purpose : gets/sets the base URL
79 Returns : a URL string
80 Argument : a base URL, if setting the value; otherwise none
81 Throws : none
82 Comments : Instantiations of an ApiQuery subclass may want to change
83 which api host they connect to (i.e. for beta testing
84 interface changes that aren't yet deployed to the default
85 host, http://api.technorati.com).
86 See Also : WebService::Technorati
87  
88 =cut
89  
90 sub apiHostUrl {
91 my $self = shift;
92 my $change = shift;
93 if ($change) {
94 $api_host_url = $change;
95 }
96 if ($api_host_url) {
97 return $api_host_url;
98 }
99 return DEFAULT_API_HOST_URL;
100 }
101  
102  
103 =head2 fetch_url
104  
105 Usage : fetch_url('http://developers.technorati.com')
106 Purpose : fetches the URL contents
107 Returns : a scalar of the content data
108 Argument : a URL
109 Throws : WebService::Technorati::NetworkException if the URL contents
110 cannot be fetched
111 Comments : the underlying implementation uses LWP::UserAgent
112 See Also : WebService::Technorati
113  
114 =cut
115
116 sub fetch_url {
117 my $url = shift;
118 my $ua = LWP::UserAgent->new;
119 my $request = HTTP::Request->new('GET', $url);
120 my $response = $ua->request($request);
121 if ($response->is_success) {
122 return $response->content;
123 } else {
124 print $response->code,"\n";
125 print $response->error_as_HTML;
126 WebService::Technorati::NetworkException->throw("fetching $url failed, stopping");
127 }
128 }
129  
130 =head2 build_query_string
131  
132 Usage : build_query_string($hashref);
133 Purpose : transforms the keys/values into a query string
134 Returns : the query string
135 Argument : a hash reference
136 Throws : none
137 Comments : multi value keys are not yet accounted for
138 See Also : WebService::Technorati
139  
140 =cut
141  
142 sub build_query_string {
143 my $params = shift;
144 my @pairs = ();
145 while (my($key,$val) = each %{$params}) {
146 push(@pairs, "$key=$val");
147 }
148 return join('&', @pairs);
149 }
150  
151 =head2 execute
152  
153 Usage : build_query_string($apiurl, $hashref);
154 Purpose : handles the low level execution cycle of an API call
155 Returns : void
156 Argument : a hash reference
157 Throws : none
158 Comments : calls build_query_string, fetch_url, instantiates XML::XPath
159 and calls readResults
160 See Also : WebService::Technorati
161  
162 =cut
163  
164 sub execute {
165 my $self = shift;
166 my $url = shift;
167 my $args = shift;
168 $url .= '?' . build_query_string($args);
169 my $result_xml = fetch_url($url);
170 my $result_xp = XML::XPath->new( xml => $result_xml );
171 $self->readResults($result_xp);
172 }
173  
174  
175 =head2 readResults
176  
177 Usage : readResults($xpath_data);
178 Purpose : this is an abstract method
179 Returns : void
180 Argument : an XML::XPath representation of an API response
181 Throws : WebService::Technorati::MethodNotImplementedException if not overriden
182 Comments : derived classes must implement this in order to use execute(...)
183 See Also : WebService::Technorati
184  
185 =cut
186  
187 sub readResults {
188 WebService::Technorati::MethodNotImplementedException->throw(
189 "abstract methond 'readResults()' not implemented");
190 }
191  
192 1;
193  
194 __END__
« Technorati/SearchApiQuery.pm ↑ Summary