Class: Loginator::Middleware::Sinatra
- Inherits:
-
Object
- Object
- Loginator::Middleware::Sinatra
- Defined in:
- lib/loginator/middleware/sinatra.rb
Overview
Middleware for logging transactions with Sinatra.
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#transaction ⇒ Object
readonly
Returns the value of attribute transaction.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, logger) ⇒ Sinatra
constructor
A new instance of Sinatra.
Constructor Details
#initialize(app, logger) ⇒ Sinatra
Returns a new instance of Sinatra.
11 12 13 14 |
# File 'lib/loginator/middleware/sinatra.rb', line 11 def initialize(app, logger) @app = app @logger = logger end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
7 8 9 |
# File 'lib/loginator/middleware/sinatra.rb', line 7 def app @app end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'lib/loginator/middleware/sinatra.rb', line 7 def logger @logger end |
#transaction ⇒ Object (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 |