ebaytr.rb 4.8 KB

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