ebaytr.rb 2.3 KB

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