Class: Rack::RequestId

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/request_id.rb,
lib/rack-request-id/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ RequestId

Returns a new instance of RequestId.



5
6
7
8
9
# File 'lib/rack/request_id.rb', line 5

def initialize(app, opts = {})
  @app = app
  @storage = opts[:storage] || proc { Thread.current }
  @id_generator = opts[:id_generator] || proc { SecureRandom.hex(16) }
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/rack/request_id.rb', line 11

def call(env)
  storage = @storage.respond_to?(:call) ? @storage.call : @storage
  storage[:request_id] = env['HTTP_X_REQUEST_ID'] || @id_generator.call
  status, headers, body = @app.call(env)
  headers['X-Request-Id'] ||= storage[:request_id]
  [status, headers, body]
end