Class: Low::Rack::RequestId

Inherits:
Object
  • Object
show all
Defined in:
lib/low/rack/request_id.rb

Overview

‘RequestId` adds a value for env. `RequestLogger`, for one, uses this value to scope the logger it instantiates.

Constant Summary collapse

VALID_REQUEST_ID_REGEX =
/^[a-zA-Z0-9\s\-_]*$/
@@request_id =
0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RequestId

Returns a new instance of RequestId.



16
17
18
19
20
21
# File 'lib/low/rack/request_id.rb', line 16

def initialize(app)
  @app = app

  #generate a request ID (not too worried about uniqueness here).
  @current_request_id = (@@request_id += 1)
end

Class Method Details

.is_valid?(id) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/low/rack/request_id.rb', line 12

def self.is_valid?(id)
  id =~ VALID_REQUEST_ID_REGEX
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/low/rack/request_id.rb', line 23

def call(env)
  unless env['low.request_id']

    # If there is a request_id parameter,
    req = ::Rack::Request.new(env)

    # and it's valid, use it;
    if req['request_id'] and RequestId.is_valid?(req['request_id'])
      env['low.request_id'] = req['request_id']

    # otherwise, use the generated one
    else
      env['low.request_id'] = @current_request_id.to_s
    end
  end

  @app.call(env)
end