Class: RemoteResource::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_resource/request.rb

Constant Summary collapse

SUPPORTED_HTTP_METHODS =
[:get, :put, :patch, :post, :delete].freeze
DEFAULT_HEADERS =
{
  'Accept'     => 'application/json',
  'User-Agent' => "RemoteResource #{RemoteResource::VERSION}"
}.freeze
DEFAULT_CONTENT_TYPE =
{
  'Content-Type' => 'application/json'
}.freeze
DEFAULT_EXTENSION =
'.json'.freeze
DEFAULT_CONNECT_TIMEOUT =
30
DEFAULT_READ_TIMEOUT =
120

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, http_action, attributes = {}, connection_options = {}) ⇒ Request

Returns a new instance of Request.



22
23
24
25
26
27
28
# File 'lib/remote_resource/request.rb', line 22

def initialize(resource, http_action, attributes = {}, connection_options = {})
  @resource           = resource
  @resource_klass     = resource.is_a?(Class) ? resource : resource.class
  @http_action        = http_action.to_sym
  @attributes         = attributes
  @connection_options = connection_options.dup
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



20
21
22
# File 'lib/remote_resource/request.rb', line 20

def attributes
  @attributes
end

#http_actionObject (readonly)

Returns the value of attribute http_action.



20
21
22
# File 'lib/remote_resource/request.rb', line 20

def http_action
  @http_action
end

#resourceObject (readonly)

Returns the value of attribute resource.



20
21
22
# File 'lib/remote_resource/request.rb', line 20

def resource
  @resource
end

#resource_klassObject (readonly)

Returns the value of attribute resource_klass.



20
21
22
# File 'lib/remote_resource/request.rb', line 20

def resource_klass
  @resource_klass
end

Instance Method Details

#bodyObject



79
80
81
82
83
84
85
86
87
# File 'lib/remote_resource/request.rb', line 79

def body
  @body ||= begin
    if [:put, :patch, :post].include?(http_action)
      attributes.to_json
    else
      nil
    end
  end
end

#conditional_headersObject



117
118
119
120
121
122
# File 'lib/remote_resource/request.rb', line 117

def conditional_headers
  headers = {}
  headers = headers.merge(DEFAULT_CONTENT_TYPE) if body.present?
  headers = headers.merge({ 'X-Request-Id' => RequestStore.store[:request_id] }) if RequestStore.store[:request_id].present?
  headers
end

#connectionObject



30
31
32
# File 'lib/remote_resource/request.rb', line 30

def connection
  resource_klass.connection
end

#connection_optionsObject



34
35
36
37
38
39
40
41
42
# File 'lib/remote_resource/request.rb', line 34

def connection_options
  @combined_connection_options ||= begin
    default = resource.connection_options.to_hash # Defined on the resource (klass).
    block   = resource.try(:threaded_connection_options) || {} # Given as arguments in the .with_connection_options block.
    local   = @connection_options # Given as arguments directly.

    default.deep_merge(block).deep_merge(local)
  end
end

#headersObject



107
108
109
110
111
112
113
114
115
# File 'lib/remote_resource/request.rb', line 107

def headers
  @headers ||= begin
    default_headers = connection_options[:default_headers].presence || DEFAULT_HEADERS
    global_headers  = RemoteResource::Base.global_headers.presence || {}
    headers         = connection_options[:headers].presence || {}

    default_headers.merge(global_headers).merge(headers).merge(conditional_headers)
  end
end

#performObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/remote_resource/request.rb', line 44

def perform
  SUPPORTED_HTTP_METHODS.include?(http_action) || raise(RemoteResource::HTTPMethodUnsupported, "Requested HTTP method=#{http_action.to_s} is NOT supported, the HTTP action MUST be a supported HTTP action=#{SUPPORTED_HTTP_METHODS.join(', ')}")

  connection_response = connection.public_send(http_action, request_url, params: query, body: body, headers: headers, **timeout_options)
  response            = RemoteResource::Response.new(connection_response, connection_options.merge(request: self, connection_request: connection_response.request))

  if response.success? || response.unprocessable_entity?
    response
  else
    raise_http_error(self, response)
  end
end

#queryObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/remote_resource/request.rb', line 67

def query
  @query ||= begin
    params = connection_options[:params]

    if params.present?
      RemoteResource::Util.encode_params_to_query(params)
    else
      nil
    end
  end
end

#request_urlObject



57
58
59
60
61
62
63
64
65
# File 'lib/remote_resource/request.rb', line 57

def request_url
  @request_url ||= begin
    id        = @attributes[:id].presence || connection_options[:id]
    base_url  = connection_options[:base_url].presence || RemoteResource::UrlNamingDetermination.new(resource_klass, connection_options).base_url(id, check_collection_options: true)
    extension = connection_options[:extension] || DEFAULT_EXTENSION

    "#{base_url}#{extension}"
  end
end

#timeout_optionsObject



124
125
126
127
128
129
# File 'lib/remote_resource/request.rb', line 124

def timeout_options
  connecttimeout = connection_options[:connecttimeout].presence || DEFAULT_CONNECT_TIMEOUT
  timeout = connection_options[:timeout].presence || DEFAULT_READ_TIMEOUT

  { connecttimeout: connecttimeout, timeout: timeout }
end