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
-
#accept(type) ⇒ Session
Accept the given MIME type(s).
-
#auth(value) ⇒ Session
Make a request with the given Authorization header.
-
#basic_auth(opts) ⇒ Session
Make a request with the given Basic authorization header.
-
#cookies(cookies) ⇒ Session
Make a request with the given cookies.
-
#encoding(encoding) ⇒ Session
Force a specific encoding for response body.
-
#follow(options = {}) ⇒ Session
Make client follow redirects.
-
#headers(headers) ⇒ Session
Make a request with the given headers.
-
#nodelay ⇒ Session
Set TCP_NODELAY on the socket.
-
#timeout(options) ⇒ Object
Set timeout on request.
-
#use(*features) ⇒ Session
Turn on given features.
-
#via(*proxy) ⇒ Session
(also: #through)
Make a request through an HTTP proxy.
Instance Method Details
#accept(type) ⇒ Session
Accept the given MIME type(s).
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.
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.
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.
78 79 80 |
# File 'lib/http/session/configurable.rb', line 78 def () branch .() end |
#encoding(encoding) ⇒ Session
Force a specific encoding for response body.
86 87 88 |
# File 'lib/http/session/configurable.rb', line 86 def encoding(encoding) branch .with_encoding(encoding) end |
#follow(options = {}) ⇒ Session
Make client follow redirects.
62 63 64 |
# File 'lib/http/session/configurable.rb', line 62 def follow( = {}) branch .with_follow end |
#headers(headers) ⇒ Session
Make a request with the given headers.
70 71 72 |
# File 'lib/http/session/configurable.rb', line 70 def headers(headers) branch .with_headers(headers) end |
#nodelay ⇒ Session
Set TCP_NODELAY on the socket.
124 125 126 |
# File 'lib/http/session/configurable.rb', line 124 def nodelay branch .with_nodelay(true) end |
#timeout(options = {}) ⇒ Session #timeout(global_timeout) ⇒ Session
Set timeout on request.
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() klass, = case when Numeric then [HTTP::Timeout::Global, {global: }] when Hash then [HTTP::Timeout::PerOperation, .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 .key? k ["#{k}_timeout".to_sym] = .delete k end branch .merge( timeout_class: klass, timeout_options: ) end |
#use(*features) ⇒ Session
Turn on given features.
131 132 133 |
# File 'lib/http/session/configurable.rb', line 131 def use(*features) branch .with_features(features) end |
#via(*proxy) ⇒ Session Also known as: through
Make a request through an HTTP proxy.
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 .with_proxy(proxy_hash) end |