Class: Ingenico::Direct::SDK::Communicator

Inherits:
Object
  • Object
show all
Includes:
Logging::LoggingCapable
Defined in:
lib/ingenico/direct/sdk/communicator.rb

Overview

Class responsible for facilitating communication with the Ingenico ePayments platform. It combines the following classes to provide communication functionality:

api_endpoint

The base URI (URI::HTTP) to the Ingenico ePayments platform

connection

Connection used to communicate with the Ingenico ePayments platform

authenticator

Authenticator used for authenticating messages sent

meta_data_provider

MetaDataProvider object containing information relevant for sending requests

marshaller

Marshaller that is used to marshal and unmarshal data to and from JSON format

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_endpoint, connection, authenticator, meta_data_provider, marshaller) ⇒ Communicator

Creates a new Communicator based on the given arguments.

Parameters:

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ingenico/direct/sdk/communicator.rb', line 26

def initialize(api_endpoint, connection, authenticator, , marshaller)
  raise ArgumentError, 'api_endpoint is required' unless api_endpoint
  raise ArgumentError, 'connection is required' unless connection
  raise ArgumentError, 'authenticator is required' unless authenticator
  raise ArgumentError, 'meta_data_provider is required' unless 
  raise ArgumentError('marshaller is required') unless marshaller

  @api_endpoint = URI(api_endpoint)
  if @api_endpoint.path.length.positive? || @api_endpoint.query || @api_endpoint.fragment
    raise ArgumentError, "Base URL should not contain a path, query or fragment #{@api_endpoint}"
  end
  @connection = connection
  @authenticator = authenticator
  @meta_data_provider = 
  @marshaller = marshaller
end

Instance Attribute Details

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint.



216
217
218
# File 'lib/ingenico/direct/sdk/communicator.rb', line 216

def api_endpoint
  @api_endpoint
end

#authenticatorObject (readonly)

Returns the value of attribute authenticator.



218
219
220
# File 'lib/ingenico/direct/sdk/communicator.rb', line 218

def authenticator
  @authenticator
end

#connectionObject (readonly)

Returns the value of attribute connection.



217
218
219
# File 'lib/ingenico/direct/sdk/communicator.rb', line 217

def connection
  @connection
end

#marshallerIngenico::Direct::SDK::Marshaller (readonly)

A Marshaller instance used by the communicator for serializing/deserializing to/from JSON

Returns:



15
16
17
# File 'lib/ingenico/direct/sdk/communicator.rb', line 15

def marshaller
  @marshaller
end

#meta_data_providerObject (readonly)

Returns the value of attribute meta_data_provider.



219
220
221
# File 'lib/ingenico/direct/sdk/communicator.rb', line 219

def 
  @meta_data_provider
end

Instance Method Details

#closeObject

Frees networking resources by closing the underlying network connections. After calling close, any use of the get, delete, post and put methods will not function and likely result in an error.



211
212
213
# File 'lib/ingenico/direct/sdk/communicator.rb', line 211

def close
  @connection.close
end

#close_expired_connectionsObject

Closes any connections that have expired. Will not have any effect if the connection is not a pooled connection (an instance of PooledConnection).



190
191
192
# File 'lib/ingenico/direct/sdk/communicator.rb', line 190

def close_expired_connections
  @connection.close_expired_connections if connection.is_a? PooledConnection
end

#close_idle_connections(idle_time) ⇒ Object

Closes any connections idle for more than idle_time seconds. Will not have any effect if the connection is not a pooled connection (an instance of PooledConnection).



184
185
186
# File 'lib/ingenico/direct/sdk/communicator.rb', line 184

def close_idle_connections(idle_time)
  @connection.close_idle_connections(idle_time) if connection.is_a? PooledConnection
end

#delete(relative_path, request_headers, request_parameters, response_type, context) ⇒ Object

Performs a DELETE request to the Ingenico ePayments platform and returns the response as the given response type.

Parameters:

Returns:

  • The response of the DELETE request as the given response type

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ingenico/direct/sdk/communicator.rb', line 86

def delete(relative_path, request_headers, request_parameters, response_type, context)
  connection = @connection
  request_parameter_list = request_parameters&.to_request_parameters
  uri = to_absolute_uri(relative_path, request_parameter_list)
  request_headers ||= []
  add_generic_headers('DELETE', uri, request_headers, context)

  response_status_code, response_headers, response_body = nil
  connection.delete(uri, request_headers) do |status_code, headers, content|
    response_status_code = status_code
    response_headers = headers
    response_body = content.read.force_encoding('UTF-8')
  end
  process_response(response_body, response_status_code, response_headers, response_type, relative_path, context)
end

#disable_loggingObject

Disables logging by unregistering any previous logger that might be registered.



204
205
206
# File 'lib/ingenico/direct/sdk/communicator.rb', line 204

def disable_logging
  @connection.disable_logging
end

#enable_logging(communicator_logger) ⇒ Object

Enables logging outgoing requests and incoming responses by registering the communicator_logger. Note that only one logger can be registered at a time and calling enable_logging a second time will override the old logger instance with the new one.

Parameters:



199
200
201
# File 'lib/ingenico/direct/sdk/communicator.rb', line 199

def enable_logging(communicator_logger)
  @connection.enable_logging(communicator_logger)
end

#get(relative_path, request_headers, request_parameters, response_type, context) ⇒ Object

Performs a GET request to the Ingenico ePayments platform and returns the response as the given response type.

Parameters:

Returns:

  • the response of the GET request as the given response type

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ingenico/direct/sdk/communicator.rb', line 56

def get(relative_path, request_headers, request_parameters, response_type, context)
  connection = @connection
  request_parameter_list = request_parameters&.to_request_parameters
  uri = to_absolute_uri(relative_path, request_parameter_list)

  request_headers ||= []
  add_generic_headers('GET', uri, request_headers, context)

  response_status_code, response_headers, response_body = nil
  connection.get(uri, request_headers) do |status_code, headers, content|
    response_status_code = status_code
    response_headers = headers
    response_body = content.read.force_encoding('UTF-8')
  end
  process_response(response_body, response_status_code, response_headers, response_type, relative_path, context)
end

#post(relative_path, request_headers, request_parameters, request_body, response_type, context) ⇒ Object

Performs a POST request to the Ingenico ePayments platform and returns the response as the given response type.

Parameters:

Returns:

  • The response of the POST request as the given response type

Raises:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ingenico/direct/sdk/communicator.rb', line 116

def post(relative_path, request_headers, request_parameters, request_body, response_type, context)
  request_parameter_list = request_parameters&.to_request_parameters
  uri = to_absolute_uri(relative_path, request_parameter_list)
  request_headers ||= []

  body = nil
  if request_body
    request_headers.push(RequestHeader.new('Content-Type', 'application/json'))
    body = @marshaller.marshal(request_body)
  else
    # Set the content-type, even though there is no body, to prevent the httpClient from
    # adding a content-type header after authentication has been generated.
    request_headers.push(RequestHeader.new('Content-Type', 'text/plain'))
  end

  add_generic_headers('POST', uri, request_headers, context)

  response_status_code, response_headers, response_body = nil
  @connection.post(uri, request_headers, body) do |status_code, headers, content|
    response_status_code = status_code
    response_headers = headers
    response_body = content.read.force_encoding('UTF-8')
  end
  process_response(response_body, response_status_code, response_headers, response_type, relative_path, context)
end

#put(relative_path, request_headers, request_parameters, request_body, response_type, context) ⇒ Object

Performs a PUT request to the Ingenico ePayments platform and returns the response as the given response type.

Parameters:

Returns:

  • The response of the PUT request as the given response type

Raises:



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ingenico/direct/sdk/communicator.rb', line 157

def put(relative_path, request_headers, request_parameters, request_body, response_type, context)
  request_parameter_list = request_parameters&.to_request_parameters
  uri = to_absolute_uri(relative_path, request_parameter_list)
  request_headers ||= []

  body = nil
  if request_body
    request_headers.push(RequestHeader.new('Content-Type', 'application/json'))
    body = @marshaller.marshal(request_body)
  else
    # Set the content-type, even though there is no body, to prevent the httpClient from
    # adding a content-type header after authentication has been generated.
    request_headers.push(RequestHeader.new('Content-Type', 'text/plain'))
  end
  add_generic_headers('PUT', uri, request_headers, context)

  response_status_code, response_headers, response_body = nil
  @connection.put(uri, request_headers, body) do |status_code, headers, content|
    response_status_code = status_code
    response_headers = headers
    response_body = content.read.force_encoding('UTF-8')
  end
  process_response(response_body, response_status_code, response_headers, response_type, relative_path, context)
end