ebaytr.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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, :url, :api, :site, :app_name
  10. @@token = ''#yml[Rails.env]["token"]
  11. @@app_name = ''#yml[Rails.env]["appid"]
  12. @@url = "https://api.sandbox.ebay.com/ws/api.dll"
  13. @@api = "967"
  14. @@site = "0"
  15. @@global_id = "EBAY-US"
  16. def self.trading(request_name,hash = {})
  17. url = URI.parse(@@url)
  18. https = Net::HTTP.new(url.host,url.port)
  19. https.use_ssl = true
  20. req = Net::HTTP::Post.new(url.path)
  21. req.add_field("X-EBAY-API-SITEID", @@site)
  22. req.add_field("X-EBAY-API-COMPATIBILITY-LEVEL", @@api)
  23. req.add_field("X-EBAY-API-CALL-NAME", request_name)
  24. main_hash = {
  25. RequesterCredentials: {
  26. eBayAuthToken: @@token
  27. },
  28. ErrorLanguage: "en_US",
  29. WarningLevel: "High"
  30. }.merge(hash)
  31. # body = main_hash.to_xml(root: 'ReplaceRoot', skip_instruct: true).gsub('<ReplaceRoot>','').gsub('</ReplaceRoot>','')
  32. body = Gyoku.xml(main_hash, { key_converter: :camelcase })
  33. req.body = <<-XML
  34. <?xml version="1.0" encoding="utf-8"?>
  35. <#{request_name}Request xmlns="urn:ebay:apis:eBLBaseComponents">
  36. #{body}
  37. </#{request_name}Request>
  38. XML
  39. req.body = req.body.gsub("&#39;","'")
  40. puts req.body
  41. res = https.request(req)
  42. object_hash = Hash.from_xml(res.body)["#{request_name}Response"]
  43. object_hash
  44. end
  45. def self.finding(request_name,hash = {})
  46. api_url = "http://svcs.ebay.com/services/search/FindingService/v1"
  47. url = URI.parse(api_url)
  48. https = Net::HTTP.new(url.host,url.port)
  49. # https.use_ssl = true
  50. req = Net::HTTP::Post.new(url.path)
  51. req.add_field("X-EBAY-SOA-SECURITY-APPNAME", @@app_name)
  52. req.add_field("X-EBAY-SOA-OPERATION-NAME", request_name)
  53. req.add_field("X-EBAY-SOA-GLOBAL-ID", @@global_id)
  54. body = Gyoku.xml(hash)
  55. req.body = <<-XML
  56. <#{request_name}Request xmlns="http://www.ebay.com/marketplace/search/v1/services">
  57. #{body}
  58. </#{request_name}Request>
  59. XML
  60. req.body = req.body.gsub("&#39;","'")
  61. puts req.body
  62. res = https.request(req)
  63. object_hash = Hash.from_xml(res.body)["#{request_name}Response"]
  64. object_hash
  65. end
  66. end