Class: Amazon::MWS::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon/mws/connection.rb,
lib/amazon/mws/connection/request_builder.rb

Defined Under Namespace

Classes: RequestBuilder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Connection

Returns a new instance of Connection.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/amazon/mws/connection.rb', line 15

def initialize(params = {})
  # These values are essential to establishing a connection
  @locale = (params['locale'] || "us").downcase.to_sym
  @server            = HOSTS[locale]
  #@persistent        = params['persistent'] || false
  # These values are essential to signing requests
  @access_key        = params['access_key']
  @secret_access_key = params['secret_access_key']
  @merchant_id       = params['merchant_id']
  @marketplace_id    = params['marketplace_id']
  @path    		   = '/'

  raise MissingConnectionOptions if [@access_key, @secret_access_key, @merchant_id, @marketplace_id].any? {|option| option.nil?}

  @http = connect
end

Instance Attribute Details

#access_keyObject (readonly)

Returns the value of attribute access_key.



6
7
8
# File 'lib/amazon/mws/connection.rb', line 6

def access_key
  @access_key
end

#localeObject (readonly)

Returns the value of attribute locale.



6
7
8
# File 'lib/amazon/mws/connection.rb', line 6

def locale
  @locale
end

#marketplace_idObject (readonly)

Returns the value of attribute marketplace_id.



6
7
8
# File 'lib/amazon/mws/connection.rb', line 6

def marketplace_id
  @marketplace_id
end

#merchant_idObject (readonly)

Returns the value of attribute merchant_id.



6
7
8
# File 'lib/amazon/mws/connection.rb', line 6

def merchant_id
  @merchant_id
end

#serverObject (readonly)

Returns the value of attribute server.



6
7
8
# File 'lib/amazon/mws/connection.rb', line 6

def server
  @server
end

Class Method Details

.connect(options = {}) ⇒ Object



10
11
12
# File 'lib/amazon/mws/connection.rb', line 10

def connect(options = {})
  new(options)
end

Instance Method Details

#authenticate_query_string(verb, query_params = {}) ⇒ Object

Generates the authentication query string used by Amazon. Takes the http method and the query string of the request and returns the authenticated query string



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/amazon/mws/connection.rb', line 80

def authenticate_query_string(verb, query_params = {})
  Authentication::QueryString.new(
    :verb              => verb,
    :query_params      => query_params,
    :access_key        => @access_key,
    :secret_access_key => @secret_access_key,
    :merchant_id       => @merchant_id,
    :marketplace_id    => @marketplace_id,
    :server            => @server,
    :path              => @path
  )
end

#build_request(verb, path, body = nil) ⇒ Object

Builds up a Net::HTTP request object



94
95
96
97
98
99
100
101
# File 'lib/amazon/mws/connection.rb', line 94

def build_request(verb, path, body = nil)
  builder = RequestBuilder.new(verb, path, body)
  builder.add_user_agent
  builder.add_content_type
  builder.add_content_md5(body) unless body.nil?

  return builder.request
end

#connectObject

Create the Net::HTTP object to use for this connection



33
34
35
36
37
38
# File 'lib/amazon/mws/connection.rb', line 33

def connect
  http             = Net::HTTP.new(@server, 443)
  http.use_ssl     = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  return http
end

#prepare_path(verb, path, query_params) ⇒ Object

Create the signed authentication query string. Add this query string to the path WITHOUT prepending the server address.



73
74
75
76
# File 'lib/amazon/mws/connection.rb', line 73

def prepare_path(verb, path, query_params)
  query_string = authenticate_query_string(verb, query_params)
  return "#{path}?#{query_string}"
end

#request(verb, path, query_params, body = nil, attempts = 0, &block) ⇒ Object

Make the request, based on the appropriate request object Called from Amazon::MWS::Base



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/amazon/mws/connection.rb', line 42

def request(verb, path, query_params, body = nil, attempts = 0, &block)
  @path = path
  # presumably this is for files
  body.rewind if body.respond_to?(:rewind) unless attempts.zero?
  # Prepare the Proc to be called by Net::HTTP
  proc = requester(verb, path, query_params, body)

  #if @persistent
  #  @http.start unless @http.started?
  #  proc.call
  #else
    @http.start(&proc)
  #end
rescue Errno::EPIPE, Timeout::Error, Errno::EINVAL, EOFError
  @http = connect
  attempts == 3 ? raise : (attempts += 1; retry)
end

#requester(verb, path, query_params, body) ⇒ Object

A Proc used by the request method



61
62
63
64
65
66
67
68
69
# File 'lib/amazon/mws/connection.rb', line 61

def requester(verb, path, query_params, body)
  Proc.new do |http|
    path    = prepare_path(verb, path, query_params)
    puts "#{path}\n\n" if Amazon::MWS::Base.debug
    request = build_request(verb, path, body)

    @http.request(request)
  end
end