Class: RemoteResource::Request
- Inherits:
-
Object
- Object
- RemoteResource::Request
- 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
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#http_action ⇒ Object
readonly
Returns the value of attribute http_action.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#resource_klass ⇒ Object
readonly
Returns the value of attribute resource_klass.
Instance Method Summary collapse
- #body ⇒ Object
- #conditional_headers ⇒ Object
- #connection ⇒ Object
- #connection_options ⇒ Object
- #headers ⇒ Object
-
#initialize(resource, http_action, attributes = {}, connection_options = {}) ⇒ Request
constructor
A new instance of Request.
- #perform ⇒ Object
- #query ⇒ Object
- #request_url ⇒ Object
- #timeout_options ⇒ Object
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 = {}, = {}) @resource = resource @resource_klass = resource.is_a?(Class) ? resource : resource.class @http_action = http_action.to_sym @attributes = attributes @connection_options = .dup end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
20 21 22 |
# File 'lib/remote_resource/request.rb', line 20 def attributes @attributes end |
#http_action ⇒ Object (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 |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
20 21 22 |
# File 'lib/remote_resource/request.rb', line 20 def resource @resource end |
#resource_klass ⇒ Object (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
#body ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/remote_resource/request.rb', line 79 def body @body ||= begin case http_action when :put, :patch, :post attributes.to_json when :get [:params].to_json if [:force_get_params_in_body] else nil end end end |
#conditional_headers ⇒ Object
120 121 122 123 124 125 |
# File 'lib/remote_resource/request.rb', line 120 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 |
#connection ⇒ Object
30 31 32 |
# File 'lib/remote_resource/request.rb', line 30 def connection resource_klass.connection end |
#connection_options ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/remote_resource/request.rb', line 34 def @combined_connection_options ||= begin default = resource..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 |
#headers ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/remote_resource/request.rb', line 110 def headers @headers ||= begin default_headers = [:default_headers].presence || DEFAULT_HEADERS global_headers = RemoteResource::Base.global_headers.presence || {} headers = [:headers].presence || {} default_headers.merge(global_headers).merge(headers).merge(conditional_headers) end end |
#perform ⇒ Object
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, **) response = RemoteResource::Response.new(connection_response, .merge(request: self, connection_request: connection_response.request)) if response.success? || response.unprocessable_entity? response else raise_http_error(self, response) end end |
#query ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/remote_resource/request.rb', line 67 def query @query ||= begin params = [:params] if params.present? && ![:force_get_params_in_body] RemoteResource::Util.encode_params_to_query(params) else nil end end end |
#request_url ⇒ Object
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 || [:id] base_url = [:base_url].presence || RemoteResource::UrlNamingDetermination.new(resource_klass, ).base_url(id, check_collection_options: true) extension = [:extension] || DEFAULT_EXTENSION "#{base_url}#{extension}" end end |
#timeout_options ⇒ Object
127 128 129 130 131 132 |
# File 'lib/remote_resource/request.rb', line 127 def connecttimeout = [:connecttimeout].presence || DEFAULT_CONNECT_TIMEOUT timeout = [:timeout].presence || DEFAULT_READ_TIMEOUT { connecttimeout: connecttimeout, timeout: timeout } end |