Class: Elastomer::Middleware::LimitSize

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/elastomer/middleware/limit_size.rb

Overview

Request middleware that raises an exception if the request body exceeds a ‘max_request_size`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, options = {}) ⇒ LimitSize

Returns a new instance of LimitSize.



8
9
10
11
# File 'lib/elastomer/middleware/limit_size.rb', line 8

def initialize(app = nil, options = {})
  super(app)
  @max_request_size = options.fetch(:max_request_size)
end

Instance Attribute Details

#max_request_sizeObject (readonly)

Returns the value of attribute max_request_size.



13
14
15
# File 'lib/elastomer/middleware/limit_size.rb', line 13

def max_request_size
  @max_request_size
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/elastomer/middleware/limit_size.rb', line 15

def call(env)
  if body = env[:body]
    if body.is_a?(String) && body.bytesize > max_request_size
      raise ::Elastomer::Client::RequestSizeError,
        "Request of size `#{body.bytesize}` exceeds the maximum requst size: #{max_request_size}"
    end
  end
  @app.call(env)
end