Class: TallyCounter::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.

Parameters:

  • app
    • a rack compliant app

  • options (defaults to: {})

Options Hash (options):

  • :interval (Object)

    time in seconds for window granularity

  • :logger (Object)

    a logger instance

  • :namespace (Object)

    optional redis key namespace, like ‘app_name’

  • :redis (Object)

    a redis instance

  • :timeout (Object)

    seconds before timing out a redis command



56
57
58
59
60
61
62
63
# File 'lib/tally_counter.rb', line 56

def initialize(app, options = {})
  @app       = app
  @interval  = options[:interval] || 300
  @logger    = options[:logger] || Logger.new($stdout)
  @namespace = options[:namespace]
  @redis     = options[:redis] || Redis.current
  @timeout   = options[:timeout] || 0.007
end

Instance Method Details

#call(env) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/tally_counter.rb', line 65

def call(env)
  tuple = @app.call(env)

  unless tuple[1].delete('X-Tally-Counter-Skip')
    req = Rack::Request.new(env)
    track_ip(req.ip)
  end

  tuple
end