Class: Loginator::Middleware::Sinatra

Inherits:
Object
  • Object
show all
Defined in:
lib/loginator/middleware/sinatra.rb

Overview

Middleware for logging transactions with Sinatra.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, logger) ⇒ Sinatra

Returns a new instance of Sinatra.

Parameters:

  • app (Rack::App)

    #Rack::App being passed through middleware chain

  • logger (IO)

    #IO object where log messages will be sent via puts()



11
12
13
14
# File 'lib/loginator/middleware/sinatra.rb', line 11

def initialize(app, logger)
  @app = app
  @logger = logger
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



7
8
9
# File 'lib/loginator/middleware/sinatra.rb', line 7

def app
  @app
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/loginator/middleware/sinatra.rb', line 7

def logger
  @logger
end

#transactionObject (readonly)

Returns the value of attribute transaction.



7
8
9
# File 'lib/loginator/middleware/sinatra.rb', line 7

def transaction
  @transaction
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/loginator/middleware/sinatra.rb', line 16

def call(env)
  uuid = env['X-REQUEST-ID'] ||= SecureRandom.uuid
  req = Rack::Request.new(env)
  @transaction = Loginator::Transaction.new(uuid: uuid)
  transaction.begin do |txn|
    txn.path = env['PATH_INFO']
    txn.request = read_body(req)
    status, headers, body = @app.call(env)
    txn.status = status
    txn.response = body
    [status, headers, body]
  end
ensure
  logger.puts(transaction.to_json)
end