Class: Rack::IdempotencyKey::Request
- Inherits:
-
Object
- Object
- Rack::IdempotencyKey::Request
- Defined in:
- lib/rack/idempotency_key/request.rb
Constant Summary collapse
- DEFAULT_LOCK_TTL =
seconds
60
Instance Method Summary collapse
-
#allowed? ⇒ Boolean
Checks if the
Idempotency-Keyheader is present, if the HTTP request method is allowed. -
#allowed_method? ⇒ Boolean
Checks if the HTTP request method is non-idempotent by design.
- #cache!(response) ⇒ Object
- #cached_response! ⇒ Object
-
#idempotency_key ⇒ String?
Fetches the Idempotency-Key header value from the request headers.
-
#idempotency_key? ⇒ Boolean
Checks if the given request has the Idempotency-Key header.
-
#initialize(request, store) ⇒ Request
constructor
A new instance of Request.
- #locked! ⇒ Object
Constructor Details
#initialize(request, store) ⇒ Request
Returns a new instance of Request.
12 13 14 15 16 |
# File 'lib/rack/idempotency_key/request.rb', line 12 def initialize(request, store) @request = request @request_id = Rack::IdempotencyKey::RequestHash.new(request).id @store = store end |
Instance Method Details
#allowed? ⇒ Boolean
Checks if the Idempotency-Key header is present, if the HTTP request method is allowed.
21 22 23 |
# File 'lib/rack/idempotency_key/request.rb', line 21 def allowed? idempotency_key? && allowed_method? end |
#allowed_method? ⇒ Boolean
Checks if the HTTP request method is non-idempotent by design.
49 50 51 |
# File 'lib/rack/idempotency_key/request.rb', line 49 def allowed_method? %w[POST PATCH CONNECT].include? request.request_method end |
#cache!(response) ⇒ Object
41 42 43 44 |
# File 'lib/rack/idempotency_key/request.rb', line 41 def cache!(response) status, = response store.set(cache_key, response) if status != 400 end |
#cached_response! ⇒ Object
25 26 27 28 29 |
# File 'lib/rack/idempotency_key/request.rb', line 25 def cached_response! store.get(cache_key).tap do |response| response[1]["Idempotent-Replayed"] = true unless response.nil? end end |
#idempotency_key ⇒ String?
Fetches the Idempotency-Key header value from the request headers
63 64 65 |
# File 'lib/rack/idempotency_key/request.rb', line 63 def idempotency_key request.get_header "HTTP_IDEMPOTENCY_KEY" end |
#idempotency_key? ⇒ Boolean
Checks if the given request has the Idempotency-Key header
56 57 58 |
# File 'lib/rack/idempotency_key/request.rb', line 56 def idempotency_key? request.has_header? "HTTP_IDEMPOTENCY_KEY" end |
#locked! ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/rack/idempotency_key/request.rb', line 31 def locked! store.set(lock_key, 1, ttl: DEFAULT_LOCK_TTL) begin yield ensure store.unset(lock_key) end end |