Class: Parse::Request
- Inherits:
-
Object
- Object
- Parse::Request
- Defined in:
- lib/parse/client/request.rb
Overview
This class represents a Parse request.
Class Attribute Summary collapse
-
.enable_request_id ⇒ Boolean
Whether to automatically generate request IDs for idempotency.
-
.idempotent_methods ⇒ Array<Symbol>
HTTP methods that should include request IDs.
-
.request_id_header ⇒ String
The header name to use for request IDs.
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
- #cache ⇒ Boolean
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#method ⇒ Object
Returns the value of attribute method.
-
#opts ⇒ Hash
A set of options for this request.
-
#path ⇒ Object
Returns the value of attribute path.
-
#request_id ⇒ String
Unique identifier for this request to enable idempotency.
Class Method Summary collapse
-
.configure_idempotency(enabled: true, methods: [:post, :put, :patch], header: "X-Parse-Request-Id") ⇒ Object
Configures idempotency settings.
-
.disable_idempotency! ⇒ Object
Disables request ID generation globally.
-
.enable_idempotency!(methods: [:post, :put, :patch], header: "X-Parse-Request-Id") ⇒ Object
Enables request ID generation globally.
Instance Method Summary collapse
- #==(r) ⇒ Boolean
-
#as_json ⇒ Hash
JSON encoded hash.
-
#idempotent? ⇒ Boolean
Checks if this request has idempotency enabled.
-
#initialize(method, uri, body: nil, headers: nil, opts: {}) ⇒ Request
constructor
Creates a new request.
-
#query ⇒ Hash
The parameters of this request if the HTTP method is GET.
-
#signature ⇒ Hash
Signature provies a way for us to compare different requests objects.
- #to_s ⇒ String
-
#with_idempotency(custom_id = nil) ⇒ self
Enables idempotency for this specific request.
-
#without_idempotency ⇒ self
Disables idempotency for this specific request.
Constructor Details
#initialize(method, uri, body: nil, headers: nil, opts: {}) ⇒ Request
Creates a new request
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/parse/client/request.rb', line 64 def initialize(method, uri, body: nil, headers: nil, opts: {}) @tag = 0 method = method.downcase.to_sym unless method == :get || method == :put || method == :post || method == :delete raise ArgumentError, "Invalid method #{method} for request : '#{uri}'" end self.method = method self.path = uri self.body = body self.headers = headers || {} self.opts = opts || {} # Handle request ID for idempotency setup_request_id end |
Class Attribute Details
.enable_request_id ⇒ Boolean
Returns whether to automatically generate request IDs for idempotency.
40 41 42 |
# File 'lib/parse/client/request.rb', line 40 def enable_request_id @enable_request_id end |
.idempotent_methods ⇒ Array<Symbol>
Returns HTTP methods that should include request IDs.
48 49 50 |
# File 'lib/parse/client/request.rb', line 48 def idempotent_methods @idempotent_methods end |
.request_id_header ⇒ String
Returns the header name to use for request IDs.
44 45 46 |
# File 'lib/parse/client/request.rb', line 44 def request_id_header @request_id_header end |
Instance Attribute Details
#cache ⇒ Boolean
26 |
# File 'lib/parse/client/request.rb', line 26 attr_accessor :method, :path, :body, :headers, :opts, :cache |
#headers ⇒ Object
Returns the value of attribute headers.
26 27 28 |
# File 'lib/parse/client/request.rb', line 26 def headers @headers end |
#method ⇒ Object
Returns the value of attribute method.
|
|
# File 'lib/parse/client/request.rb', line 11
|
#opts ⇒ Hash
Returns a set of options for this request.
26 |
# File 'lib/parse/client/request.rb', line 26 attr_accessor :method, :path, :body, :headers, :opts, :cache |
#request_id ⇒ String
Returns unique identifier for this request to enable idempotency.
34 35 36 |
# File 'lib/parse/client/request.rb', line 34 def request_id @request_id end |
Class Method Details
.configure_idempotency(enabled: true, methods: [:post, :put, :patch], header: "X-Parse-Request-Id") ⇒ Object
Configures idempotency settings
227 228 229 230 231 |
# File 'lib/parse/client/request.rb', line 227 def self.configure_idempotency(enabled: true, methods: [:post, :put, :patch], header: "X-Parse-Request-Id") self.enable_request_id = enabled self.idempotent_methods = methods self.request_id_header = header end |
.disable_idempotency! ⇒ Object
Disables request ID generation globally
219 220 221 |
# File 'lib/parse/client/request.rb', line 219 def self.disable_idempotency! self.enable_request_id = false end |
.enable_idempotency!(methods: [:post, :put, :patch], header: "X-Parse-Request-Id") ⇒ Object
Enables request ID generation globally
212 213 214 215 216 |
# File 'lib/parse/client/request.rb', line 212 def self.enable_idempotency!(methods: [:post, :put, :patch], header: "X-Parse-Request-Id") self.enable_request_id = true self.idempotent_methods = methods self.request_id_header = header end |
Instance Method Details
#==(r) ⇒ Boolean
93 94 95 96 |
# File 'lib/parse/client/request.rb', line 93 def ==(r) return false unless r.is_a?(Request) @method == r.method && @path == r.path && @body == r.body && @headers == r.headers end |
#as_json ⇒ Hash
Returns JSON encoded hash.
88 89 90 |
# File 'lib/parse/client/request.rb', line 88 def as_json signature.as_json end |
#idempotent? ⇒ Boolean
Checks if this request has idempotency enabled
203 204 205 |
# File 'lib/parse/client/request.rb', line 203 def idempotent? @request_id.present? && @headers[self.class.request_id_header].present? end |
#query ⇒ Hash
The parameters of this request if the HTTP method is GET.
83 84 85 |
# File 'lib/parse/client/request.rb', line 83 def query body if @method == :get end |
#signature ⇒ Hash
Signature provies a way for us to compare different requests objects. Two requests objects are the same if they have the same signature.
101 102 103 |
# File 'lib/parse/client/request.rb', line 101 def signature { method: @method.upcase, path: @path, body: @body } end |
#to_s ⇒ String
111 112 113 |
# File 'lib/parse/client/request.rb', line 111 def to_s "#{@method.to_s.upcase} #{@path}" end |
#with_idempotency(custom_id = nil) ⇒ self
Enables idempotency for this specific request
185 186 187 188 189 190 |
# File 'lib/parse/client/request.rb', line 185 def with_idempotency(custom_id = nil) @opts[:idempotent] = true @opts[:request_id] = custom_id if custom_id setup_request_id self end |
#without_idempotency ⇒ self
Disables idempotency for this specific request
194 195 196 197 198 199 |
# File 'lib/parse/client/request.rb', line 194 def without_idempotency @opts[:idempotent] = false @request_id = nil @headers.delete(self.class.request_id_header) self end |