require "ebaytr/version"
# require 'yaml'
require "net/http"
require "uri"
module Ebaytr
# CONFIG_FILE_PATH = %w(ebay_client.yml)
# yml = YAML.load_file(Rails.root.join(*CONFIG_FILE_PATH))
# yml = YAML::load(IO.read(path_to_yaml_file))
mattr_accessor :token,
:app_name, :dev_name, :cert_name,
:env, :url, :api, :site, :global_id
@@token = ''
@@app_name = ''
@@dev_name = ''
@@cert_name = ''
@@env = "development"
@@url = "https://api.sandbox.ebay.com/ws/api.dll"
@@api = "967"
@@site = "US"
@@global_id = "EBAY-US"
def self.trading(request_name, hash={})
if @@env=='production'
@@url = "https://api.ebay.com/ws/api.dll"
else
@@url = "https://api.sandbox.ebay.com/ws/api.dll"
end
url = URI.parse(@@url)
https = Net::HTTP.new(url.host,url.port)
https.open_timeout = 60
https.read_timeout = 60
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
https.use_ssl = true
req = Net::HTTP::Post.new(url.path)
#取得站點ID
fetch_site = site_map.select{|x| x[:site_code]==@@site }
puts "查無此站點: #{@@site}" if fetch_site.size==0
site_id = fetch_site[0][:site_id]
req.add_field("X-EBAY-API-SITEID", site_id)
req.add_field("X-EBAY-API-COMPATIBILITY-LEVEL", @@api)
req.add_field("X-EBAY-API-CALL-NAME", request_name)
req.add_field("X-EBAY-API-APP-NAME", @@app_name)
req.add_field("X-EBAY-API-DEV-NAME", @@dev_name)
req.add_field("X-EBAY-API-CERT-NAME", @@cert_name)
token_hash = {
RequesterCredentials: {
"eBayAuthToken" => @@token
}
}
show_request_xml = 1 if hash[:show_request_xml].present?
show_response_xml = 1 if hash[:show_response_xml].present?
show_request_json = 1 if hash[:show_request_json].present?
show_response_json = 1 if hash[:show_response_json].present?
hash.except!(:show_request_xml, :show_response_xml, :show_request_json, :show_response_json)
main_hash = {
ErrorLanguage: "en_US",
WarningLevel: "High"
}.merge(hash)
main_hash = main_hash.merge(token_hash) if @@token.present?
# body = main_hash.to_xml(root: 'ReplaceRoot', skip_instruct: true).gsub('','').gsub('','')
body = Gyoku.xml(main_hash, { key_converter: :camelcase })
req.body = <<-XML
<#{request_name}Request xmlns="urn:ebay:apis:eBLBaseComponents">
#{body}
#{request_name}Request>
XML
req.body = req.body.gsub("'","'")
# puts req.body
main_str = ''
main_str += " #{hash[:ItemID]}" if hash[:ItemID].present?
main_str += " #{hash[:SKU]}" if hash[:SKU].present?
if hash[:Item].present?
main_str += " #{hash[:Item][:SKU]}" if hash[:Item][:SKU].present?
end
if main_str.blank?
main_str = hash.to_s[0,100]
end
puts " "
puts "╒======================================="
puts "│ [#{@@env}] EbayAPI Trading #{Time.now.to_s(:db)}"
puts "│ [#{@@site}][#{request_name}]#{main_str}"
puts "└---------------------------------------"
# 若有丟 show_request_xml 參數,就顯示 xml
puts req.body if show_request_xml.present?
# 若有丟 show_request_json 參數,就顯示 json
puts main_hash if show_request_json.present?
res = https.request(req)
# 特例 (attr 解析)
if request_name==:GetOrders
if res.body.index('151.96"
tmp_str = res.body[res.body.index('').to_i-1]
if tmp_str.present?
#e.g. "USD"
attr_currency = tmp_str.split('"').first
res.body = res.body.insert(res.body.index('#{attr_currency}")
end
end
end
object_hash = Hash.from_xml(res.body)["#{request_name}Response"]
# 若有丟 show_response_xml 參數,就顯示 xml
puts res.body if show_response_xml.present?
# 若有丟 show_response_json 參數,就顯示 json
puts object_hash if show_response_json.present?
object_hash
end
def self.finding(request_name,hash = {})
api_url = "https://svcs.ebay.com/services/search/FindingService/v1"
url = URI.parse(api_url)
https = Net::HTTP.new(url.host,url.port)
https.use_ssl = true
req = Net::HTTP::Post.new(url.path)
req.add_field("X-EBAY-SOA-SECURITY-APPNAME", @@app_name)
req.add_field("X-EBAY-SOA-OPERATION-NAME", request_name)
req.add_field("X-EBAY-SOA-GLOBAL-ID", @@global_id)
body = Gyoku.xml(hash)
req.body = <<-XML
<#{request_name}Request xmlns="http://www.ebay.com/marketplace/search/v1/services">
#{body}
#{request_name}Request>
XML
req.body = req.body.gsub("'","'")
# puts req.body
main_str = ''
main_str += " #{hash[:itemID]}" if hash[:itemID].present?
main_str += " #{hash[:SKU]}" if hash[:SKU].present?
if hash[:item].present?
main_str += " #{hash[:item][:SKU]}" if hash[:item][:SKU].present?
end
if main_str.blank?
main_str = hash.to_s[0,100]
end
puts " "
puts "╒========================================"
puts "│ EbayAPI Finding #{Time.now.to_s(:db)}"
puts "│ [#{@@global_id}][#{request_name}]#{main_str}"
puts "└---------------------------------------"
res = https.request(req)
object_hash = Hash.from_xml(res.body)["#{request_name}Response"]
object_hash
end
def self.site_map
# http://developer.ebay.com/devzone/xml/docs/reference/ebay/types/sitecodetype.html
[
{ site_id: 15, site_code: 'Australia' },
{ site_id: 16, site_code: 'Austria' },
{ site_id: 123, site_code: 'Belgium_Dutch' },
{ site_id: 23, site_code: 'Belgium_French' },
{ site_id: 2, site_code: 'Canada' },
{ site_id: 210, site_code: 'CanadaFrench' },
{ site_id: 100, site_code: 'eBayMotors' },
{ site_id: 71, site_code: 'France' },
{ site_id: 77, site_code: 'Germany' },
{ site_id: 201, site_code: 'HongKong' },
{ site_id: 203, site_code: 'India' },
{ site_id: 205, site_code: 'Ireland' },
{ site_id: 101, site_code: 'Italy' },
{ site_id: 207, site_code: 'Malaysia' },
{ site_id: 146, site_code: 'Netherlands' },
{ site_id: 211, site_code: 'Philippines' },
{ site_id: 212, site_code: 'Poland' },
{ site_id: 215, site_code: 'Russia' },
{ site_id: 216, site_code: 'Singapore' },
{ site_id: 186, site_code: 'Spain' },
{ site_id: 193, site_code: 'Switzerland' },
{ site_id: 3, site_code: 'UK' },
{ site_id: 0, site_code: 'US' },
]
end
end