ebaytr.rb 5.1 KB

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