Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global ID Fix #14

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pkg/
.DS_Store
.rvmrc
*.sw[^f]
.idea
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.7.5
34 changes: 24 additions & 10 deletions lib/rebay/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ module Rebay
class Api
# default site is EBAY_US, for other available sites see eBay documentation:
# http://developer.ebay.com/DevZone/merchandising/docs/Concepts/SiteIDToGlobalID.html
EBAY_US = 0

class << self
attr_accessor :app_id, :default_site_id, :sandbox

def base_url
[base_url_prefix,
sandbox ? "sandbox" : nil,
base_url_suffix].compact.join('.')
end

def base_url_prefix
"http://svcs"
"https://svcs"
end

def base_url_suffix
Expand All @@ -28,20 +26,36 @@ def base_url_suffix
def sandbox
@sandbox ||= false
end

def default_site_id
@default_site_id || EBAY_US
@default_site_id ||= "EBAY-US"
end

def configure
yield self if block_given?
end
end

attr_accessor :proxy_url, :proxy_port, :proxy_username, :proxy_password, :user_agent

def initialize(proxy_url: nil, proxy_port: nil, proxy_username: nil, proxy_password: nil, user_agent: 'eBayiPhone/6.24.0')
@proxy_url = proxy_url
@proxy_port = proxy_port
@proxy_username = proxy_username
@proxy_password = proxy_password
@user_agent = user_agent
end

protected

def get_json_response(url)
Rebay::Response.new(JSON.parse(Net::HTTP.get_response(URI.parse(url)).body))

def get_json_response(url, headers: {})
uri = URI.parse(url)
response = nil
Net::HTTP.start(uri.host, 443, proxy_url, proxy_port, proxy_username, proxy_password, use_ssl: true) do |http|
request = Net::HTTP::Get.new(uri, { 'User-Agent' => user_agent }.merge(headers))
response = http.request request # Net::HTTPResponse object
end
Rebay::Response.new(JSON.parse(response.body))
end

def build_rest_payload(params)
Expand Down
53 changes: 39 additions & 14 deletions lib/rebay/finding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,33 @@ class Finding < Rebay::Api
def self.base_url_suffix
"ebay.com/services/search/FindingService/v1"
end

VERSION = '1.0.0'


#http://developer.ebay.com/DevZone/finding/CallRef/findCompletedItems.html
def find_completed_items(params)
raise ArgumentError unless params[:keywords] or params[:categoryId]
response = get_json_response(build_request_url('findCompletedItems', params))
response.trim(:findCompletedItemsResponse)

if response.response.has_key?('searchResult') && response.response['searchResult'].has_key?('item')
response.results = response.response['searchResult']['item']
end
return response
end

#http://developer.ebay.com/DevZone/finding/CallRef/findItemsAdvanced.html
def find_items_advanced(params)
raise ArgumentError unless params[:keywords] or params[:categoryId]
response = get_json_response(build_request_url('findItemsAdvanced', params))
response.trim(:findItemsAdvancedResponse)

if response.response.has_key?('searchResult') && response.response['searchResult'].has_key?('item')
response.results = response.response['searchResult']['item']
end
return response
end

#http://developer.ebay.com/DevZone/finding/CallRef/findItemsByCategory.html
def find_items_by_category(params)
raise ArgumentError unless params[:categoryId]
Expand All @@ -28,7 +40,19 @@ def find_items_by_category(params)
end
return response
end


#http://developer.ebay.com/DevZone/finding/CallRef/findItemsByImage.html
# only available for Clothing, Shoes & Accessories (parent category ID 11450 on the US site)
def find_items_by_image(params)
raise ArgumentError unless params[:itemId]
response = get_json_response(build_request_url('findItemsByImage', params))
response.trim(:findItemsByImageResponse)
if response.response.has_key?('searchResult') && response.response['searchResult'].has_key?('item')
response.results = response.response['searchResult']['item']
end
return response
end

#http://developer.ebay.com/DevZone/finding/CallRef/findItemsByKeywords.html
def find_items_by_keywords(params)
raise ArgumentError unless params[:keywords]
Expand All @@ -39,19 +63,19 @@ def find_items_by_keywords(params)
end
return response
end

