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.

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



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

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

Instance Attribute Details

#marshallerObject (readonly)

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



179
180
181
# File 'lib/ingenico/connect/sdk/communicator.rb', line 179

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.



174
175
176
# File 'lib/ingenico/connect/sdk/communicator.rb', line 174

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



149
150
151
152
153
154
# File 'lib/ingenico/connect/sdk/communicator.rb', line 149

def close_expired_connections
  connection = @session.connection
  if connection.is_a? PooledConnection
    connection.close_expired_connections
  end
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).



140
141
142
143
144
145
# File 'lib/ingenico/connect/sdk/communicator.rb', line 140

def close_idle_connections(idle_time)
  connection = @session.connection
  if connection.is_a? PooledConnection
    connection.close_idle_connections(idle_time)
  end
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 a Response object.

Throws:

ResponseException

Thrown if the request could not be fulfilled successfully. This occurs for example if the request is not authenticated correctly.

NotFoundException

Thrown if the referred resource is not found.

CommunicationException

Thrown if there is an error in communicating with the Ingenico ePayments platform. This occurs for example if a timeout occurs.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ingenico/connect/sdk/communicator.rb', line 62

def delete(relative_path, request_headers, request_parameters, response_type, context)
  connection = @session.connection
  if request_parameters.nil?
    request_parameter_list = nil
  else
    request_parameter_list = request_parameters.to_request_parameters
  end
  uri = to_absolute_uri(relative_path, request_parameter_list)
  if request_headers.nil?
    request_headers = []
  end
  add_generic_headers('DELETE', uri, request_headers, context)
  response = connection.delete(uri, request_headers)
  process_response(response, response_type, relative_path, context)
end

#disable_loggingObject

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



167
168
169
# File 'lib/ingenico/connect/sdk/communicator.rb', line 167

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.

communicator_logger

The Logging::CommunicatorLogger the requests and responses are logged to



162
163
164
# File 'lib/ingenico/connect/sdk/communicator.rb', line 162

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 a Response object.

Throws:

ResponseException

Thrown if the request could not be fulfilled successfully. This occurs for example if the request is not authenticated correctly

NotFoundException

Thrown if the requested resource is not found

CommunicationException

Thrown if there is an error in communicating with the Ingenico ePayments platform. This occurs for example if a timeout occurs.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ingenico/connect/sdk/communicator.rb', line 38

def get(relative_path, request_headers, request_parameters, response_type, context)
  connection = @session.connection
  if request_parameters.nil?
    request_parameter_list = nil
  else
    request_parameter_list = request_parameters.to_request_parameters
  end
  uri = to_absolute_uri(relative_path, request_parameter_list)
  if request_headers.nil?
    request_headers = []
  end
  add_generic_headers('GET', uri, request_headers, context)
  response = connection.get(uri, request_headers)
  process_response(response, 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 Ingenico ePayments platform and returns the response as a Response object.

Throws:

ResponseException

Thrown if the request could not be fulfilled successfully. This occurs for example if the request is not authenticated correctly.

NotFoundException

Thrown if the referred resource is not found.

CommunicationException

Thrown if there is an error in communicating with the Ingenico ePayments platform. This occurs for example if a timeout occurs.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ingenico/connect/sdk/communicator.rb', line 86

def post(relative_path, request_headers, request_parameters, request_body,
         response_type, context)
  connection = @session.connection
  if request_parameters.nil?
    request_parameter_list = nil
  else
    request_parameter_list = request_parameters.to_request_parameters
  end
  uri = to_absolute_uri(relative_path, request_parameter_list)
  if request_headers.nil?
    request_headers = []
  end
  request_json = nil
  unless request_body.nil?
    request_headers.push(RequestHeader.new('Content-Type', 'application/json'))
    request_json = @marshaller.marshal(request_body)
  end
  add_generic_headers('POST', uri, request_headers, context)
  response = connection.post(uri, request_headers, request_json)
  process_response(response, 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 Ingenico ePayments platform and returns the response as a Response object.

Throws:

ResponseException

Thrown if the request could not be fulfilled successfully. This occurs for example if the request is not authenticated correctly.

NotFoundException

Thrown if the referred resource is not found.

CommunicationException

Thrown if there is an error in communicating with the Ingenico ePayments platform. This occurs for example if a timeout occurs.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ingenico/connect/sdk/communicator.rb', line 116

def put(relative_path, request_headers, request_parameters, request_body,
        response_type, context)
  connection = @session.connection
  if request_parameters.nil?
    request_parameter_list = nil
  else
    request_parameter_list = request_parameters.to_request_parameters
  end
  uri = to_absolute_uri(relative_path, request_parameter_list)
  if request_headers.nil?
    request_headers = []
  end
  request_json = nil
  unless request_body.nil?
    request_headers.push(RequestHeader.new('Content-Type', 'application/json'))
    request_json = @marshaller.marshal(request_body)
  end
  add_generic_headers('PUT', uri, request_headers, context)
  response = connection.put(uri, request_headers, request_json)
  process_response(response, response_type, relative_path, context)
end