ebaytr.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. object_hash = Hash.from_xml(res.body)["#{request_name}Response"]
  77. object_hash
  78. end
  79. def self.finding(request_name,hash = {})
  80. api_url = "http://svcs.ebay.com/services/search/FindingService/v1"
  81. url = URI.parse(api_url)
  82. https = Net::HTTP.new(url.host,url.port)
  83. # https.use_ssl = true
  84. req = Net::HTTP::Post.new(url.path)
  85. req.add_field("X-EBAY-SOA-SECURITY-APPNAME", @@app_name)
  86. req.add_field("X-EBAY-SOA-OPERATION-NAME", request_name)
  87. req.add_field("X-EBAY-SOA-GLOBAL-ID", @@global_id)
  88. body = Gyoku.xml(hash)
  89. req.body = <<-XML
  90. <#{request_name}Request xmlns="http://www.ebay.com/marketplace/search/v1/services">
  91. #{body}
  92. </#{request_name}Request>
  93. XML
  94. req.body = req.body.gsub("&#39;","'")
  95. # puts req.body
  96. main_str = ''
  97. main_str += " #{hash[:itemID]}" if hash[:itemID].present?
  98. main_str += " #{hash[:SKU]}" if hash[:SKU].present?
  99. if hash[:item].present?
  100. main_str += " #{hash[:item][:SKU]}" if hash[:item][:SKU].present?
  101. end
  102. if main_str.blank?
  103. main_str = hash.to_s[0,100]
  104. end
  105. puts " "
  106. puts "╒========================================"
  107. puts "│ EbayAPI Finding #{Time.now.to_s(:db)}"
  108. puts "│ [#{@@global_id}][#{request_name}]#{main_str}"
  109. puts "└---------------------------------------"
  110. res = https.request(req)
  111. object_hash = Hash.from_xml(res.body)["#{request_name}Response"]
  112. object_hash
  113. end
  114. def self.site_map
  115. # http://developer.ebay.com/devzone/xml/docs/reference/ebay/types/sitecodetype.html
  116. [
  117. { site_id: 15, site_code: 'Australia' },
  118. { site_id: 16, site_code: 'Austria' },
  119. { site_id: 123, site_code: 'Belgium_Dutch' },
  120. { site_id: 23, site_code: 'Belgium_French' },
  121. { site_id: 2, site_code: 'Canada' },
  122. { site_id: 210, site_code: 'CanadaFrench' },
  123. { site_id: 100, site_code: 'eBayMotors' },
  124. { site_id: 71, site_code: 'France' },
  125. { site_id: 77, site_code: 'Germany' },
  126. { site_id: 201, site_code: 'HongKong' },
  127. { site_id: 203, site_code: 'India' },
  128. { site_id: 205, site_code: 'Ireland' },
  129. { site_id: 101, site_code: 'Italy' },
  130. { site_id: 207, site_code: 'Malaysia' },
  131. { site_id: 146, site_code: 'Netherlands' },
  132. { site_id: 211, site_code: 'Philippines' },
  133. { site_id: 212, site_code: 'Poland' },
  134. { site_id: 215, site_code: 'Russia' },
  135. { site_id: 216, site_code: 'Singapore' },
  136. { site_id: 186, site_code: 'Spain' },
  137. { site_id: 193, site_code: 'Switzerland' },
  138. { site_id: 3, site_code: 'UK' },
  139. { site_id: 0, site_code: 'US' },
  140. ]
  141. end
  142. end