class Rack::RequestIDPassthrough

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack/request-id-passthrough.rb, line 17
def initialize(app, options = {})
  @app = app
  @headers = RackRequestIDPassthrough.source_headers
  @outgoing_header = RackRequestIDPassthrough.response_headers
  @patch_http = (RackRequestIDPassthrough.http_headers.length > 0)
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/request-id-passthrough.rb, line 24
def call(env)
  Thread.current[:request_id_passthrough] = determine_request_id(env)
  Thread.current[:add_request_id_to_http] = @patch_http

  status, headers, response = @app.call(env)

  populate_headers(headers)
  [status, headers, response]
end

Private Instance Methods

determine_request_id(env) click to toggle source
# File lib/rack/request-id-passthrough.rb, line 36
def determine_request_id(env)
  request_id = SecureRandom.uuid
  matches = {}

  env.each do |k, v|
    @headers.find do |header|
      matches[header] = v if same_header?(header, k)
    end
  end

  @headers.find do |header_name|
    request_id = matches[header_name] if matches[header_name]
  end

  request_id
end
populate_headers(headers) click to toggle source
# File lib/rack/request-id-passthrough.rb, line 59
def populate_headers(headers)
  @outgoing_header.each do |header_name|
    headers[header_name] = Thread.current[:request_id_passthrough]
  end
end
same_header?(header_name, env_key) click to toggle source
# File lib/rack/request-id-passthrough.rb, line 53
def same_header?(header_name, env_key)
  h = header_name.upcase.gsub('_','-').gsub('HTTP-','')
  k = env_key.upcase.gsub('_','-').gsub('HTTP-','')
  h == k
end