Skip to content

Commit

Permalink
initial commit with comprehensive select operations
Browse files Browse the repository at this point in the history
  • Loading branch information
nas committed Jun 20, 2010
0 parents commit 61974f4
Show file tree
Hide file tree
Showing 11 changed files with 446 additions and 0 deletions.
4 changes: 4 additions & 0 deletions History.txt
@@ -0,0 +1,4 @@
=== 0.1.1 2010-06-20

* 1 major enhancement:
* Initial release
7 changes: 7 additions & 0 deletions Manifest.txt
@@ -0,0 +1,7 @@
History.txt
Manifest.txt
README.rdoc
lib/yql.rb
lib/yql/client.rb
lib/yql/error.rb
lib/yql/query_builder.rb
104 changes: 104 additions & 0 deletions README.rdoc
@@ -0,0 +1,104 @@
==DESCRIPTION

A basic Ruby Wrapper for interacting programatically with YQL API.

I started working on this library during ScienceHackDay held at Guardian, London on 19/06/2010-20/06/2010

==TODO

1. Add Unit Tests
2. Add oauth
3. Add table creation
4. Add update / insert / delete operations

==INSTALLATION

sudo gem source --add http://rubygems.org

sudo gem install yql


==USAGE

require 'rubygems'

require 'yql'

===Building Query


====Finders

yql = Yql::Client.new

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

query.conditions = "term like '%pizza%'"

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

query.conditions = {:term => '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
result = yql.get

yql.format = 'json'
result = yql.get


===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'
result = yql.get


====Pagination

query.per_page = 10
query.current_page = 1

yql.query = query
result = yql.get


===Describe and show tables

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

query = Yql::QueryBuilder.show_tables

yql.query = query
result = yql.get
11 changes: 11 additions & 0 deletions lib/yql.rb
@@ -0,0 +1,11 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

require 'rubygems'
require 'CGI'
require 'net/http'
require 'net/https'
require 'rexml/document'
require 'yql/error.rb'
require 'yql/client.rb'
require 'yql/query_builder.rb'
65 changes: 65 additions & 0 deletions lib/yql/client.rb
@@ -0,0 +1,65 @@
require 'net/http'
module Yql

class Client

BASE_URL = 'query.yahooapis.com'
VERSION = 'v1'
URL_SUFFIX = 'public/yql'
YQL_ENV = 'http://datatables.org/alltables.env'

attr_accessor :query, :diagnostics, :format

def initialize(args={})
@diagnostics = args[:diagnostics] #true or false
@version = args[:version]
@query = args[:query]
@format = args[:format] || 'xml'
end

def query
@query.kind_of?(Yql::QueryBuilder) ? @query.to_s : @query
end

def version
@version ||= VERSION
end

def full_url
"#{version}/#{URL_SUFFIX}"
end

def get
if query.nil?
raise Yql::IncompleteRequestParameter, "Query not specified"
end
http = Net::HTTP.new(BASE_URL, Net::HTTP.https_default_port)
http.use_ssl = true
path = "/#{version}/#{URL_SUFFIX}"
result = http.post(path, parameters)
#raise(Yql::ResponseFailure, result.response) unless result.code == '200'
return result.body unless format == 'xml'
REXML::Document.new(result.body)
end

def parameters
url_parameters = "q=#{CGI.escape(query)}&env=#{YQL_ENV}"
url_parameters = add_format(url_parameters)
add_diagnostics(url_parameters)
end

def add_format(existing_parameters)
return unless existing_parameters
return existing_parameters unless format
return existing_parameters + "&format=#{format}"
end

def add_diagnostics(existing_parameters)
return unless existing_parameters
return existing_parameters unless diagnostics
return existing_parameters + "&diagnostics=true"
end

end

end
19 changes: 19 additions & 0 deletions lib/yql/error.rb
@@ -0,0 +1,19 @@
module Yql
class Error < StandardError

def initialize(data)
@data = data
super
end
end

class ResponseFailure < Error
end

class NoResult < Error
end

class IncompleteRequestParameter < Error
end

end

0 comments on commit 61974f4

Please sign in to comment.