Module: Ebayr
- Defined in:
- lib/ebayr.rb,
lib/ebayr/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
-
.call(call, arguments = {}) ⇒ Object
Make an eBay call (symbol or string).
- .sandbox? ⇒ Boolean
-
.uri ⇒ Object
Gets the URI used for calls.
- .xml(structure) ⇒ Object
Instance Method Summary collapse
-
#ebay_call(call, arguments = {}) ⇒ Object
Shorthand for call(call, arguments.merge(:auth_token => this.ebay_token)) Allows objects which mix in this module to use their own token.
Class Method Details
.call(call, arguments = {}) ⇒ Object
Make an eBay call (symbol or string). You can pass in these arguments:
- auth_token
-
to use a user’s token instead of the general token
- site_id
-
to use a specific eBay site (default is 0, which is US ebay.com)
- compatability_level
-
declare another eBay Trading API compatability_level
All other arguments are passed into the API call, and may be nested.
Remember, case matters.
call(:GeteBayOfficialTime)
The response is a Hash of the response, deserialized from the XML by ActiveSupport’s XML deserializer.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ebayr.rb', line 66 def self.call(call, arguments = {}) call = call.to_s auth_token = arguments.delete(:auth_token) || self.auth_token.to_s site_id = arguments.delete(:site_id) || self.site_id.to_s compatability_level = arguments.delete(:compatability_level) || self.compatability_level.to_s headers = { 'X-EBAY-API-COMPATIBILITY-LEVEL' => compatability_level.to_s, 'X-EBAY-API-DEV-NAME' => dev_id.to_s, 'X-EBAY-API-APP-NAME' => app_id.to_s, 'X-EBAY-API-CERT-NAME' => cert_id.to_s, 'X-EBAY-API-CALL-NAME' => call.to_s, 'X-EBAY-API-SITEID' => site_id.to_s, 'Content-Type' => 'text/xml' } xml = xml(arguments) xml = <<-XML <?xml version="1.0" encoding="utf-8"?> <#{call}Request xmlns="urn:ebay:apis:eBLBaseComponents"> <RequesterCredentials> <eBayAuthToken>#{auth_token}</eBayAuthToken> </RequesterCredentials> #{xml} </#{call}Request> XML request = Net::HTTP::Post.new(uri.path, headers) request.body = xml.to_s http = Net::HTTP.new(uri.host, uri.port) if uri.port == 443 http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end response = http.start { |http| http.request(request) } if callbacks callbacks.each do |callback| if callback.is_a?(Symbol) send(callback, request, response) elsif callback.respond_to?(:call) callback.call(request, response) else throw Error.new("Invalid callback: #{callback.to_s}") end end end case response when Net::HTTPSuccess result = Hash.from_xml(response.body)["#{call}Response"] unless result raise Exception.new("No #{call}Response in response", request, response) end case result['Ack'] when 'Success' return result when 'Warning' @@logger.warn(result['Errors'].inspect) return result else raise Error.new(result['Errors'], request, response) end return result else raise Exception.new("Unexpected response from server", request, response) end end |
.sandbox? ⇒ Boolean
35 36 37 |
# File 'lib/ebayr.rb', line 35 def self.sandbox? !!sandbox end |
.uri ⇒ Object
Gets the URI used for calls
40 41 42 |
# File 'lib/ebayr.rb', line 40 def self.uri URI::parse("https://api#{sandbox ? ".sandbox" : ""}.ebay.com/ws/api.dll") end |
.xml(structure) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/ebayr.rb', line 44 def self.xml(structure) case structure when Hash then structure.map { |k, v| "<#{k.to_s}>#{xml(v)}</#{k.to_s}>" }.join when Array then structure.map { |v| xml(v) } else structure.to_s end end |
Instance Method Details
#ebay_call(call, arguments = {}) ⇒ Object
Shorthand for call(call, arguments.merge(:auth_token => this.ebay_token)) Allows objects which mix in this module to use their own token.
143 144 145 146 |
# File 'lib/ebayr.rb', line 143 def ebay_call(call, arguments = {}) raise "#{self} has no eBay token" unless ebay_token Ebay.call(call, arguments.merge(:auth_token => ebay_token)) end |