class ArtirixDataModels::DataGateway
Attributes
connection[R]
post_as_json[R]
Public Class Methods
new(connection: nil, post_as_json: true, ensure_relative: false, timeout: nil, authorization_bearer: nil, authorization_token_hash: nil)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 4 def initialize(connection: nil, post_as_json: true, ensure_relative: false, timeout: nil, authorization_bearer: nil, authorization_token_hash: nil) @connection = connection || ConnectionLoader.default_connection @post_as_json = !!post_as_json @authorization_bearer = authorization_bearer @authorization_token_hash = authorization_token_hash @timeout = timeout @ensure_relative = !!ensure_relative end
Private Class Methods
exception_for_status(response_status)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 232 def self.exception_for_status(response_status) case response_status.to_i when 404 NotFound when 406 NotAcceptable when 422 UnprocessableEntity when 409 Conflict when 400 BadRequest when 401 Unauthorized when 403 Forbidden when 408 RequestTimeout when 429 TooManyRequests when 500 ServerError else GatewayError end end
treat_response(response, method, path)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 221 def self.treat_response(response, method, path) return response.body if response.success? klass = exception_for_status(response.status) raise klass, path: path, method: method, response_body: response.body, response_status: response.status end
Public Instance Methods
call(method, path, json_body: true, response_adaptor: nil, body: nil, fake: false, fake_response: nil, cache_adaptor: nil, timeout: nil, authorization_bearer: nil, authorization_token_hash: nil, headers: nil, **_ignored_options)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 38 def call(method, path, json_body: true, response_adaptor: nil, body: nil, fake: false, fake_response: nil, cache_adaptor: nil, timeout: nil, authorization_bearer: nil, authorization_token_hash: nil, headers: nil, **_ignored_options) if fake result = fake_response.respond_to?(:call) ? fake_response.call : fake_response elsif cache_adaptor.present? result = cache_adaptor.call do perform method, path: path, body: body, json_body: json_body, timeout: timeout, authorization_bearer: authorization_bearer, authorization_token_hash: authorization_token_hash, headers: headers end else result = perform method, path: path, body: body, json_body: json_body, timeout: timeout, authorization_bearer: authorization_bearer, authorization_token_hash: authorization_token_hash, headers: headers end parse_response result: result, response_adaptor: response_adaptor, method: method, path: path rescue => e # if anything goes wrong => delete the cache just in case. if cache_adaptor.present? && cache_adaptor.respond_to?(:delete) cache_adaptor.delete end raise e end
delete(path, **opts)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 34 def delete(path, **opts) call :delete, path, **opts end
ensure_relative?()
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 18 def ensure_relative? !!@ensure_relative end
get(path, **opts)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 22 def get(path, **opts) call :get, path, **opts end
post(path, **opts)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 26 def post(path, **opts) call :post, path, **opts end
put(path, **opts)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 30 def put(path, **opts) call :put, path, **opts end
Private Instance Methods
body_to_json(body)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 179 def body_to_json(body) case body when String body else body.to_json end end
connect(method, path:, body: nil, json_body: true, timeout: nil, authorization_bearer: nil, authorization_token_hash: nil, headers: nil)
click to toggle source
for options ‘timeout`, `authorization_bearer` and `authorization_token_hash`: if `nil` is passed (or param is omitted) it will try to use the default passed on the gateway creation but if `false` is passed, it will stay as false (can be used to override a default option passed on gateway creation)
# File lib/artirix_data_models/gateways/data_gateway.rb, line 136 def connect(method, path:, body: nil, json_body: true, timeout: nil, authorization_bearer: nil, authorization_token_hash: nil, headers: nil) timeout = timeout.nil? ? @timeout : timeout authorization_bearer = authorization_bearer.nil? ? @authorization_bearer : authorization_bearer authorization_token_hash = authorization_token_hash.nil? ? @authorization_token_hash : authorization_token_hash if ensure_relative? path = path.to_s.start_with?('/') ? path.to_s[1..-1] : path end connection.send(method, path) do |req| req.options.timeout = timeout if timeout.present? req.headers['Authorization'] = Faraday::Request::Authorization.header(:Bearer, authorization_bearer) if authorization_bearer.present? req.headers['Authorization'] = Faraday::Request::Authorization.header(:Token, authorization_token_hash) if authorization_token_hash.present? unless body.nil? if json_body req.body = body_to_json body req.headers['Content-Type'] = 'application/json' else req.body = body end end Array(headers).each do |key, value| req.headers[key.to_s] = value end end rescue Faraday::ConnectionFailed, Faraday::Error::TimeoutError, Errno::ETIMEDOUT => e raise ConnectionError, path: path, method: method, message: "method: #{method}, path: #{path}, error: #{e}" end
exception_for_status(response_status)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 217 def exception_for_status(response_status) self.class.exception_for_status(response_status) end
parse_response(result:, response_adaptor:, path:, method:)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 188 def parse_response(result:, response_adaptor:, path:, method:) if result.present? parsed_response = Oj.load result, symbol_keys: true else parsed_response = nil end if response_adaptor.present? response_adaptor.call parsed_response else parsed_response end rescue Oj::ParseError => e raise ParseError, path: path, method: method, response_body: result, message: e.message end
perform(method, path:, body: nil, json_body: true, timeout: nil, authorization_bearer: nil, authorization_token_hash: nil, headers: nil)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 110 def perform(method, path:, body: nil, json_body: true, timeout: nil, authorization_bearer: nil, authorization_token_hash: nil, headers: nil) pars = { path: path, body: body, json_body: json_body, timeout: timeout, authorization_bearer: authorization_bearer, authorization_token_hash: authorization_token_hash, headers: headers } response = connect(method, pars) treat_response(response, method, path) end
perform_delete(path, **opts)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 106 def perform_delete(path, **opts) perform :delete, path: path, **opts end
perform_get(path, **opts)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 94 def perform_get(path, **opts) perform :get, path: path, **opts end
perform_post(path, **opts)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 98 def perform_post(path, **opts) perform :post, path: path, **opts end
perform_put(path, **opts)
click to toggle source
# File lib/artirix_data_models/gateways/data_gateway.rb, line 102 def perform_put(path, **opts) perform :put, path: path, **opts end
treat_response(response, method, path)
click to toggle source
EXCEPTION TREATMENT #
# File lib/artirix_data_models/gateways/data_gateway.rb, line 213 def treat_response(response, method, path) self.class.treat_response(response, method, path) end