#http://developer.ebay.com/DevZone/finding/CallRef/findItemsByProduct.html
def find_items_by_product(params)
raise ArgumentError unless params[:productId]
params['productId.@type'] = 'ReferenceID'
params['productId.@type'] = params[:productId_type]
response = get_json_response(build_request_url('findItemsByProduct', params))
response.trim(:findItemsByProductResponse)
if response.response.has_key?('searchResult') && response.response['searchResult'].has_key?('item')
response.results = response.response['searchResult']['item']
end
return response
end

#http://developer.ebay.com/DevZone/finding/CallRef/findItemsIneBayStores.html
def find_items_in_ebay_stores(params)
raise ArgumentError unless params[:keywords] or params[:storeName]
Expand All @@ -62,15 +86,15 @@ def find_items_in_ebay_stores(params)
end
return response
end

#http://developer.ebay.com/DevZone/finding/CallRef/getHistograms.html
def get_histograms(params)
raise ArgumentError unless params[:categoryId]
response = get_json_response(build_request_url('getHistograms', params))
response.trim(:getHistorgramsResponse)
return response
end

#http://developer.ebay.com/DevZone/finding/CallRef/getSearchKeywordsRecommendation.html
def get_search_keywords_recommendation(params)
raise ArgumentError unless params[:keywords]
Expand All @@ -81,7 +105,7 @@ def get_search_keywords_recommendation(params)
end
return response
end

#http://developer.ebay.com/DevZone/finding/CallRef/getVersion.html
def get_version
response = get_json_response(build_request_url('getVersion'))
Expand All @@ -91,10 +115,11 @@ def get_version
end
return response
end
private

private
def build_request_url(service, params=nil)
url = "#{self.class.base_url}?OPERATION-NAME=#{service}&SERVICE-VERSION=#{VERSION}&SECURITY-APPNAME=#{Rebay::Api.app_id}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD"
app_id = params.key?(:app_id) && params.delete(:app_id)
url = "#{self.class.base_url}?OPERATION-NAME=#{service}&SERVICE-VERSION=#{VERSION}&SECURITY-APPNAME=#{app_id || Rebay::Api.app_id}&X-EBAY-SOA-GLOBAL-ID=#{Rebay::Api.default_site_id}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD"
url += build_rest_payload(params)
return url
end
Expand Down
41 changes: 25 additions & 16 deletions lib/rebay/shopping.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
module Rebay
class Shopping < Rebay::Api
VERSION = '793'

attr_accessor :token

VERSION = '1199'

class << self
def base_url_prefix
"http://open.api"
end

def base_url_suffix
"ebay.com/shopping"
end
end

#http://developer.ebay.com/DevZone/shopping/docs/CallRef/FindProducts.html
def find_products(params)
raise ArgumentError unless params[:CategoryID] or params[:ProductID] or params[:QueryKeywords] or
(params[:'ProductID.Value'] && params[:'ProductID.type'])
response = get_json_response(build_request_url('FindProducts', params))
response = get_json_response(build_request_url('FindProducts', params), headers: headers)
if response.response.has_key?('Product')
response.results = response.response['Product']
end
Expand All @@ -27,7 +29,7 @@ def find_products(params)
def find_half_products(params)
raise ArgumentError unless params[:ProductID] or params[:QueryKeywords] or
(params[:'ProductID.Value'] && params[:'ProductID.type'])
response = get_json_response(build_request_url('FindHalfProducts', params))
response = get_json_response(build_request_url('FindHalfProducts', params), headers: headers)
if response.response.has_key?('Products') && response.response['Products'].has_key?('Product')
response.results = response.response['Products']['Product']
end
Expand All @@ -37,7 +39,7 @@ def find_half_products(params)
#http://developer.ebay.com/DevZone/shopping/docs/CallRef/GetSingleItem.html
def get_single_item(params)
raise ArgumentError unless params[:ItemID]
response = get_json_response(build_request_url('GetSingleItem', params))
response = get_json_response(build_request_url('GetSingleItem', params), headers: headers)
if response.response.has_key?('Item')
response.results = response.response['Item']
end
Expand All @@ -47,7 +49,7 @@ def get_single_item(params)
#http://developer.ebay.com/DevZone/shopping/docs/CallRef/GetItemStatus.html
def get_item_status(params)
raise ArgumentError unless params[:ItemID]
response = get_json_response(build_request_url('GetItemStatus', params))
response = get_json_response(build_request_url('GetItemStatus', params), headers: headers)
if response.response.has_key?('Item')
response.results = response.response['Item']
end
Expand All @@ -57,14 +59,14 @@ def get_item_status(params)
#http://developer.ebay.com/DevZone/shopping/docs/CallRef/GetShippingCosts.html
def get_shipping_costs(params)
raise ArgumentError unless params[:ItemID]
response = get_json_response(build_request_url('GetShippingCosts', params))
response = get_json_response(build_request_url('GetShippingCosts', params), headers: headers)
return response
end

