Class: RecipeScrapers::Http::BodyLimit

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/recipe_scrapers/http/body_limit.rb

Overview

Refuses a response larger than a limit, checked against Content-Length and against the body itself. Registered as :recipe_scrapers_body_limit.

Instance Method Summary collapse

Constructor Details

#initialize(app, bytes:) ⇒ BodyLimit

Returns a new instance of BodyLimit.

Parameters:

  • app (#call)
  • bytes (Integer) —

    the largest body accepted



12
13
14
15
# File 'lib/recipe_scrapers/http/body_limit.rb', line 12

def initialize(app, bytes:)
  super(app)
  @bytes = bytes
end

Instance Method Details

#on_complete(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • env (Faraday::Env)

Raises:



21
22
23
24
25
26
# File 'lib/recipe_scrapers/http/body_limit.rb', line 21

def on_complete(env)
  refuse(declared_size(env).to_i) if over_limit?(declared_size(env))

  actual = env[:body].to_s.bytesize
  refuse(actual) if actual > @bytes
end