Class: ArtirixDataModels::DataGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/artirix_data_models/gateways/data_gateway.rb

Defined Under Namespace

Modules: ConnectionLoader Classes: BadRequest, Conflict, ConnectionError, Error, Forbidden, GatewayError, NotAcceptable, NotFound, ParseError, RequestTimeout, ServerError, TooManyRequests, Unauthorized, UnprocessableEntity

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection: nil, post_as_json: true, ensure_relative: false, timeout: nil, authorization_bearer: nil, authorization_token_hash: nil) ⇒ DataGateway

Returns a new instance of DataGateway.



4
5
6
7
8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



2
3
4
# File 'lib/artirix_data_models/gateways/data_gateway.rb', line 2

def connection
  @connection
end

#post_as_jsonObject (readonly)

Returns the value of attribute post_as_json.



2
3
4
# File 'lib/artirix_data_models/gateways/data_gateway.rb', line 2

def post_as_json
  @post_as_json
end

Class Method Details

.exception_for_status(response_status) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/artirix_data_models/gateways/data_gateway.rb', line 214

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) ⇒ Object

Raises:

  • (klass)


203
204
205
206
207
208
209
210
211
212
# File 'lib/artirix_data_models/gateways/data_gateway.rb', line 203

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

Instance Method Details

#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, **_ignored_options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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,
         **_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
    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
  end

  parse_response result:           result,
                 response_adaptor: response_adaptor,
                 method:           method,
                 path:             path
end

#delete(path, **opts) ⇒ Object



34
35
36
# File 'lib/artirix_data_models/gateways/data_gateway.rb', line 34

def delete(path, **opts)
  call :delete, path, **opts
end

#ensure_relative?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/artirix_data_models/gateways/data_gateway.rb', line 18

def ensure_relative?
  !!@ensure_relative
end

#get(path, **opts) ⇒ Object



22
23
24
# File 'lib/artirix_data_models/gateways/data_gateway.rb', line 22

def get(path, **opts)
  call :get, path, **opts
end

#post(path, **opts) ⇒ Object



26
27
28
# File 'lib/artirix_data_models/gateways/data_gateway.rb', line 26

def post(path, **opts)
  call :post, path, **opts
end

#put(path, **opts) ⇒ Object



30
31
32
# File 'lib/artirix_data_models/gateways/data_gateway.rb', line 30

def put(path, **opts)
  call :put, path, **opts
end