Class: Wayfarer::HTTPAdapters::NetHTTPAdapter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/wayfarer/http_adapters/net_http_adapter.rb

Overview

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

A singleton adapter for net-http-persistent.

Constant Summary collapse

RECOGNIZED_URI_TYPES =

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

Supported standard lib classes

[
  URI::HTTP,
  URI::HTTPS
].freeze
MalformedURI =

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

Class.new(StandardError)
MalformedRedirectURI =

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

Class.new(StandardError)
MaximumRedirectCountReached =

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

Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ NetHTTPAdapter

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 a new instance of NetHTTPAdapter.


29
30
31
32
# 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

Instance Attribute Details

#request_header_overridesObject

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.


22
23
24
# File 'lib/wayfarer/http_adapters/net_http_adapter.rb', line 22

def request_header_overrides
  @request_header_overrides
end

Class Method Details

.instance(config = Wayfarer.config) ⇒ 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.

TODO: Remove default parameter value


25
26
27
# File 'lib/wayfarer/http_adapters/net_http_adapter.rb', line 25

def self.instance(config = Wayfarer.config)
  @@instance ||= new(config)
end

Instance Method Details

#fetch(uri, redirects_followed = 0) ⇒ Page

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.

Fetches a page. encountered.

Returns:

Raises:


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

#freeObject

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.

Shuts down all connections.


72
73
74
# File 'lib/wayfarer/http_adapters/net_http_adapter.rb', line 72

def free
  @conn.shutdown
end