Permalink
Browse files

Add Yql::Response class and modify Yql::Client class to

return response object
  • Loading branch information...
1 parent f370ed0 commit 7925c6e8c1f9e82be4740c70aace86f2418e7781 nas committed Jun 20, 2010
Showing with 46 additions and 10 deletions.
  1. +7 −5 README.rdoc
  2. +2 −1 lib/yql.rb
  3. +1 −4 lib/yql/client.rb
  4. +36 −0 lib/yql/response.rb
View
@@ -61,10 +61,10 @@ query.to_s #=> "select * from yelp.review.search where term='pizza' and location
query.select = 'user_photo_url, state'
yql.query = query
-result = yql.get
+response = yql.get
yql.format = 'json'
-result = yql.get
+response = yql.get #=> Yql::Response object
===Piped Filters
@@ -82,7 +82,7 @@ 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
+response = yql.get #=> Yql::Response object
====Pagination
@@ -91,7 +91,7 @@ query.per_page = 10
query.current_page = 1
yql.query = query
-result = yql.get
+response = yql.get #=> Yql::Response object
===Describe and show tables
@@ -101,4 +101,6 @@ query = Yql::QueryBuilder.describe_table('yelp.review.search')
query = Yql::QueryBuilder.show_tables
yql.query = query
-result = yql.get
+response = yql.get #=> Yql::Response object
+
+response.show
View
@@ -8,4 +8,5 @@
require 'rexml/document'
require 'yql/error.rb'
require 'yql/client.rb'
-require 'yql/query_builder.rb'
+require 'yql/query_builder.rb'
+require 'yql/response.rb'
View
@@ -35,10 +35,7 @@ def get
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)
+ Yql::Response.new(http.post(path, parameters), format)
end
def parameters
View
@@ -0,0 +1,36 @@
+module Yql
+
+ class Response
+
+ def initialize(response, format)
+ @response = response
+ @format = format
+ end
+
+ def show
+ raise Yql::ResponseFailure, body.inspect unless success?
+ output
+ end
+
+ def body
+ @response.body
+ end
+
+ def code
+ @response.code
+ end
+
+ def success?
+ code == '200'
+ end
+
+ private
+
+ def output
+ return body unless @format == 'xml'
+ REXML::Document.new(body)
+ end
+
+ end
+
+end

0 comments on commit 7925c6e

Please sign in to comment.