Class: Faraday::ResponseBodySizeLimit::Middleware

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/response_body_size_limit/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, max_size_bytes:) ⇒ Middleware

Returns a new instance of Middleware.



8
9
10
11
12
# File 'lib/faraday/response_body_size_limit/middleware.rb', line 8

def initialize(app, max_size_bytes:)
  super(app)

  @max_size_bytes = max_size_bytes
end

Instance Method Details

#call(env) ⇒ Object

rubocop:disable Metrics/MethodLength



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/faraday/response_body_size_limit/middleware.rb', line 14

def call(env) # rubocop:disable Metrics/MethodLength
  response_body_size = 0
  accumulated_body   = + ""

  env.request.on_data = proc do |chunk, _|
    response_body_size += chunk.bytesize
    accumulated_body   << chunk

    if response_body_size > @max_size_bytes
      raise LimitExceededError,
            "Response body too large, exceeced the configured max size of #{@max_size_bytes} bytes."
    end
  end

  @app.call(env).on_complete do |response_env|
    response_env.body = accumulated_body
  end
end