Module: HTTP::Session::Configurable

Included in:
HTTP::Session
Defined in:
lib/http/session/configurable.rb

Overview

Provides the same configure API interfaces as HTTP::Client.

Mostly borrowed from http/lib/http/chainable.rb

Instance Method Summary collapse

Instance Method Details

#accept(type) ⇒ Session

Accept the given MIME type(s).

Parameters:

  • type

Returns:



94
95
96
# File 'lib/http/session/configurable.rb', line 94

def accept(type)
  headers HTTP::Headers::ACCEPT => HTTP::MimeType.normalize(type)
end

#auth(value) ⇒ Session

Make a request with the given Authorization header.

Parameters:

  • value (#to_s)

    Authorization header value

Returns:



102
103
104
# File 'lib/http/session/configurable.rb', line 102

def auth(value)
  headers HTTP::Headers::AUTHORIZATION => value.to_s
end

#basic_auth(opts) ⇒ Session

Make a request with the given Basic authorization header.

Parameters:

  • opts (#fetch)

Options Hash (opts):

  • :user (#to_s)
  • :pass (#to_s)

Returns:

See Also:



113
114
115
116
117
118
119
# File 'lib/http/session/configurable.rb', line 113

def basic_auth(opts)
  user = opts.fetch(:user)
  pass = opts.fetch(:pass)
  creds = "#{user}:#{pass}"

  auth("Basic #{Base64.strict_encode64(creds)}")
end

#cookies(cookies) ⇒ Session

Make a request with the given cookies.

Parameters:

  • cookies

Returns:



78
79
80
# File 'lib/http/session/configurable.rb', line 78

def cookies(cookies)
  branch default_options.with_cookies(cookies)
end

#encoding(encoding) ⇒ Session

Force a specific encoding for response body.

Parameters:

  • encoding

Returns:



86
87
88
# File 'lib/http/session/configurable.rb', line 86

def encoding(encoding)
  branch default_options.with_encoding(encoding)
end

#follow(options = {}) ⇒ Session

Make client follow redirects.

Parameters:

  • options (defaults to: {})

Returns:



62
63
64
# File 'lib/http/session/configurable.rb', line 62

def follow(options = {})
  branch default_options.with_follow options
end

#headers(headers) ⇒ Session

Make a request with the given headers.

Parameters:

  • headers

Returns:



70
71
72
# File 'lib/http/session/configurable.rb', line 70

def headers(headers)
  branch default_options.with_headers(headers)
end

#nodelaySession

Set TCP_NODELAY on the socket.

Returns:



124
125
126
# File 'lib/http/session/configurable.rb', line 124

def nodelay
  branch default_options.with_nodelay(true)
end

#timeout(options = {}) ⇒ Session #timeout(global_timeout) ⇒ Session

Set timeout on request.

Overloads:

  • #timeout(options = {}) ⇒ Session

    Adds per operation timeouts to the request.

    Parameters:

    • options (Hash) (defaults to: {})

    Options Hash (options):

    • :read (Float)

      Read timeout

    • :write (Float)

      Write timeout

    • :connect (Float)

      Connect timeout

    Returns:

  • #timeout(global_timeout) ⇒ Session

    Adds a global timeout to the full request.

    Parameters:

    • global_timeout (Numeric)

    Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/http/session/configurable.rb', line 20

def timeout(options)
  klass, options =
    case options
    when Numeric then [HTTP::Timeout::Global, {global: options}]
    when Hash then [HTTP::Timeout::PerOperation, options.dup]
    when :null then [HTTP::Timeout::Null, {}]
    else raise ArgumentError, "Use `.timeout(global_timeout_in_seconds)` or `.timeout(connect: x, write: y, read: z)`."
    end

  %i[global read write connect].each do |k|
    next unless options.key? k
    options["#{k}_timeout".to_sym] = options.delete k
  end

  branch default_options.merge(
    timeout_class: klass,
    timeout_options: options
  )
end

#use(*features) ⇒ Session

Turn on given features.

Returns:



131
132
133
# File 'lib/http/session/configurable.rb', line 131

def use(*features)
  branch default_options.with_features(features)
end

#via(*proxy) ⇒ Session Also known as: through

Make a request through an HTTP proxy.

Parameters:

  • proxy (Array)

Returns:

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/http/session/configurable.rb', line 44

def via(*proxy)
  proxy_hash = {}
  proxy_hash[:proxy_address] = proxy[0] if proxy[0].is_a?(String)
  proxy_hash[:proxy_port] = proxy[1] if proxy[1].is_a?(Integer)
  proxy_hash[:proxy_username] = proxy[2] if proxy[2].is_a?(String)
  proxy_hash[:proxy_password] = proxy[3] if proxy[3].is_a?(String)
  proxy_hash[:proxy_headers] = proxy[2] if proxy[2].is_a?(Hash)
  proxy_hash[:proxy_headers] = proxy[4] if proxy[4].is_a?(Hash)
  raise ArgumentError, "invalid HTTP proxy: #{proxy_hash}" unless (2..5).cover?(proxy_hash.keys.size)

  branch default_options.with_proxy(proxy_hash)
end