require "ebaytr/version" # require 'yaml' require "net/http" require "uri" module Ebaytr # CONFIG_FILE_PATH = %w(ebay_client.yml) # yml = YAML.load_file(Rails.root.join(*CONFIG_FILE_PATH)) # yml = YAML::load(IO.read(path_to_yaml_file)) mattr_accessor :token, :app_name, :dev_name, :cert_name, :env, :url, :api, :site, :global_id @@token = '' @@app_name = '' @@dev_name = '' @@cert_name = '' @@env = "development" @@url = "https://api.sandbox.ebay.com/ws/api.dll" @@api = "967" @@site = "US" @@global_id = "EBAY-US" def self.trading(request_name, hash={}) if @@env=='production' @@url = "https://api.ebay.com/ws/api.dll" else @@url = "https://api.sandbox.ebay.com/ws/api.dll" end url = URI.parse(@@url) https = Net::HTTP.new(url.host,url.port) https.use_ssl = true req = Net::HTTP::Post.new(url.path) site_id = site_map.select{|x| x[:site_code]==@@site }[0][:site_id] req.add_field("X-EBAY-API-SITEID", site_id) req.add_field("X-EBAY-API-COMPATIBILITY-LEVEL", @@api) req.add_field("X-EBAY-API-CALL-NAME", request_name) req.add_field("X-EBAY-API-APP-NAME", @@app_name) req.add_field("X-EBAY-API-DEV-NAME", @@dev_name) req.add_field("X-EBAY-API-CERT-NAME", @@cert_name) token_hash = { RequesterCredentials: { eBayAuthToken: @@token } } main_hash = { ErrorLanguage: "en_US", WarningLevel: "High" }.merge(hash) main_hash = main_hash.merge(token_hash) if @@token.present? # body = main_hash.to_xml(root: 'ReplaceRoot', skip_instruct: true).gsub('','').gsub('','') body = Gyoku.xml(main_hash, { key_converter: :camelcase }) req.body = <<-XML <#{request_name}Request xmlns="urn:ebay:apis:eBLBaseComponents"> #{body} XML req.body = req.body.gsub("'","'") # puts req.body main_str = '' main_str += " #{hash[:ItemID]}" if hash[:ItemID].present? main_str += " #{hash[:SKU]}" if hash[:SKU].present? if hash[:Item].present? main_str += " #{hash[:Item][:SKU]}" if hash[:Item][:SKU].present? end if main_str.blank? main_str = hash.to_s[0,100] end puts " " puts "╒=======================================" puts "│ [#{@@env}] EbayAPI Trading #{Time.now.to_s(:db)}" puts "│ [#{@@site}][#{request_name}]#{main_str}" puts "└---------------------------------------" res = https.request(req) object_hash = Hash.from_xml(res.body)["#{request_name}Response"] object_hash end def self.finding(request_name,hash = {}) api_url = "http://svcs.ebay.com/services/search/FindingService/v1" url = URI.parse(api_url) https = Net::HTTP.new(url.host,url.port) # https.use_ssl = true req = Net::HTTP::Post.new(url.path) req.add_field("X-EBAY-SOA-SECURITY-APPNAME", @@app_name) req.add_field("X-EBAY-SOA-OPERATION-NAME", request_name) req.add_field("X-EBAY-SOA-GLOBAL-ID", @@global_id) body = Gyoku.xml(hash) req.body = <<-XML <#{request_name}Request xmlns="http://www.ebay.com/marketplace/search/v1/services"> #{body} XML req.body = req.body.gsub("'","'") # puts req.body main_str = '' main_str += " #{hash[:itemID]}" if hash[:itemID].present? main_str += " #{hash[:SKU]}" if hash[:SKU].present? if hash[:item].present? main_str += " #{hash[:item][:SKU]}" if hash[:item][:SKU].present? end if main_str.blank? main_str = hash.to_s[0,100] end puts " " puts "╒========================================" puts "│ EbayAPI Finding #{Time.now.to_s(:db)}" puts "│ [#{@@global_id}][#{request_name}]#{main_str}" puts "└---------------------------------------" res = https.request(req) object_hash = Hash.from_xml(res.body)["#{request_name}Response"] object_hash end def self.site_map # http://developer.ebay.com/devzone/xml/docs/reference/ebay/types/sitecodetype.html [ { site_id: 15, site_code: 'Australia' }, { site_id: 16, site_code: 'Austria' }, { site_id: 123, site_code: 'Belgium_Dutch' }, { site_id: 23, site_code: 'Belgium_French' }, { site_id: 2, site_code: 'Canada' }, { site_id: 210, site_code: 'CanadaFrench' }, { site_id: 100, site_code: 'eBayMotors' }, { site_id: 71, site_code: 'France' }, { site_id: 77, site_code: 'Germany' }, { site_id: 201, site_code: 'HongKong' }, { site_id: 203, site_code: 'India' }, { site_id: 205, site_code: 'Ireland' }, { site_id: 101, site_code: 'Italy' }, { site_id: 207, site_code: 'Malaysia' }, { site_id: 146, site_code: 'Netherlands' }, { site_id: 211, site_code: 'Philippines' }, { site_id: 212, site_code: 'Poland' }, { site_id: 215, site_code: 'Russia' }, { site_id: 216, site_code: 'Singapore' }, { site_id: 186, site_code: 'Spain' }, { site_id: 193, site_code: 'Switzerland' }, { site_id: 3, site_code: 'UK' }, { site_id: 0, site_code: 'US' }, ] end end