ebaytr.rb 6.5 KB

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