Class: Peastash::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app, before_block = nil, after_block = nil) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
8
9
# File 'lib/peastash/middleware.rb', line 5

def initialize(app, before_block = nil, after_block = nil)
  @app = app
  define_singleton_method(:before_block, before_block || ->(_, _) {})
  define_singleton_method(:after_block, after_block || ->(_, _) {})
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/peastash/middleware.rb', line 11

def call(env)
  response = nil
  Peastash.with_instance.log do
    start = Time.now

    Peastash.safely do
      before_block(env, response)

      # Setting this before calling the next middleware so it can be overriden
      request = Rack::Request.new(env)
      if env.has_key? 'HTTP_X_REQUEST_START'
        Peastash.with_instance.store[:time_in_queue] = ((Time.now.to_f - env['HTTP_X_REQUEST_START'].to_f) * 1000.0).round(2)
      end
      Peastash.with_instance.store[:ip] = request.ip
    end

    response = @app.call(env)

    Peastash.safely do
      Peastash.with_instance.store[:duration] = ((Time.now - start) * 1000.0).round(2)
      Peastash.with_instance.store[:status] = response.first

      after_block(env, response)
    end
  end

  response
end