Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Ruby Wrapper for Yahoo Query Language

branch: master

Merge pull request #1 from EvilScott/master

Add optional param to bypass SSL verification
latest commit 673c1fe71b
Nasir Jamal authored October 14, 2011
Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 History.txt
Octocat-spinner-32 Manifest.txt
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 yql.gemspec
README.rdoc

DESCRIPTION

A basic Ruby Wrapper for interacting programatically with YQL API.

The idea for this library came while attending the world's first Sciencehackday (@sciencehackday) in London - www.guardian.co.uk/open-platform/blog/science-hack-day-at-the-guardian.

Most of the people during science hack day were using YQL to fetch or post their data and some were using rails, so I thought it would be cool to have a ruby library that can provide interface to YQL to do a bunch of things programatically. And I ended up hacking together and releasing version 0.0.1 and 0.0.2 during the event.

TODO

  1. Add Unit Tests

  2. Add oauth

  3. Add YQL table creation

  4. Add YQL update / insert / delete operations

INSTALLATION

sudo gem source –add rubygems.org

sudo gem install yql

USAGE

require 'rubygems'

require 'yql'

Building Query and connecting to YQL

Initialize Client

yql = Yql::Client.new

Finders

  • Query builder takes table as mandatory parameter.

query = Yql::QueryBuilder.new 'yelp.review.search'

query.to_s #=> “select * from yelp.review.search”

query.find #=> “select * from yelp.review.search limit 1”

query.limit = 4

query.to_s #=> “select * from yelp.review.search limit 4”

query.find_all #=> “select * from yelp.review.search”

Conditions

  • Conditions for a query can be either provided as a string or a hash just like rails

query.conditions = “term like '%pizza%'”

query.to_s #=> “select * from yelp.review.search where term='%pizza%'”

query.conditions = {:term => 'pizza', :location => 'london', 'ywsid' => '6L0Lc-yn1OKMkCKeXLD4lg'}

query.to_s #=> “select * from yelp.review.search where term='pizza' and location='london' and ywsid= '6L0Lc-yn1OKMkCKeXLD4lg'”

query.select = 'user_photo_url, state'

yql.query = query

response = yql.get

  • the above method call will give an xml output, set the yql client format to json like so

yql.format = 'json'

response = yql.get #=> Yql::Response object

response.show

  • to print the xml output on console

response.show.to_s

Piped Filters

query.unique = 'name'

query.to_s #=> “select title, Rating, LastReviewIntro from yelp.review.search where ywsid='6L0Lc-yn1OKMkCKeXLD4lg' and term='pizza' and location='london' | unique(field='name')”

query.tail = 4

query.to_s #=> “select title, Rating, LastReviewIntro from yelp.review.search where ywsid='6L0Lc-yn1OKMkCKeXLD4lg' and term='pizza' and location='london' | unique(field='name') | tail(count=4)”

query.reorder_pipe_command :from => 1, :to => 0

query.to_s #=> “select title, Rating, LastReviewIntro from yelp.review.search where ywsid='6L0Lc-yn1OKMkCKeXLD4lg' and term='pizza' and location='london' | tail(count=4) | unique(field='name')”

yql.format = 'json'

response = yql.get #=> Yql::Response object

  • to print the xml output on console

response.show.to_s

Pagination

query.per_page = 10

query.current_page = 1

yql.query = query

response = yql.get #=> Yql::Response object

Describe and show tables

  • To describe a table and see its required parameters, etc.

query = Yql::QueryBuilder.describe_table('yelp.review.search')

  • To see all the table sources and their names in YQL

query = Yql::QueryBuilder.show_tables

yql.query = query

response = yql.get #=> Yql::Response object

response.show

Something went wrong with that request. Please try again.