Class: Rack::API::Middleware::Limit

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/api/middleware/limit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Limit.



7
8
9
10
11
12
13
14
# File 'lib/rack/api/middleware/limit.rb', line 7

def initialize(app, options = {})
  @app = app
  @options = {
    :limit  => 60,
    :key    => "REMOTE_ADDR",
    :with   => Redis.new
  }.merge(options)
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/rack/api/middleware/limit.rb', line 5

def env
  @env
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/rack/api/middleware/limit.rb', line 5

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rack/api/middleware/limit.rb', line 16

def call(env)
  @env = env

  if authorized?
    @app.call(env)
  else
    [503, {"Content-Type" => "text/plain"}, ["Over Rate Limit."]]
  end
rescue Exception => e
  @app.call(env)
end