Class: RecipeScrapers::Http::Adapter

Inherits:
Faraday::Adapter::NetHttp
  • Object
show all
Defined in:
lib/recipe_scrapers/http/adapter.rb

Overview

Faraday's net/http adapter, connecting to the addresses AddressGuard checked instead of resolving the host again. It tries the pinned addresses in order and moves on when one does not answer. The host name still goes into the TLS handshake and the Host header. Registered as :recipe_scrapers_net_http, the default Configuration#adapter.

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Faraday::Response

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)

Returns:

  • (Faraday::Response)

Raises:

  • (Faraday::ConnectionFailed) —

    when no pinned address answers



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/recipe_scrapers/http/adapter.rb', line 24

def call(env)
  addresses = Array(env[:request]&.context&.dig(PIN_KEY))
  return super if addresses.empty?

  addresses.each_with_index do |address, index|
    env[ATTEMPT_KEY] = address
    return super(env)
  rescue Faraday::ConnectionFailed
    raise if index == addresses.size - 1
  end
end

#net_http_connection(env) ⇒ Net::HTTP

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.

Returns the connection, set to the pinned address unless a proxy is in use.

Parameters:

  • env (Faraday::Env)

Returns:

  • (Net::HTTP) —

    the connection, set to the pinned address unless a proxy is in use



40
41
42
43
44
45
# File 'lib/recipe_scrapers/http/adapter.rb', line 40

def net_http_connection(env)
  super.tap do |http|
    address = env[ATTEMPT_KEY] || Array(env[:request]&.context&.dig(PIN_KEY)).first
    http.ipaddr = address if address && !http.proxy?
  end
end