class Rack::Idempotency

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.

Constants

VERSION

Public Class Methods

new(app, store: NullStore.new) click to toggle source
# File lib/rack/idempotency.rb, line 19
def initialize(app, store: NullStore.new)
  @app     = app
  @store   = store
end

Public Instance Methods

call(env) click to toggle source
# 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

Private Instance Methods

store_response(storage, env) click to toggle source
# File lib/rack/idempotency.rb, line 33
def store_response(storage, env)
  response = Response.new(*@app.call(env))

  storage.write(response) if response.success?

  response.to_a
end