Permalink
Browse files

Merge branch 'release/0.001'

  • Loading branch information...
2 parents f7acf9b + 557d057 commit 604026818675e28432fc783ba2386de908b2fd59 @cwimmer committed Mar 4, 2012
Showing with 262 additions and 0 deletions.
  1. +7 −0 .gitignore
  2. +4 −0 Changes
  3. +30 −0 dist.ini
  4. +119 −0 lib/WWW/YQL.pm
  5. +21 −0 t/000_list_tables.t
  6. +16 −0 t/001_load.t
  7. +23 −0 t/002_empty_query.t
  8. +23 −0 t/003_bad_query.t
  9. +19 −0 t/004_insert_query.t
View
7 .gitignore
@@ -0,0 +1,7 @@
+Makefile
+Makefile.old
+blib/
+MYMETA.*
+pm_to_blib
+WWW-YQL-*
+.build
View
4 Changes
@@ -0,0 +1,4 @@
+{{$NEXT}}
+
+0.001
+- Initial revision includes select and insert functionality with tests.
View
30 dist.ini
@@ -0,0 +1,30 @@
+name = WWW-YQL
+author = Charles A. Wimmer <charles@wimmer.net>
+license = BSD
+copyright_holder = Charles A. Wimmer
+copyright_year = 2012
+
+version = 0.001
+
+[@Basic]
+[MetaResources]
+repository.web = http://github.com/cwimmer/WWW-YQL
+repository.url = git://github.com/cwimmer/WWW-YQL.git
+repository.type = git
+[AutoPrereqs]
+[Prereqs]
+[Prereqs / TestRequires]
+TAP::Harness::JUnit = 0
+Test::Perl::Critic = 0
+Test::Pod::Coverage = 1.08
+Pod::Coverage::TrustPod = 0
+TAP::Formatter::JUnit = 0
+[Test::Perl::Critic]
+[PodCoverageTests]
+[PodSyntaxTests]
+[PkgVersion]
+[NextRelease]
+format = %-9v
+[PodWeaver]
+[PodVersion]
+[Test::Compile]
View
119 lib/WWW/YQL.pm
@@ -0,0 +1,119 @@
+use strict;
+package WWW::YQL;
+use warnings;
+use Moose;
+use URI;
+use URI::QueryParam;
+use LWP::UserAgent;
+use JSON::Any;
+
+
+# ABSTRACT: Module for YQL queries
+# PODNAME: WWW::YQL
+
+has 'ua' => (
+ is => 'rw',
+ isa => 'Object',
+ predicate => 'has_ua',
+ );
+
+has 'json_parser' => (
+ is => 'rw',
+ isa => 'Object',
+ predicate => 'has_json_parser',
+ );
+
+=head1 SYNOPSIS
+
+ use WWW::YQL;
+
+ my $yql = WWW::YQL->new();
+ my $data = $yql->query("show tables");
+
+ foreach my $table ( @{ $data->{'query'}{'results'}{'table'} }){
+ print "$table\n";
+ }
+
+ $data = $yql->query("insert into yahoo.y.ahoo.it (url) values ('http://search.cpan.org/~cwimmer/')");
+
+ my $shorty=$data->{'query'}->{'results'}->{'url'};
+
+ The User Agent that will be used to make the connection is available
+ from the ua() method. You may make changes to the User Agent
+ before running the query() method.
+
+=head2 DESCRIPTION
+
+ This module is used to submit YQL queries and receive their responses.
+
+=cut
+
+sub BUILD{
+ my $self=shift;
+ $self->ua(new LWP::UserAgent());
+ $self->json_parser(new JSON::Any)
+}
+
+=method query
+
+ This method takes one argument, a string. The string is the YQL
+ query string for this request.
+
+ The return value is a has reference representing the result from
+ the YQL service.
+
+=cut
+
+sub query{
+ my $self=shift;
+ my $query=shift;
+ die "No query string specified for WWW::YQL->query()" unless defined $query;
+
+ my $URI=URI->new('http://query.yahooapis.com/v1/public/yql');
+ my $res;
+ my $req;
+ if ($query =~ m/^insert/){
+ $res = $self->{'ua'}->post($URI, {
+ 'q' => $query,
+ 'format' => 'json',
+ });
+ }else{
+ $URI->query_form( 'q' => $query );
+ $URI->query_param( 'format' => 'json' );
+ $req = HTTP::Request->new(GET => $URI);
+ $res = $self->{'ua'}->request($req);
+ }
+ if ($res->is_success) {
+ return $self->json_parser->decode($res->content);
+ }
+ else {
+ warn "$URI status ".$res->status_line;
+ return "Bad request: $query";
+ }
+
+}
+
+1;
+=head1 KNOWN BUGS
+
+None known at this time. Please log issues at:
+
+https://github.com/cwimmer/WWW-YQL/issues
+
+=head1 AVAILABILITY
+
+Source code is available on GitHub:
+
+https://github.com/cwimmer/WWW-YQL
+
+Module available on CPAN as WWW::YQL:
+
+http://search.cpan.org/~cwimmer/
+
+=cut
+
+=begin Pod::Coverage
+
+BUILD
+
+=end Pod::Coverage
View
21 t/000_list_tables.t
@@ -0,0 +1,21 @@
+ # -*- perl -*-
+
+use strict;
+use warnings;
+use Test::More;
+
+use WWW::YQL;
+use List::MoreUtils qw(any);
+
+my $yql = WWW::YQL->new();
+isa_ok($yql, 'WWW::YQL');
+
+$yql->ua->env_proxy;
+
+my $data = $yql->query("show tables");
+
+ok( any { $_ eq 'yahoo.y.ahoo.it' } @{ $data->{'query'}{'results'}{'table'} },
+ 'found table yahoo.y.ahoo.it' );
+
+done_testing();
+exit(0);
View
16 t/001_load.t
@@ -0,0 +1,16 @@
+ # -*- perl -*-
+
+use strict;
+use warnings;
+use Test::More;
+
+BEGIN { use_ok('WWW::YQL') };
+
+can_ok('WWW::YQL', ('new'));
+
+use WWW::YQL;
+my $yql=new WWW::YQL();
+
+isa_ok($yql, 'WWW::YQL');
+
+done_testing();
View
23 t/002_empty_query.t
@@ -0,0 +1,23 @@
+ # -*- perl -*-
+
+use strict;
+use warnings;
+use Test::More;
+
+use WWW::YQL;
+use List::MoreUtils qw(any);
+
+my $yql = WWW::YQL->new();
+isa_ok($yql, 'WWW::YQL');
+
+$yql->ua->env_proxy;
+
+my $data;
+eval{
+ $data = $yql->query();
+};
+diag($@);
+like ($@, qr/No query string specified for WWW::YQL->query/, "Correctly detected empty request");
+done_testing();
+exit(0);
+
View
23 t/003_bad_query.t
@@ -0,0 +1,23 @@
+ # -*- perl -*-
+
+use strict;
+use warnings;
+use Test::More;
+
+use WWW::YQL;
+use List::MoreUtils qw(any);
+
+my $yql = WWW::YQL->new();
+isa_ok($yql, 'WWW::YQL');
+
+$yql->ua->env_proxy;
+
+my $data;
+eval{
+ $data = $yql->query("bogus");
+};
+
+like ($@, qr//, "Correctly detected bad request");
+done_testing();
+exit(0);
+
View
19 t/004_insert_query.t
@@ -0,0 +1,19 @@
+ # -*- perl -*-
+
+use strict;
+use warnings;
+use Test::More;
+
+use WWW::YQL;
+use List::MoreUtils qw(any);
+
+my $yql = WWW::YQL->new();
+
+my $data = $yql->query("insert into yahoo.y.ahoo.it (url) values ('http://search.cpan.org/~cwimmer/')");
+
+my $shorty=$data->{'query'}->{'results'}->{'url'};
+$data = $yql->query("select * from yahoo.y.ahoo.it where url='".$shorty."'");
+my $long=$data->{'query'}->{'results'}->{'url'};
+like($long, qr|http://search.cpan.org/~cwimmer/|, "Select returns same as insert");
+done_testing();
+exit(0);

0 comments on commit 6040268

Please sign in to comment.