Class: Worldline::Acquiring::SDK::Communicator

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

Overview

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

api_endpoint

the base URL to the Worldline Acquiring platform

connection

Worldline::Acquiring::SDK::Communication::Connection used to communicate with the Worldline Acquiring platform

authenticator

Authentication::Authenticator used for authenticating messages sent

metadata_provider

Worldline::Acquiring::SDK::Communication::MetadataProvider object containing information relevant for sending requests

marshaller

JSON::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, metadata_provider, marshaller) ⇒ Communicator

Creates a new Communicator.

Parameters:

Raises:

  • (ArgumentError)


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

def initialize(api_endpoint, connection, authenticator, , marshaller)
  raise ArgumentError.new('api_endpoint is required') unless api_endpoint
  raise ArgumentError.new('connection is required') unless connection
  raise ArgumentError.new('authenticator is required') unless authenticator
  raise ArgumentError.new('metadata_provider is required') unless 
  raise ArgumentError('marshaller is required') if marshaller.nil?

  uri = URI(api_endpoint)
  raise RuntimeError('api_endpoint should not contain a path') unless uri.path.nil? || uri.path.empty?
  unless uri.userinfo.nil? && uri.query.nil? && uri.fragment.nil?
    raise RuntimeError('api_endpoint should not contain user info, query or fragment')
  end
  @api_endpoint = uri
  @connection = connection
  @authenticator = authenticator
  @metadata_provider = 
  @marshaller = marshaller
end

Instance Attribute Details

#marshallerWorldline::Acquiring::SDK::JSON::Marshaller (readonly)

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

Returns:



28
29
30
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 28

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.



407
408
409
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 407

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 Worldline::Acquiring::SDK::Communication::PooledConnection).



374
375
376
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 374

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 Worldline::Acquiring::SDK::Communication::PooledConnection).



368
369
370
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 368

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 Worldline Acquiring platform and returns the response as the given response type.

Parameters:

Returns:

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

Raises:



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 131

def delete(relative_path, request_headers, request_parameters, response_type, context)
  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<Worldline::Acquiring::SDK::Communication::ResponseHeader>, IO| ... } ⇒ Object

Performs a DELETE request to the Worldline Acquiring platform and yields the response as the headers and body.

Parameters:

Yields:

Raises:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 158

def delete_with_binary_response(relative_path, request_headers, request_parameters, context)
  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.



400
401
402
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 400

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:



395
396
397
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 395

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 Worldline Acquiring platform and returns the response as the given response type.

Parameters:

Returns:

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

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 72

def get(relative_path, request_headers, request_parameters, response_type, context)
  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<Worldline::Acquiring::SDK::Communication::ResponseHeader>, IO| ... } ⇒ Object

Performs a GET request to the Worldline Acquiring platform and yields the response as the headers and body.

Parameters:

Yields:

Raises:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 100

def get_with_binary_response(relative_path, request_headers, request_parameters, context)
  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 Worldline Acquiring platform and returns the response as the given response type.

Parameters:

Returns:

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

Raises:



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 190

def post(relative_path, request_headers, request_parameters, request_body, response_type, context)
  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? Communication::MultipartFormDataObject
    request_headers.push(Communication::RequestHeader.new('Content-Type', request_body.content_type))
    body = request_body
  elsif request_body.is_a? Communication::MultipartFormDataRequest
    multipart = request_body.to_multipart_form_data_object
    request_headers.push(Communication::RequestHeader.new('Content-Type', multipart.content_type))
    body = multipart
  elsif !request_body.nil?
    request_headers.push(Communication::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(Communication::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<Worldline::Acquiring::SDK::Communication::ResponseHeader>, IO| ... } ⇒ Object

Performs a POST request to the Worldline Acquiring platform and yields the response as the headers and body.

Parameters:

Yields:

Raises:



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 237

def post_with_binary_response(relative_path, request_headers, request_parameters, request_body, context)
  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? Communication::MultipartFormDataObject
    request_headers.push(Communication::RequestHeader.new('Content-Type', request_body.content_type))
    body = request_body
  elsif request_body.is_a? Communication::MultipartFormDataRequest
    multipart = request_body.to_multipart_form_data_object
    request_headers.push(Communication::RequestHeader.new('Content-Type', multipart.content_type))
    body = multipart
  elsif !request_body.nil?
    request_headers.push(Communication::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(Communication::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 Worldline Acquiring platform and returns the response as the given response type.

Parameters:

Returns:

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

Raises:



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 286

def put(relative_path, request_headers, request_parameters, request_body, response_type, context)
  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? Communication::MultipartFormDataObject
    request_headers.push(Communication::RequestHeader.new('Content-Type', request_body.content_type))
    body = request_body
  elsif request_body.is_a? Communication::MultipartFormDataRequest
    multipart = request_body.to_multipart_form_data_object
    request_headers.push(Communication::RequestHeader.new('Content-Type', multipart.content_type))
    body = multipart
  elsif !request_body.nil?
    request_headers.push(Communication::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(Communication::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<Worldline::Acquiring::SDK::Communication::ResponseHeader>, IO| ... } ⇒ Object

Performs a PUT request to the Worldline Acquiring platform and yields the response as the headers and body.

Parameters:

Yields:

Raises:



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 332

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



380
381
382
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 380

def set_body_obfuscator(body_obfuscator)
  @connection.set_body_obfuscator(body_obfuscator)
end

#set_header_obfuscator(header_obfuscator) ⇒ Object

Sets the current header obfuscator to use.



386
387
388
# File 'lib/worldline/acquiring/sdk/communicator.rb', line 386

def set_header_obfuscator(header_obfuscator)
  @connection.set_header_obfuscator(header_obfuscator)
end