Sha256: 74921be38f70dd405c7fb10e442ff378690d119d290ac4b64c76fa15434e1b75

Contents?: true

Size: 962 Bytes

Versions: 3

Compression:

Stored size: 962 Bytes

Contents

require 'socket'

class Peastash
  class Middleware
    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

    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)
          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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
peastash-0.1.0 lib/peastash/middleware.rb
peastash-0.0.9 lib/peastash/middleware.rb
peastash-0.0.8 lib/peastash/middleware.rb