Class: Rack::Counter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Counter.



8
9
10
11
12
13
# File 'lib/rack_counter.rb', line 8

def initialize(app, options={})
  @app      = app
  @options  = options

  reset_stats! unless initialized?
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/rack_counter.rb', line 6

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/rack_counter.rb', line 6

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/rack_counter.rb', line 15

def call(env)
  case env['PATH_INFO']
    when '/_stats.json'  then [200, {}, [stats.to_json]]
    when '/_stats/reset' then reset_stats!
    else                      record_hit!; app.call(env)
  end
end