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

Inherits:
Object
  • Object
show all
Includes:
Logging::LoggingCapable
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:



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

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:



12
13
14
# File 'lib/ingenico/connect/sdk/communicator.rb', line 12

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.



383
384
385
# File 'lib/ingenico/connect/sdk/communicator.rb', line 383

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).



361
362
363
364
# File 'lib/ingenico/connect/sdk/communicator.rb', line 361

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).



353
354
355
356
357
# File 'lib/ingenico/connect/sdk/communicator.rb', line 353

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:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ingenico/connect/sdk/communicator.rb', line 104

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:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ingenico/connect/sdk/communicator.rb', line 133

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.



376
377
378
# File 'lib/ingenico/connect/sdk/communicator.rb', line 376

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:



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

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:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ingenico/connect/sdk/communicator.rb', line 41

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:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ingenico/connect/sdk/communicator.rb', line 71

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:



167
168
169
170
171
172
173
174
175
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
# File 'lib/ingenico/connect/sdk/communicator.rb', line 167

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:



216
217
218
219
220
221
222
223
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
# File 'lib/ingenico/connect/sdk/communicator.rb', line 216

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:



267
268
269
270
271
272
273
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
# File 'lib/ingenico/connect/sdk/communicator.rb', line 267

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:



315
316
317
318
319
320
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
# File 'lib/ingenico/connect/sdk/communicator.rb', line 315

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