Class: FrederickAPI::V2::Helpers::Requestor

Inherits:
JsonApiClient::Query::Requestor
  • Object
show all
Defined in:
lib/frederick_api/v2/helpers/requestor.rb

Overview

Requestor for v2 client to use built on top of JsonApiClient::Query::Requestor

Constant Summary collapse

GET_VIA_POST_PATHS =

Paths that may have an unbounded query param length so we should always use a POST instead of a GET to get around AWS Cloudfront limitations

[
  %r{^.*locations\/[^\/]+\/contacts$},
  %r{^.*locations\/[^\/]+\/interactions$}
].map(&:freeze).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, path = nil) ⇒ Requestor

Returns a new instance of Requestor.



17
18
19
20
# File 'lib/frederick_api/v2/helpers/requestor.rb', line 17

def initialize(klass, path = nil)
  @klass = klass
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/frederick_api/v2/helpers/requestor.rb', line 8

def path
  @path
end

Instance Method Details

#get(params = {}) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/frederick_api/v2/helpers/requestor.rb', line 31

def get(params = {})
  path = resource_path(params)

  params.delete(klass.primary_key)
  return request(:post, path, params, 'X-Request-Method' => 'GET') if get_via_post_path?(path)
  request(:get, path, params)
end

#linked(path) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/frederick_api/v2/helpers/requestor.rb', line 39

def linked(path)
  uri = URI.parse(path)
  return super unless get_via_post_path?(uri.path)

  path_without_params = "#{uri.scheme}://#{uri.host}#{uri.path}"
  params = uri.query ? CGI.parse(uri.query).each_with_object({}) { |(k, v), h| h[k] = v[0] } : {}
  request(:post, path_without_params, params, 'X-Request-Method' => 'GET')
end

#request(type, path, params, additional_headers = {}) ⇒ Object

Retry once on unhandled server errors



49
50
51
52
53
54
55
56
57
58
# File 'lib/frederick_api/v2/helpers/requestor.rb', line 49

def request(type, path, params, additional_headers = {})
  headers = klass.custom_headers.merge(additional_headers)
  make_request = proc { handle_background(handle_errors(make_request(type, path, params, headers))) }
  begin
    make_request.call
  rescue JsonApiClient::Errors::ConnectionError, JsonApiClient::Errors::ServerError => ex
    raise ex if ex.is_a?(JsonApiClient::Errors::NotFound) || ex.is_a?(JsonApiClient::Errors::Conflict)
    make_request.call
  end
end

#resource_path(parameters) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/frederick_api/v2/helpers/requestor.rb', line 22

def resource_path(parameters)
  base_path = path || klass.path(parameters)
  if (resource_id = parameters[klass.primary_key])
    File.join(base_path, encode_part(resource_id))
  else
    base_path
  end
end