class Wayfarer::HTTPAdapters::NetHTTPAdapter

A singleton adapter for net-http-persistent. @api private

Constants

MalformedRedirectURI
MalformedURI
MaximumRedirectCountReached
RECOGNIZED_URI_TYPES

Supported standard lib classes

Attributes

request_header_overrides[RW]

Public Class Methods

instance(config = Wayfarer.config) click to toggle source

TODO: Remove default parameter value

# File lib/wayfarer/http_adapters/net_http_adapter.rb, line 25
def self.instance(config = Wayfarer.config)
  @@instance ||= new(config)
end

Private Class Methods

new(config) click to toggle source
# File lib/wayfarer/http_adapters/net_http_adapter.rb, line 29
def initialize(config)
  @config = config
  @conn = Net::HTTP::Persistent.new("wayfarer-#{SecureRandom.uuid}")
end

Public Instance Methods

fetch(uri, redirects_followed = 0) click to toggle source

Fetches a page. @return [Page] @raise [MalformedURI] if the URI is not supported. @raise [MalformedRedirectURI] if a redirection URI is not supported. @raise [MaximumRedirectCountReached] if too many redirections are encountered.

# File lib/wayfarer/http_adapters/net_http_adapter.rb, line 43
def fetch(uri, redirects_followed = 0)
  if !RECOGNIZED_URI_TYPES.include?(uri.class)
    raise _ = if redirects_followed.positive?
                MalformedRedirectURI
              else
                MalformedURI
              end
  elsif redirects_followed > @config.max_http_redirects
    raise MaximumRedirectCountReached
  end

  res = @conn.request(uri)

  if res.is_a? Net::HTTPRedirection
    redirect_uri = URI(res["location"])
    return fetch(redirect_uri, redirects_followed + 1)
  end

  Page.new(
    uri: uri,
    status_code: res.code.to_i,
    body: res.body,
    headers: res.to_hash
  )
rescue SocketError
  raise MalformedURI
end
free() click to toggle source

Shuts down all connections.

# File lib/wayfarer/http_adapters/net_http_adapter.rb, line 72
def free
  @conn.shutdown
end