Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

rename project

add json object transform
  • Loading branch information...
commit d445576e3b5628aa44314cd420fd413858132630 1 parent bb42857
@ccollins authored
View
2  README.md
@@ -1,3 +1,3 @@
-Ebay-api
+rebay
========
Work in progress!
View
2  init.rb
@@ -1 +1 @@
-require File.join(File.dirname(__FILE__), "lib", "ebay")
+require File.join(File.dirname(__FILE__), "lib", "rebay")
View
1  lib/ebay.rb
@@ -1 +0,0 @@
-require 'ebay/finding'
View
1  lib/rebay.rb
@@ -0,0 +1 @@
+require 'rebay/finding'
View
20 lib/ebay/finding.rb → lib/rebay/finding.rb
@@ -1,7 +1,8 @@
require 'net/http'
require 'json'
+require 'ostruct'
-module Ebay
+module Rebay
class Finding
BASE_URL = 'http://svcs.ebay.com/services/search/FindingService/v1'
@@ -67,13 +68,26 @@ def build_request_url(service, params=nil)
url += "&#{key}=#{params[key]}"
end
end
- print url
- print "\n"
+
return url
end
def get_json_response(url)
JSON.parse(Net::HTTP.get_response(URI.parse(url)).body)
end
+
+ def transform_json_response(response)
+ if response.class == Hash
+ r = Hash.new
+ response.keys.each do |k|
+ r[k.to_sym] = transform_json_response(response[k])
+ end
+ return r
+ elsif response.class == Array and response.size == 1
+ return transform_json_response(response[0])
+ else
+ return response
+ end
+ end
end
end
View
28 spec/finding_spec.rb
@@ -2,7 +2,7 @@
APP_ID = ''
-module Ebay
+module Rebay
describe Finding do
it "should specify base url" do
Finding::BASE_URL.should_not be_nil
@@ -163,6 +163,32 @@ module Ebay
end
end
+ context "when parsing response" do
+ before(:each) do
+ @json_happy = JSON.parse(File.read(File.dirname(__FILE__) + '/json_responses/finding/get_search_keywords_recommendation_happy.json'))
+ @json_sad = JSON.parse(File.read(File.dirname(__FILE__) + '/json_responses/finding/get_search_keywords_recommendation_sad.json'))
+ @finder = Finding.new(APP_ID)
+ end
+
+ it "should transform the happy json" do
+ happy = @finder.send :transform_json_response, @json_happy
+ happy.should eq({:getSearchKeywordsRecommendationResponse => {:ack => "Success", :version => "1.5.0",
+ :timestamp => "2010-08-13T21:11:02.539Z", :keywords => "accordion"}})
+ end
+
+ it "should transform the sad json" do
+ sad = @finder.send :transform_json_response, @json_sad
+ sad.should eq({:getSearchKeywordsRecommendationResponse =>
+ {:ack => "Warning",
+ :errorMessage => {:error => {:errorId => "59", :domain => "Marketplace", :severity => "Warning",
+ :category => "Request", :message => "No recommendation was identified for the submitted keywords.",
+ :subdomain => "Search"}},
+ :version => "1.5.0",
+ :timestamp => "2010-08-13T21:08:30.081Z",
+ :keywords => ""}})
+ end
+ end
+
context "when calling get_version" do
before(:each) do
@finder = Finding.new(APP_ID)
Please sign in to comment.
Something went wrong with that request. Please try again.