File Coverage

blib/lib/WebService/YQL.pm
Criterion Covered Total %
statement 49 49 100.0
branch 4 6 66.6
condition 1 3 33.3
subroutine 13 13 100.0
pod 3 3 100.0
total 70 74 94.5


line stmt bran cond sub pod time code
1             package WebService::YQL;
2              
3 2     2   106850 use strict;
  2         6  
  2         84  
4 2     2   11 use warnings;
  2         4  
  2         64  
5              
6 2     2   129388 use URI;
  2         30566  
  2         68  
7 2     2   1960 use URI::QueryParam;
  2         1620  
  2         64  
8 2     2   116568 use LWP::UserAgent;
  2         592793  
  2         87  
9 2     2   2598 use JSON::Any;
  2         122019  
  2         16  
10              
11             BEGIN {
12 2     2   34854 use vars qw($VERSION);
  2         5  
  2         95  
13 2     2   847 $VERSION = '0.04';
14             }
15              
16             =head1 NAME
17              
18             WebService::YQL - Simple interface for Yahoo Query Language
19              
20             =head1 SYNOPSIS
21              
22             use WebService::YQL;
23            
24             my $yql = WebService::YQL->new;
25              
26             my $data = $yql->query("select * from search.web where query = 'YQL'");
27             for my $result ( @{ $data->{'query'}{'results'}{'result'} } ) {
28             print $result->{'title'}, "\n";
29             print $result->{'abstract'}, "\n";
30             print '* ', $result->{'url'}, "\n\n";
31             }
32              
33             =head1 DESCRIPTION
34              
35             This is a simple wrapper to the Yahoo Query Language service. Instead of
36             manually sending a GET request to Yahoo and getting XML or JSON you can
37             now use a simple function call and get a deep Perl data structure.
38              
39             =head1 USAGE
40              
41             my $data = $yql->query("select * from table");
42              
43             =head1 FUNCTIONS
44              
45             =head2 new
46              
47             New instance of WebService::YQL. Accepts one argument, 'env', to load more data tables,
48             e.g. WebService::YQL->new(env => 'http://datatables.org/alltables.env');
49              
50             =cut
51              
52             sub new {
53 2     2 1 33 my ($class, %params) = @_;
54              
55 2   33     24 my $self = bless ({}, ref ($class) || $class);
56              
57 2         24 $self->{'_base_url'} = URI->new('http://query.yahooapis.com/v1/public/yql');
58 2         25431 $self->{'_env'} = $params{'env'}; # || 'http://datatables.org/alltables.env';
59             # $self->{'_other_query_args'} = ...
60              
61             # Instantiate helper objects
62 2         25 $self->{'_ua'} = LWP::UserAgent->new;
63 2         796171 $self->{'_json'} = JSON::Any->new;
64              
65 2         216 return $self;
66             }
67              
68             =head2 query
69              
70             Run an YQL query. Accepts one argument, the query as a string.
71              
72             =cut
73              
74             sub query {
75 2     2 1 818 my ($self, $query) = @_;
76 2 50       11 die "You must specify a yql statement to execute" unless defined $query;
77              
78 2         10 my $url = $self->_base_url;
79 2         188 $url->query_form( q => $query );
80              
81 2         215 my $response = $self->_request($url);
82 2         53 my $decoded = $self->{'_json'}->decode($response);
83              
84 1         218 return $decoded;
85             }
86              
87             =head2 useragent
88              
89             Returns the LWP::UserAgent object used to contact yahoo. You can tweak that
90             object as required, e.g. $yql->useragent->env_proxy in order to use the proxy
91             set in environment.
92              
93             =cut
94              
95             sub useragent {
96 1     1 1 7 my ($self) = @_;
97 1         6 return $self->{'_ua'};
98             }
99              
100             sub _request {
101 2     2   5 my ($self, $url) = @_;
102              
103 2         18 $url->query_param( format => 'json' );
104 2 50       405 $url->query_param( env => $self->{'_env'} ) if $self->{'_env'};
105              
106             # XXX POST for insert/update/delete ?
107 2         19 my $req = HTTP::Request->new(GET => $url);
108 2         175 my $res = $self->{'_ua'}->request($req);
109              
110             # Check the outcome of the response
111 2 100       637894 if ($res->is_success) {
112 1         20 return $res->content;
113             }
114             else {
115 1         26 warn "$url status ".$res->status_line;
116 1         245 return undef;
117             }
118             }
119              
120             sub _base_url {
121 2     2   6 my ($self) = @_;
122 2         28 return $self->{'_base_url'}->clone;
123             }
124              
125             =head1 BUGS
126              
127             As any software, it has bugs, but I'm hunting them down.
128              
129             =head1 SUPPORT
130              
131             Check the source code or contact author for support.
132              
133             =head1 AUTHOR
134              
135             Viorel Stirbu
136             CPAN ID: VIORELS
137             http://stirbu.name
138              
139             =head1 COPYRIGHT
140              
141             This program is free software; you can redistribute
142             it and/or modify it under the same terms as Perl itself.
143              
144             The full text of the license can be found in the
145             LICENSE file included with this module.
146              
147             =head1 SEE ALSO
148              
149             http://developer.yahoo.com/yql
150             http://developer.yahoo.com/yql/console
151              
152             =cut
153              
154             1;
155             # The preceding line will help the module return a true value
156