ebaytr.rb 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. require "ebaytr/version"
  2. # require 'yaml'
  3. require "net/http"
  4. require "uri"
  5. module Ebaytr
  6. # CONFIG_FILE_PATH = %w(ebay_client.yml)
  7. # yml = YAML.load_file(Rails.root.join(*CONFIG_FILE_PATH))
  8. # yml = YAML::load(IO.read(path_to_yaml_file))
  9. mattr_accessor :token,
  10. :app_name, :dev_name, :cert_name,
  11. :env, :url, :api, :site, :global_id
  12. @@token = ''
  13. @@app_name = ''
  14. @@dev_name = ''
  15. @@cert_name = ''
  16. @@env = "development"
  17. @@url = "https://api.sandbox.ebay.com/ws/api.dll"
  18. @@api = "967"
  19. @@site = "US"
  20. @@global_id = "EBAY-US"
  21. def self.trading(request_name, hash={})
  22. if @@env=='production'
  23. @@url = "https://api.ebay.com/ws/api.dll"
  24. else
  25. @@url = "https://api.sandbox.ebay.com/ws/api.dll"
  26. end
  27. url = URI.parse(@@url)
  28. https = Net::HTTP.new(url.host,url.port)
  29. https.open_timeout = 60
  30. https.read_timeout = 60
  31. https.verify_mode = OpenSSL::SSL::VERIFY_PEER
  32. https.use_ssl = true
  33. req = Net::HTTP::Post.new(url.path)
  34. #取得站點ID
  35. fetch_site = site_map.select{|x| x[:site_code]==@@site }
  36. puts "查無此站點: #{@@site}" if fetch_site.size==0
  37. site_id = fetch_site[0][:site_id]
  38. req.add_field("X-EBAY-API-SITEID", site_id)
  39. req.add_field("X-EBAY-API-COMPATIBILITY-LEVEL", @@api)
  40. req.add_field("X-EBAY-API-CALL-NAME", request_name)
  41. req.add_field("X-EBAY-API-APP-NAME", @@app_name)
  42. req.add_field("X-EBAY-API-DEV-NAME", @@dev_name)
  43. req.add_field("X-EBAY-API-CERT-NAME", @@cert_name)
  44. token_hash = {
  45. RequesterCredentials: {
  46. "eBayAuthToken" => @@token
  47. }
  48. }
  49. show_request_xml = 1 if hash[:show_request_xml].present?
  50. show_response_xml = 1 if hash[:show_response_xml].present?
  51. show_request_json = 1 if hash[:show_request_json].present?
  52. show_response_json = 1 if hash[:show_response_json].present?
  53. hash.except!(:show_request_xml, :show_response_xml, :show_request_json, :show_response_json)
  54. main_hash = {
  55. ErrorLanguage: "en_US",
  56. WarningLevel: "High"
  57. }.merge(hash)
  58. main_hash = main_hash.merge(token_hash) if @@token.present?
  59. # body = main_hash.to_xml(root: 'ReplaceRoot', skip_instruct: true).gsub('<ReplaceRoot>','').gsub('</ReplaceRoot>','')
  60. body = Gyoku.xml(main_hash, { key_converter: :camelcase })
  61. req.body = <<-XML
  62. <?xml version="1.0" encoding="utf-8"?>
  63. <#{request_name}Request xmlns="urn:ebay:apis:eBLBaseComponents">
  64. #{body}
  65. </#{request_name}Request>
  66. XML
  67. req.body = req.body.gsub("&#39;","'")
  68. # puts req.body
  69. main_str = ''
  70. main_str += " #{hash[:ItemID]}" if hash[:ItemID].present?
  71. main_str += " #{hash[:SKU]}" if hash[:SKU].present?
  72. if hash[:Item].present?
  73. main_str += " #{hash[:Item][:SKU]}" if hash[:Item][:SKU].present?
  74. end
  75. if main_str.blank?
  76. main_str = hash.to_s[0,100]
  77. end
  78. puts " "
  79. puts "╒======================================="
  80. puts "│ [#{@@env}] EbayAPI Trading #{Time.now.to_s(:db)}"
  81. puts "│ [#{@@site}][#{request_name}]#{main_str}"
  82. puts "└---------------------------------------"
  83. # 若有丟 show_request_xml 參數,就顯示 xml
  84. puts req.body if show_request_xml.present?
  85. # 若有丟 show_request_json 參數,就顯示 json
  86. puts main_hash if show_request_json.present?
  87. res = https.request(req)
  88. # 特例 (attr 解析)
  89. if request_name==:GetOrders
  90. if res.body.index('<Subtotal currencyID').present?
  91. #e.g. "USD\">151.96"
  92. tmp_str = res.body[res.body.index('<Subtotal currencyID').to_i+22..res.body.index('</Subtotal>').to_i-1]
  93. if tmp_str.present?
  94. #e.g. "USD"
  95. attr_currency = tmp_str.split('"').first
  96. res.body = res.body.insert(res.body.index('<Subtotal currencyID').to_i, "<SubtotalAttrCurrencyID>#{attr_currency}</SubtotalAttrCurrencyID>")
  97. end
  98. end
  99. end
  100. object_hash = Hash.from_xml(res.body)["#{request_name}Response"]
  101. # 若有丟 show_response_xml 參數,就顯示 xml
  102. puts res.body if show_response_xml.present?
  103. # 若有丟 show_response_json 參數,就顯示 json
  104. puts object_hash if show_response_json.present?
  105. object_hash
  106. end
  107. def self.finding(request_name,hash = {})
  108. api_url = "https://svcs.ebay.com/services/search/FindingService/v1"
  109. url = URI.parse(api_url)
  110. https = Net::HTTP.new(url.host,url.port)
  111. https.use_ssl = true
  112. req = Net::HTTP::Post.new(url.path)
  113. req.add_field("X-EBAY-SOA-SECURITY-APPNAME", @@app_name)
  114. req.add_field("X-EBAY-SOA-OPERATION-NAME", request_name)
  115. req.add_field("X-EBAY-SOA-GLOBAL-ID", @@global_id)
  116. body = Gyoku.xml(hash)
  117. req.body = <<-XML
  118. <#{request_name}Request xmlns="http://www.ebay.com/marketplace/search/v1/services">
  119. #{body}
  120. </#{request_name}Request>
  121. XML
  122. req.body = req.body.gsub("&#39;","'")
  123. # puts req.body
  124. main_str = ''
  125. main_str += " #{hash[:itemID]}" if hash[:itemID].present?
  126. main_str += " #{hash[:SKU]}" if hash[:SKU].present?
  127. if hash[:item].present?
  128. main_str += " #{hash[:item][:SKU]}" if hash[:item][:SKU].present?
  129. end
  130. if main_str.blank?
  131. main_str = hash.to_s[0,100]
  132. end
  133. puts " "
  134. puts "╒========================================"
  135. puts "│ EbayAPI Finding #{Time.now.to_s(:db)}"
  136. puts "│ [#{@@global_id}][#{request_name}]#{main_str}"
  137. puts "└---------------------------------------"
  138. res = https.request(req)
  139. object_hash = Hash.from_xml(res.body)["#{request_name}Response"]
  140. object_hash
  141. end
  142. def self.site_map
  143. # http://developer.ebay.com/devzone/xml/docs/reference/ebay/types/sitecodetype.html
  144. [
  145. { site_id: 15, site_code: 'Australia' },
  146. { site_id: 16, site_code: 'Austria' },
  147. { site_id: 123, site_code: 'Belgium_Dutch' },
  148. { site_id: 23, site_code: 'Belgium_French' },
  149. { site_id: 2, site_code: 'Canada' },
  150. { site_id: 210, site_code: 'CanadaFrench' },
  151. { site_id: 100, site_code: 'eBayMotors' },
  152. { site_id: 71, site_code: 'France' },
  153. { site_id: 77, site_code: 'Germany' },
  154. { site_id: 201, site_code: 'HongKong' },
  155. { site_id: 203, site_code: 'India' },
  156. { site_id: 205, site_code: 'Ireland' },
  157. { site_id: 101, site_code: 'Italy' },
  158. { site_id: 207, site_code: 'Malaysia' },
  159. { site_id: 146, site_code: 'Netherlands' },
  160. { site_id: 211, site_code: 'Philippines' },
  161. { site_id: 212, site_code: 'Poland' },
  162. { site_id: 215, site_code: 'Russia' },
  163. { site_id: 216, site_code: 'Singapore' },
  164. { site_id: 186, site_code: 'Spain' },
  165. { site_id: 193, site_code: 'Switzerland' },
  166. { site_id: 3, site_code: 'UK' },
  167. { site_id: 0, site_code: 'US' },
  168. ]
  169. end
  170. end