Permalink
Browse files

Add rspec tests for Yql::Client#get and make #parameters,

#add_format, add_diagnostics methods private
  • Loading branch information...
1 parent 9219de0 commit c33d909ec6906b2e6c744f0977b1277f4b16906c nas committed Jun 24, 2010
Showing with 92 additions and 14 deletions.
  1. +14 −12 lib/yql/client.rb
  2. +78 −2 spec/yql/client_spec.rb
View
@@ -1,57 +1,59 @@
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
attr_reader :version
-
+
def initialize(args={})
@diagnostics = args[:diagnostics] || false
@version = args[:version] || VERSION
@format = args[:format] || 'xml'
@query = args[:query]
end
-
+
def query
@query.kind_of?(Yql::QueryBuilder) ? @query.to_s : @query
end
-
+
def path_without_domain
"/#{version}/#{URL_SUFFIX}"
end
-
+
def get
if query.nil?
- raise Yql::IncompleteRequestParameter, "Query not specified"
+ raise Yql::IncompleteRequestParameter, "You must set the query attribute for the Yql::Client object before sending the request"
end
http = Net::HTTP.new(BASE_URL, Net::HTTP.https_default_port)
http.use_ssl = true
Yql::Response.new(http.post(path_without_domain, parameters), format)
end
-
+
+ private
+
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
View
@@ -17,7 +17,7 @@
it "should have the api version set to 'v1' by default" do
@yql_client.version.should eql('v1')
end
-
+
it "should have the diagnostics turned off" do
@yql_client.diagnostics.should be_false
end
@@ -37,7 +37,7 @@
it "should set the api version" do
@yql_client.version.should eql('v2')
end
-
+
it "should have the diagnostics turned on" do
@yql_client.diagnostics.should be_true
end
@@ -139,5 +139,81 @@
end
end
+
end
+
+ describe "#get" do
+
+ before(:each) do
+ @yql_client = Yql::Client.new
+ end
+
+ context "when query attribute is not set" do
+
+ it "should raise error" do
+ lambda { @yql_client.get }.should raise_error(Yql::IncompleteRequestParameter, "You must set the query attribute for the Yql::Client object before sending the request")
+ end
+
+ end
+
+ context "when query attribute is set" do
+
+ before(:each) do
+ @yql_client.query = "addquery"
+ @net_http = stub(:use_ssl= => true, :post => 'response')
+ Net::HTTP.stub!(:new).and_return(@net_http)
+ @response_object = stub
+ Yql::Response.stub!(:new).and_return(@response_object)
+ end
+
+ it "should create a new http object for connecting to yahoo YQL" do
+ Net::HTTP.should_receive(:new).with('query.yahooapis.com', Net::HTTP.https_default_port).and_return(@net_http)
+ @yql_client.get
+ end
+
+ it "should set the ssl for http object" do
+ @net_http.should_receive(:use_ssl=).with(true)
+ @yql_client.get
+ end
+
+ it "should return the response object" do
+ @yql_client.get.should eql(@response_object)
+ end
+
+ context "and format is set to default ie, xml" do
+
+ it "should send the request to get data" do
+ @net_http.should_receive(:post).with('/v1/public/yql', 'q=addquery&env=http://datatables.org/alltables.env&format=xml')
+ @yql_client.get
+ end
+
+ it "should create the response object" do
+ Yql::Response.should_receive(:new).with('response', 'xml')
+ @yql_client.get
+ end
+
+ end
+
+ context "and format is set to json" do
+
+ before(:each) do
+ @yql_client.format = 'json'
+ end
+
+ it "should send the request to get data" do
+ @net_http.should_receive(:post).with('/v1/public/yql', 'q=addquery&env=http://datatables.org/alltables.env&format=json')
+ @yql_client.get
+ end
+
+ it "should create the response object" do
+ Yql::Response.should_receive(:new).with('response', 'json')
+ @yql_client.get
+ end
+
+ end
+
+ end
+
+ end
+
end

0 comments on commit c33d909

Please sign in to comment.