Module: Amazon::MWS::Connection::Management::ClassMethods

Defined in:
lib/amazon/mws/connection/management.rb

Overview

Manage the creation and destruction of connections for Amazon::MWS::Base and its subclasses. Connections are created with establish_connection!.

Instance Method Summary collapse

Instance Method Details

#connected?Boolean

Returns true if a connection has been made yet.

Returns:



60
61
62
# File 'lib/amazon/mws/connection/management.rb', line 60

def connected?
  !connections.empty?
end

#connectionObject

Returns the connection for the current class, or Base’s default connection if the current class does not have its own connection.

If not connection has been established yet, NoConnectionEstablished will be raised.



51
52
53
54
55
56
57
# File 'lib/amazon/mws/connection/management.rb', line 51

def connection
  if connected?
    connections[connection_name] || default_connection
  else
    raise Amazon::MWS::NoConnectionEstablished.new
  end
end

#disconnect(name = connection_name) ⇒ Object

Removes the connection for the current class. If there is no connection for the current class, the default connection will be removed.



66
67
68
69
70
71
# File 'lib/amazon/mws/connection/management.rb', line 66

def disconnect(name = connection_name)
  name       = default_connection unless connections.has_key?(name)
  connection = connections[name]
  connection.http.finish if connection.persistent?
  connections.delete(name)
end

#disconnect!Object

Clears all connections, from all classes, with prejudice.



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

def disconnect!
  connections.each_key {|connection| disconnect(connection)}
end

#establish_connection!(options = {}) ⇒ Object

Creates a new connection with which to make requests to the S3 servers for the calling class.

Amazon::MWS::Base.establish_connection!(
  :access_key_id     => '...', 
  :secret_access_key => '...',
  :merchant_id       => '...',
  :marketplace_id    => '...'
)

Required arguments

  • :access_key_id - The access key id for your S3 account. Provided by Amazon.

  • :secret_access_key - The secret access key for your S3 account. Provided by Amazon.

  • :merchant_id

  • :marketplace_id

If any of these required arguments is missing, a MissingAccessKey exception will be raised.

Optional arguments

  • :server - The server to make requests to. You can use this to specify your bucket in the subdomain,

or your own domain’s cname if you are using virtual hosted buckets. Defaults to mws.amazonaws.com. will be implicitly set to 443, unless specified otherwise. Defaults to false.

  • :persistent - Whether to use a persistent connection to the server. Having this on provides around a two fold

performance increase but for long running processes some firewalls may find the long lived connection suspicious and close the connection. If you run into connection errors, try setting :persistent to false. Defaults to false.



40
41
42
43
44
45
# File 'lib/amazon/mws/connection/management.rb', line 40

def establish_connection!(options = {})
  # After you've already established the default connection, just specify 
  # the difference for subsequent connections
  #options = default_connection.options.merge(options) if connected?
  connections[connection_name] = Amazon::MWS::Connection.connect(options)
end