Class: Springcm::Middleware::RateLimit

Inherits:
Object
  • Object
show all
Defined in:
lib/springcm-sdk/middleware/rate_limit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RateLimit.



6
7
8
9
# File 'lib/springcm-sdk/middleware/rate_limit.rb', line 6

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/springcm-sdk/middleware/rate_limit.rb', line 4

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/springcm-sdk/middleware/rate_limit.rb', line 11

def call(env)
  @app.call(env).on_complete do |response_env|
    return response_env if response_env[:response].env.response_headers["Content-Type"] != "application/json"
    body = JSON.parse(response_env[:body])
    if body.fetch("Error", {}).fetch("ErrorCode", nil) == 103
      raise Springcm::RateLimitExceededError.new
    end
  end
end