Class: Ebayr::Request

Inherits:
Object
  • Object
show all
Includes:
Ebayr
Defined in:
lib/ebayr/request.rb

Overview

Encapsulates a request which is sent to the eBay Trading API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ebayr

#authorization_uri, #call, included, normalize_responses?, #sandbox?, #uri, #uri_prefix

Constructor Details

#initialize(command, options = {}) ⇒ Request

Make a new call. The URI used will be that of Ebayr::uri, unless overridden here (same for auth_token, site_id and compatability_level).



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ebayr/request.rb', line 11

def initialize(command, options = {})
  @command = self.class.camelize(command.to_s)
  @uri = options.delete(:uri) || self.uri
  @uri = URI.parse(@uri) unless @uri.is_a? URI
  @auth_token = (options.delete(:auth_token) || self.auth_token).to_s
  @site_id = (options.delete(:site_id) || self.site_id).to_s
  @compatability_level = (options.delete(:compatability_level) || self.compatability_level).to_s
  @http_timeout = (options.delete(:http_timeout) || 60).to_i
  # Remaining options are converted and used as input to the call
  @input = options.delete(:input) || options
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



7
8
9
# File 'lib/ebayr/request.rb', line 7

def command
  @command
end

Class Method Details

.camelize(string) ⇒ Object

Converts a command like get_ebay_offical_time to GeteBayOfficialTime



118
119
120
121
122
# File 'lib/ebayr/request.rb', line 118

def self.camelize(string)
  string = string.to_s
  return string unless string == string.downcase
  string.split('_').map(&:capitalize).join.gsub('Ebay', 'eBay')
end

.serialize_input(input) ⇒ Object

Prepares an argument for input to an eBay Trading API XML call.

  • Times are converted to ISO 8601 format



110
111
112
113
114
115
# File 'lib/ebayr/request.rb', line 110

def self.serialize_input(input)
  case input
    when Time then input.to_time.utc.iso8601
    else input
  end
end

.xml(*args) ⇒ Object

A very, very simple XML serializer.

Ebayr.xml("Hello!")       # => "Hello!"
Ebayr.xml(:foo=>"Bar")  # => <foo>Bar</foo>
Ebayr.xml(:foo=>["Bar","Baz"])  # => <foo>Bar</foo>


98
99
100
101
102
103
104
105
106
# File 'lib/ebayr/request.rb', line 98

def self.xml(*args)
  args.map do |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) }.join
      else self.serialize_input(structure).to_s
    end
  end.join
end

Instance Method Details

#bodyObject

Gets the body of this request (which is XML)



46
47
48
49
50
51
52
53
54
# File 'lib/ebayr/request.rb', line 46

def body
  "    <?xml version=\"1.0\" encoding=\"utf-8\"?>\n    <\#{@command}Request xmlns=\"urn:ebay:apis:eBLBaseComponents\">\n      \#{requester_credentials_xml}\n      \#{input_xml}\n    </\#{@command}Request>\n  XML\nend\n"

#headersObject

Gets the headers that will be sent with this request.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ebayr/request.rb', line 33

def 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' => @command.to_s,
    'X-EBAY-API-SITEID' => @site_id.to_s,
    'Content-Type' => 'text/xml'
  }
end

#http(&block) ⇒ Object

Gets a HTTP connection for this request. If you pass in a block, it will be run on that HTTP connection.



126
127
128
129
130
131
132
133
134
# File 'lib/ebayr/request.rb', line 126

def http(&block)
  http = Net::HTTP.new(@uri.host, @uri.port)
  if @uri.port == 443
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  return http.start(&block) if block_given?
  http
end

#input_xmlObject



23
24
25
# File 'lib/ebayr/request.rb', line 23

def input_xml
  self.class.xml(@input)
end

#pathObject

Gets the path to which this request will be posted



28
29
30
# File 'lib/ebayr/request.rb', line 28

def path
  @uri.path
end

#requester_credentials_xmlObject

Returns eBay requester credential XML if @auth_token is present



57
58
59
60
61
62
63
64
65
# File 'lib/ebayr/request.rb', line 57

def requester_credentials_xml
  return "" unless @auth_token && !@auth_token.empty?

  "  <RequesterCredentials>\n    <eBayAuthToken>\#{@auth_token}</eBayAuthToken>\n  </RequesterCredentials>\n  XML\nend\n"

#sendObject

Makes a HTTP connection and sends the request, returning an Ebayr::Response



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ebayr/request.rb', line 69

def send
  http = Net::HTTP.new(@uri.host, @uri.port)
  http.read_timeout = @http_timeout

  # Output request XML if debug flag is set
  puts body if debug == true

  if @uri.port == 443
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  post = Net::HTTP::Post.new(@uri.path, headers)
  post.body = body

  response = http.start { |h| h.request(post) }

  @response = Response.new(self, response)
end

#to_sObject



89
90
91
# File 'lib/ebayr/request.rb', line 89

def to_s
  "#{@command}[#{@input}] <#{@uri}>"
end