Class: OnlinePayments::SDK::Communicator
- Inherits:
-
Object
- Object
- OnlinePayments::SDK::Communicator
- 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
-
#api_endpoint ⇒ Object
readonly
Returns the value of attribute api_endpoint.
-
#authenticator ⇒ Object
readonly
Returns the value of attribute authenticator.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#marshaller ⇒ OnlinePayments::SDK::Marshaller
readonly
A Marshaller instance used by the communicator for serializing/deserializing to/from JSON.
-
#meta_data_provider ⇒ Object
readonly
Returns the value of attribute meta_data_provider.
Instance Method Summary collapse
-
#close ⇒ Object
Frees networking resources by closing the underlying network connections.
-
#close_expired_connections ⇒ Object
Closes any connections that have expired.
-
#close_idle_connections(idle_time) ⇒ Object
Closes any connections idle for more than idle_time seconds.
-
#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.
-
#disable_logging ⇒ Object
Disables logging by unregistering any previous logger that might be registered.
-
#enable_logging(communicator_logger) ⇒ Object
Enables logging outgoing requests and incoming responses by registering the communicator_logger.
-
#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.
-
#initialize(api_endpoint, connection, authenticator, meta_data_provider, marshaller) ⇒ Communicator
constructor
Creates a new Communicator based on the given arguments.
-
#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.
-
#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.
Constructor Details
#initialize(api_endpoint, connection, authenticator, meta_data_provider, marshaller) ⇒ Communicator
Creates a new Communicator based on the given arguments.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 28 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_endpoint ⇒ Object (readonly)
Returns the value of attribute api_endpoint.
225 226 227 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 225 def api_endpoint @api_endpoint end |
#authenticator ⇒ Object (readonly)
Returns the value of attribute authenticator.
227 228 229 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 227 def authenticator @authenticator end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
226 227 228 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 226 def connection @connection end |
#marshaller ⇒ OnlinePayments::SDK::Marshaller (readonly)
A Marshaller instance used by the communicator for serializing/deserializing to/from JSON
17 18 19 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 17 def marshaller @marshaller end |
#meta_data_provider ⇒ Object (readonly)
Returns the value of attribute meta_data_provider.
228 229 230 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 228 def @meta_data_provider end |
Instance Method Details
#close ⇒ Object
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.
221 222 223 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 221 def close @connection.close end |
#close_expired_connections ⇒ Object
Closes any connections that have expired. Will not have any effect if the connection is not a pooled connection (an instance of PooledConnection).
200 201 202 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 200 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).
194 195 196 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 194 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.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 88 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_logging ⇒ Object
Disables logging by unregistering any previous logger that might be registered.
214 215 216 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 214 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.
209 210 211 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 209 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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 58 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.
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 149 150 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 119 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.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/onlinepayments/sdk/communicator.rb', line 167 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 |