#http://developer.ebay.com/DevZone/shopping/docs/CallRef/GetMultipleItems.html
def get_multiple_items(params)
raise ArgumentError unless params[:ItemID]
response = get_json_response(build_request_url('GetMultipleItems', params))
response = get_json_response(build_request_url('GetMultipleItems', params), headers: headers)
if response.response.has_key?('Item')
response.results = response.response['Item']
end
Expand All @@ -74,7 +76,7 @@ def get_multiple_items(params)
#http://developer.ebay.com/DevZone/shopping/docs/CallRef/GetUserProfile.html
def get_user_profile(params)
raise ArgumentError unless params[:UserID]
response = get_json_response(build_request_url('GetUserProfile', params))
response = get_json_response(build_request_url('GetUserProfile', params), headers: headers)
if response.response.has_key?('User')
response.results = response.response['User']
end
Expand All @@ -84,7 +86,7 @@ def get_user_profile(params)
#http://developer.ebay.com/DevZone/shopping/docs/CallRef/FindPopularSearches.html
def find_popular_searches(params)
raise ArgumentError unless params[:CategoryID]
response = get_json_response(build_request_url('FindPopularSearches', params))
response = get_json_response(build_request_url('FindPopularSearches', params), headers: headers)
if response.response.has_key?('PopularSearchResult')
response.results = response.response['PopularSearchResult']
end
Expand All @@ -94,7 +96,7 @@ def find_popular_searches(params)
#http://developer.ebay.com/DevZone/shopping/docs/CallRef/FindPopularItems.html
def find_popular_items(params={})
raise ArgumentError unless params[:CategoryID] or params[:QueryKeywords]
response = get_json_response(build_request_url('FindPopularItems', params))
response = get_json_response(build_request_url('FindPopularItems', params), headers: headers)
if response.response.has_key?('ItemArray') && response.response['ItemArray'].has_key?('Item')
response.results = response.response['ItemArray']['Item']
end
Expand All @@ -103,7 +105,7 @@ def find_popular_items(params={})

#http://developer.ebay.com/DevZone/shopping/docs/CallRef/FindReviewsandGuides.html
def find_reviews_and_guides(params={})
response = get_json_response(build_request_url('FindReviewsAndGuides', params))
response = get_json_response(build_request_url('FindReviewsAndGuides', params), headers: headers)
if response.response.has_key?('BuyingGuideDetails') && response.response['BuyingGuideDetails'].has_key?('BuyingGuide')
response.results = response.response['BuyingGuideDetails']['BuyingGuide']
end
Expand All @@ -113,7 +115,7 @@ def find_reviews_and_guides(params={})
#http://developer.ebay.com/DevZone/shopping/docs/CallRef/GetCategoryInfo.html
def get_category_info(params)
raise ArgumentError unless params[:CategoryID]
response = get_json_response(build_request_url('GetCategoryInfo', params))
response = get_json_response(build_request_url('GetCategoryInfo', params), headers: headers)
if response.response.has_key?('CategoryArray') && response.response['CategoryArray'].has_key?('Category')
response.results = response.response['CategoryArray']['Category']
end
Expand All @@ -128,9 +130,16 @@ def get_category_info_with_children(params)

private
def build_request_url(service, params=nil)
url = "#{self.class.base_url}?callname=#{service}&appid=#{Rebay::Api.app_id}&version=#{VERSION}&responseencoding=JSON"
app_id = params.key?(:app_id) && params.delete(:app_id)
url = "#{self.class.base_url}?callname=#{service}&appid=#{app_id || Rebay::Api.app_id}&X-EBAY-SOA-GLOBAL-ID=#{Rebay::Api.default_site_id}&version=#{VERSION}&responseencoding=JSON"
url += build_rest_payload({siteid: Rebay::Api.default_site_id}.merge(params))
return url
end

def headers
{
'X-EBAY-API-IAF-TOKEN' => self.token,
}
end
end
end
Loading