Skip to content

Instantly share code, notes, and snippets.

@lamsan
Forked from kenmazaika/ebay.yml
Created March 12, 2012 15:41
Show Gist options
  • Save lamsan/2022773 to your computer and use it in GitHub Desktop.
Save lamsan/2022773 to your computer and use it in GitHub Desktop.
Call the eBay API from Rails using HTTParty. Works in Rails 3, since it's calling the API directly.
development:
dev_id: 00000000000000000
app_id: 00000000000000000
cert_id: 0000000000000000
uri: https://api.sandbox.ebay.com/ws/api.dll
auth_token: 00000000000000
require 'rubygems'
require 'httparty'
EBAY_CONFIG = YAML::load(File.open("config/ebay.yml"))['development']
class Ebay
include HTTParty
def self.getEbayOfficialTime
format :xml
headers(ebay_headers.merge({"X-EBAY-API-CALL-NAME" => "GeteBayOfficialTime"}))
requestXml = "<?xml version='1.0' encoding='utf-8'?>
<GeteBayOfficialTimeRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\">
<RequesterCredentials>
<eBayAuthToken>#{auth_token}</eBayAuthToken>
</RequesterCredentials>
</GeteBayOfficialTimeRequest>"
response = post(api_url, :body => requestXml)
raise "Bad Response | #{response.inspect}" if response.parsed_response['GeteBayOfficialTimeResponse']['Ack'] != 'Success'
response.parsed_response['GeteBayOfficialTimeResponse']['Timestamp']
end
private
def self.ebay_headers
{"X-EBAY-API-COMPATIBILITY-LEVEL" => "433",
"X-EBAY-API-DEV-NAME" => EBAY_CONFIG['dev_id'],
"X-EBAY-API-APP-NAME" => EBAY_CONFIG['app_id'],
"X-EBAY-API-CERT-NAME" => EBAY_CONFIG['cert_id'],
"X-EBAY-API-SITEID" => "0",
"Content-Type" => "text/xml"}
end
def self.auth_token
EBAY_CONFIG['auth_token']
end
def self.api_url
EBAY_CONFIG['uri']
end
end
puts Ebay.getEbayOfficialTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment