Class: Ingenico::Connect::SDK::Communicator

Inherits:
Object
  • Object
show all
Includes:
Logging::LoggingCapable, Logging::Obfuscation::ObfuscationCapable
Defined in:
lib/ingenico/connect/sdk/communicator.rb

Overview

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

session

Session that stores data for network communication and facilitates network communication

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(session, marshaller) ⇒ Communicator

Creates a new Communicator based on a session and marshaller.

Parameters:



33
34
35
36
37
38
39
# File 'lib/ingenico/connect/sdk/communicator.rb', line 33

def initialize(session, marshaller)
  raise ArgumentError('session is required') if session.nil?
  raise ArgumentError('marshaller is required') if marshaller.nil?

  @session = session
  @marshaller = marshaller
end

Instance Attribute Details

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

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

Returns:



24
25
26
# File 'lib/ingenico/connect/sdk/communicator.rb', line 24

def marshaller
  @marshaller
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.



401
402
403
# File 'lib/ingenico/connect/sdk/communicator.rb', line 401

def close
  @session.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).



365
366
367
368
# File 'lib/ingenico/connect/sdk/communicator.rb', line 365

def close_expired_connections
  connection = @session.connection
  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).



358
359
360
361
# File 'lib/ingenico/connect/sdk/communicator.rb', line 358

def close_idle_connections(idle_time)
  connection = @session.connection
  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:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ingenico/connect/sdk/communicator.rb', line 115

def delete(relative_path, request_headers, request_parameters, response_type, context)
  connection = @session.connection
  request_parameter_list = request_parameters && request_parameters.to_request_parameters
  uri = to_absolute_uri(relative_path, request_parameter_list)
  request_headers = [] if request_headers.nil?
  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

#delete_with_binary_response(relative_path, request_headers, request_parameters, context) {|Array<Ingenico::Connect::SDK::ResponseHeader>, IO| ... } ⇒ Object

Performs a DELETE request to the Ingenico ePayments platform and yields the response as the headers and body.

Parameters:

Yields:

Raises:



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ingenico/connect/sdk/communicator.rb', line 143

def delete_with_binary_response(relative_path, request_headers, request_parameters, context)
  connection = @session.connection
  request_parameter_list = request_parameters && request_parameters.to_request_parameters
  uri = to_absolute_uri(relative_path, request_parameter_list)
  request_headers = [] if request_headers.nil?
  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 = process_binary_response(status_code, content, headers, context) do |h, c|
      yield h, c
    end
  end
  throw_exception_if_necessary(response_body, response_status_code, response_headers, relative_path)
end

#disable_loggingObject

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



394
395
396
# File 'lib/ingenico/connect/sdk/communicator.rb', line 394

def disable_logging
  @session.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:



389
390
391
# File 'lib/ingenico/connect/sdk/communicator.rb', line 389

def enable_logging(communicator_logger)
  @session.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:



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

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

  request_headers = [] if request_headers.nil?
  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

#get_with_binary_response(relative_path, request_headers, request_parameters, context) {|Array<Ingenico::Connect::SDK::ResponseHeader>, IO| ... } ⇒ Object

Performs a GET request to the Ingenico ePayments platform and yields the response as the headers and body.

Parameters:

Yields:

Raises:



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

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

  request_headers = [] if request_headers.nil?
  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 = process_binary_response(status_code, content, headers, context) do |h, c|
      yield h, c
    end
  end
  throw_exception_if_necessary(response_body, response_status_code, response_headers, relative_path)
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:



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/ingenico/connect/sdk/communicator.rb', line 176

def post(relative_path, request_headers, request_parameters, request_body, response_type, context)
  connection = @session.connection
  request_parameter_list = request_parameters && request_parameters.to_request_parameters
  uri = to_absolute_uri(relative_path, request_parameter_list)
  request_headers = [] if request_headers.nil?

  body = nil
  if request_body.is_a? MultipartFormDataObject
    request_headers.push(RequestHeader.new('Content-Type', request_body.content_type))
    body = request_body
  elsif request_body.is_a? MultipartFormDataRequest
    multipart = request_body.to_multipart_form_data_object
    request_headers.push(RequestHeader.new('Content-Type', multipart.content_type))
    body = multipart
  elsif !request_body.nil?
    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

#post_with_binary_response(relative_path, request_headers, request_parameters, request_body, context) {|Array<Ingenico::Connect::SDK::ResponseHeader>, IO| ... } ⇒ Object

Performs a POST request to the Ingenico ePayments platform and yields the response as the headers and body.

Parameters:

Yields:

Raises:



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/ingenico/connect/sdk/communicator.rb', line 224

def post_with_binary_response(relative_path, request_headers, request_parameters, request_body, context)
  connection = @session.connection
  request_parameter_list = request_parameters && request_parameters.to_request_parameters
  uri = to_absolute_uri(relative_path, request_parameter_list)
  request_headers = [] if request_headers.nil?

  body = nil
  if request_body.is_a? MultipartFormDataObject
    request_headers.push(RequestHeader.new('Content-Type', request_body.content_type))
    body = request_body
  elsif request_body.is_a? MultipartFormDataRequest
    multipart = request_body.to_multipart_form_data_object
    request_headers.push(RequestHeader.new('Content-Type', multipart.content_type))
    body = multipart
  elsif !request_body.nil?
    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 = process_binary_response(status_code, content, headers, context) do |h, c|
      yield h, c
    end
  end
  throw_exception_if_necessary(response_body, response_status_code, response_headers, relative_path)
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:



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/ingenico/connect/sdk/communicator.rb', line 274

def put(relative_path, request_headers, request_parameters, request_body, response_type, context)
  connection = @session.connection
  request_parameter_list = request_parameters && request_parameters.to_request_parameters
  uri = to_absolute_uri(relative_path, request_parameter_list)
  request_headers = [] if request_headers.nil?

  body = nil
  if request_body.is_a? MultipartFormDataObject
    request_headers.push(RequestHeader.new('Content-Type', request_body.content_type))
    body = request_body
  elsif request_body.is_a? MultipartFormDataRequest
    multipart = request_body.to_multipart_form_data_object
    request_headers.push(RequestHeader.new('Content-Type', multipart.content_type))
    body = multipart
  elsif !request_body.nil?
    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

#put_with_binary_response(relative_path, request_headers, request_parameters, request_body, context) {|Array<Ingenico::Connect::SDK::ResponseHeader>, IO| ... } ⇒ Object

Performs a PUT request to the Ingenico ePayments platform and yields the response as the headers and body.

Parameters:

Yields:

Raises:



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/ingenico/connect/sdk/communicator.rb', line 321

def put_with_binary_response(relative_path, request_headers, request_parameters, request_body, context)
  connection = @session.connection
  request_parameter_list = request_parameters && request_parameters.to_request_parameters
  uri = to_absolute_uri(relative_path, request_parameter_list)
  request_headers = [] if request_headers.nil?

  body = nil
  if request_body.is_a? MultipartFormDataObject
    request_headers.push(RequestHeader.new('Content-Type', request_body.content_type))
    body = request_body
  elsif request_body.is_a? MultipartFormDataRequest
    multipart = request_body.to_multipart_form_data_object
    request_headers.push(RequestHeader.new('Content-Type', multipart.content_type))
    body = multipart
  elsif !request_body.nil?
    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 = process_binary_response(status_code, content, headers, context) do |h, c|
      yield h, c
    end
  end
  throw_exception_if_necessary(response_body, response_status_code, response_headers, relative_path)
end

#set_body_obfuscator(body_obfuscator) ⇒ Object

Sets the current body obfuscator to use.



372
373
374
375
# File 'lib/ingenico/connect/sdk/communicator.rb', line 372

def set_body_obfuscator(body_obfuscator)
  connection = @session.connection
  connection.set_body_obfuscator(body_obfuscator) if connection.is_a? Logging::Obfuscation::ObfuscationCapable
end

#set_header_obfuscator(header_obfuscator) ⇒ Object

Sets the current header obfuscator to use.



379
380
381
382
# File 'lib/ingenico/connect/sdk/communicator.rb', line 379

def set_header_obfuscator(header_obfuscator)
  connection = @session.connection
  connection.set_header_obfuscator(header_obfuscator) if connection.is_a? Logging::Obfuscation::ObfuscationCapable
end