Class: Rack::IdempotencyKey

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/idempotency_key.rb,
lib/rack/idempotency_key/error.rb,
lib/rack/idempotency_key/request.rb,
lib/rack/idempotency_key/version.rb,
lib/rack/idempotency_key/redis_store.rb,
lib/rack/idempotency_key/memory_store.rb,
lib/rack/idempotency_key/request_hash.rb

Defined Under Namespace

Classes: ConflictError, MemoryStore, RedisStore, Request, RequestHash

Constant Summary collapse

Error =

Base error class for all IdempotencyKey errors

Class.new(StandardError)
StoreError =

Error raised for general store failures

Class.new(Error)
VERSION =
"0.2.2"

Instance Method Summary collapse

Constructor Details

#initialize(app, store: MemoryStore.new) ⇒ IdempotencyKey

Returns a new instance of IdempotencyKey.



21
22
23
24
# File 'lib/rack/idempotency_key.rb', line 21

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

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/rack/idempotency_key.rb', line 26

def call(env)
  request = Request.new(Rack::Request.new(env), store)
  return app.call(env) unless request.allowed?

  handle_request!(request, env)
rescue ConflictError => e
  [409, { "Content-Type" => "text/plain" }, [e.message]]
rescue StoreError => e
  [503, { "Content-Type" => "text/plain" }, [e.message]]
end