Class: Ingenico::Connect::SDK::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/ingenico/connect/sdk/factory.rb

Overview

Convenience class that constructs instances of several other classes in the SDK. Provides methods to construct CommunicatorConfiguration, Session, Communicator and Client instances.

Class Method Summary collapse

Class Method Details

.create_client_from_communicator(communicator) ⇒ Ingenico::Connect::SDK::Client

Creates and returns an Client that provides the a high-level interface with the GlobalCollect service. If a code block is given, the created client is returned to the code block and closed afterwards.

Examples:

Providing a code block

Factory.create_client_from_communicator(communicator) do |client|
  client.merchant(merchant_id).services.testconnection
end
# client is closed here


137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ingenico/connect/sdk/factory.rb', line 137

def self.create_client_from_communicator(communicator)
  client = Client.new(communicator)
  if block_given?
    begin
      yield client
    ensure
      client.close
    end
  else
    return client
  end
end

.create_client_from_configuration(configuration) ⇒ Ingenico::Connect::SDK::Client

Creates and returns an Client that provides the a high-level interface with the GlobalCollect service. If a code block is given, the created client is returned to the code block and closed afterwards.

Examples:

Providing a code block

Factory.create_client_from_configuration(configuration) do |client|
  client.merchant(merchant_id).services.testconnection
end
# client is closed here


112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ingenico/connect/sdk/factory.rb', line 112

def self.create_client_from_configuration(configuration)
  communicator = create_communicator_from_configuration(configuration)
  client = Client.new(communicator)
  if block_given?
    begin
      yield client
    ensure
      client.close
    end
  else
    return client
  end
end

.create_client_from_file(configuration_file_name, api_key_id, secret_api_key) ⇒ Ingenico::Connect::SDK::Client

Creates and returns an Client that provides the a high-level interface with the GlobalCollect service. If a code block is given, the created client is returned to the code block and closed afterwards.

Examples:

Providing a code block

Factory.create_client_from_file(configuration_file_name, api_key_id, secret_api_key) do |client|
  client.merchant(merchant_id).services.testconnection
end
# client is closed here


188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ingenico/connect/sdk/factory.rb', line 188

def self.create_client_from_file(configuration_file_name, api_key_id, secret_api_key)
  communicator = create_communicator_from_file(configuration_file_name, api_key_id, secret_api_key)
  client = Client.new(communicator)
  if block_given?
    begin
      yield client
    ensure
      client.close
    end
  else
    return client
  end
end

.create_client_from_session(session) ⇒ Ingenico::Connect::SDK::Client

Creates and returns an Client that provides the a high-level interface with the GlobalCollect service. If a code block is given, the created client is returned to the code block and closed afterwards.

Examples:

Providing a code block

Factory.create_client_from_session(session) do |client|
  client.merchant(merchant_id).services.testconnection
end
# client is closed here


161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ingenico/connect/sdk/factory.rb', line 161

def self.create_client_from_session(session)
  communicator = create_communicator_from_session(session)
  client = Client.new(communicator)
  if block_given?
    begin
      yield client
    ensure
      client.close
    end
  else
    return client
  end
end

.create_communicator_from_configuration(configuration) ⇒ Ingenico::Connect::SDK::Communicator

Creates and returns an Communicator that can be used for communication with the GlobalCollect service.



84
85
86
87
# File 'lib/ingenico/connect/sdk/factory.rb', line 84

def self.create_communicator_from_configuration(configuration)
  session = create_session_from_configuration(configuration)
  Communicator.new(session, DefaultImpl::DefaultMarshaller.INSTANCE)
end

.create_communicator_from_file(configuration_file_name, api_key_id, secret_api_key) ⇒ Ingenico::Connect::SDK::Communicator

Creates and returns an Communicator that is used for communication with the GlobalCollect service.



95
96
97
98
99
# File 'lib/ingenico/connect/sdk/factory.rb', line 95

def self.create_communicator_from_file(configuration_file_name, api_key_id, secret_api_key)
  configuration = create_configuration(configuration_file_name, api_key_id, secret_api_key)
  session = create_session_from_configuration(configuration)
  Communicator.new(session, DefaultImpl::DefaultMarshaller.INSTANCE)
end

.create_communicator_from_session(session) ⇒ Ingenico::Connect::SDK::Communicator

Creates and returns an Communicator that is used for communication with the GlobalCollect service.



76
77
78
# File 'lib/ingenico/connect/sdk/factory.rb', line 76

def self.create_communicator_from_session(session)
  Communicator.new(session, DefaultImpl::DefaultMarshaller.INSTANCE)
end

.create_configuration(configuration_file_name, api_key_id, secret_api_key) ⇒ Ingenico::Connect::SDK::CommunicatorConfiguration

Creates and returns a CommunicatorConfiguration based on the configuration in the file located at configuration_file_name.



17
18
19
20
21
22
# File 'lib/ingenico/connect/sdk/factory.rb', line 17

def self.create_configuration(configuration_file_name, api_key_id, secret_api_key)
    properties = YAML::load_file(configuration_file_name)
    CommunicatorConfiguration.new(properties: properties,
                                         api_key_id: api_key_id,
                                         secret_api_key: secret_api_key)
end

.create_session_from_configuration(configuration, meta_data_provider: nil, connection: nil, authenticator: nil) ⇒ Ingenico::Connect::SDK::Session

Creates and returns a Session from an CommunicatorConfiguration, an MetaDataProvider, an Connection and an Authenticator. If an authenticator is not given, a DefaultImpl::DefaultAuthenticator is created using the api_key_id and secret_api_key in the CommunicatorConfiguration.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ingenico/connect/sdk/factory.rb', line 34

def self.create_session_from_configuration(configuration, meta_data_provider: nil,
                                           connection: nil, authenticator: nil)
  unless 
     = MetaDataProvider.new(configuration.integrator,
                                              shopping_cart_extension: configuration.shopping_cart_extension)
  end
  unless connection
    connection = DefaultImpl::DefaultConnection.new({connect_timeout: configuration.connect_timeout,
                                                    socket_timeout: configuration.socket_timeout,
                                                    max_connections: configuration.max_connections,
                                                    proxy_configuration: configuration.proxy_configuration})
  end
  unless authenticator
    authenticator = DefaultImpl::DefaultAuthenticator.new(configuration.authorization_type,
                                             configuration.api_key_id, configuration.secret_api_key)
  end
  Session.new(configuration.api_endpoint, connection, authenticator, )
end

.create_session_from_file(configuration_file_name, api_key_id, secret_api_key, meta_data_provider: nil, connection: nil, authenticator: nil) ⇒ Ingenico::Connect::SDK::Session

Creates and returns a Session from a file containing the communicator configuration, api_key_id, secret_api_key, an MetaDataProvider, an Connection and an Authenticator. If an authenticator is not given, a DefaultImpl::DefaultAuthenticator is created using the api_key_id and secret_api_key in the CommunicatorConfiguration.



65
66
67
68
69
70
# File 'lib/ingenico/connect/sdk/factory.rb', line 65

def self.create_session_from_file(configuration_file_name, api_key_id, secret_api_key,
                                  meta_data_provider: nil, connection: nil, authenticator: nil)
  configuration = create_configuration(configuration_file_name, api_key_id, secret_api_key)
  create_session_from_configuration(configuration, meta_data_provider: , connection: connection,
                                    authenticator: authenticator)
end