ebaytr.rb 5.7 KB

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