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¶ ↑
-
Add Unit Tests
-
Add oauth
-
Add YQL table creation
-
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
LICENSE:¶ ↑
(The MIT License)
Copyright © 2014 Nasir Jamal
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.%