Class: OnlinePayments::SDK::Communicator

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

Overview

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

api_endpoint

The base URI (URI::HTTP) to the Online Payments platform

connection

Connection used to communicate with the Online Payments 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/onlinepayments/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.



223
224
225
# File 'lib/onlinepayments/sdk/communicator.rb', line 223

def api_endpoint
  @api_endpoint
end

#authenticatorObject (readonly)

Returns the value of attribute authenticator.



225
226
227
# File 'lib/onlinepayments/sdk/communicator.rb', line 225

def authenticator
  @authenticator
end

#connectionObject (readonly)

Returns the value of attribute connection.



224
225
226
# File 'lib/onlinepayments/sdk/communicator.rb', line 224

def connection
  @connection
end

#marshallerOnlinePayments::SDK::Marshaller (readonly)

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

Returns:



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

def marshaller
  @marshaller
end

#meta_data_providerObject (readonly)

Returns the value of attribute meta_data_provider.



226
227
228
# File 'lib/onlinepayments/sdk/communicator.rb', line 226

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.



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

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



198
199
200
# File 'lib/onlinepayments/sdk/communicator.rb', line 198

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



192
193
194
# File 'lib/onlinepayments/sdk/communicator.rb', line 192

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 Online Payments 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/onlinepayments/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.



212
213
214
# File 'lib/onlinepayments/sdk/communicator.rb', line 212

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:



207
208
209
# File 'lib/onlinepayments/sdk/communicator.rb', line 207

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 Online Payments 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/onlinepayments/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 Online Payments platform and returns the response as the given response type.

Parameters:

Returns:

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

Raises:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/onlinepayments/sdk/communicator.rb', line 117

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

Parameters:

Returns:

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

Raises:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/onlinepayments/sdk/communicator.rb', line 165

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