Class: Rack::Idempotency

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/idempotency.rb,
lib/rack/idempotency/errors.rb,
lib/rack/idempotency/request.rb,
lib/rack/idempotency/version.rb,
lib/rack/idempotency/response.rb,
lib/rack/idempotency/null_store.rb,
lib/rack/idempotency/memory_store.rb,
lib/rack/idempotency/request_storage.rb

Overview

Rack middleware for ensuring mutating endpoints are called at most once.

Any request with an ‘Idempotency-Key` header will store its response in the given cache. When the client retries, it will get the previously cached response.

Defined Under Namespace

Classes: Error, InsecureKeyError, MemoryStore, NullStore, Request, RequestStorage, Response

Constant Summary collapse

VERSION =
"0.4.0".freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, store: NullStore.new) ⇒ Idempotency

Returns a new instance of Idempotency.



19
20
21
22
# File 'lib/rack/idempotency.rb', line 19

def initialize(app, store: NullStore.new)
  @app     = app
  @store   = store
end

Instance Method Details

#call(env) ⇒ Object



24
25
26
27
28
29
# File 'lib/rack/idempotency.rb', line 24

def call(env)
  request = Request.new(env.dup.freeze)
  storage = RequestStorage.new(@store, request)

  storage.read || store_response(storage, env)
end