Module: HTTP::Chainable
Instance Method Summary collapse
-
#accept(type) ⇒ Object
Accept the given MIME type(s).
-
#auth(value) ⇒ Object
Make a request with the given Authorization header.
-
#basic_auth(opts) ⇒ Object
Make a request with the given Basic authorization header.
-
#build_request(*args) ⇒ Object
Prepare an HTTP request with the given verb.
-
#connect(uri, options = {}) ⇒ Object
Convert to a transparent TCP/IP tunnel.
-
#cookies(cookies) ⇒ Object
Make a request with the given cookies.
-
#default_options ⇒ HTTP::Options
Get options for HTTP.
-
#default_options=(opts) ⇒ HTTP::Options
Set options for HTTP.
-
#delete(uri, options = {}) ⇒ Object
Delete a resource.
-
#encoding(encoding) ⇒ Object
Force a specific encoding for response body.
-
#follow(options = {}) ⇒ HTTP::Client
Make client follow redirects.
-
#get(uri, options = {}) ⇒ Object
Get a resource.
-
#head(uri, options = {}) ⇒ Object
Request a get sans response body.
-
#headers(headers) ⇒ Object
Make a request with the given headers.
-
#nodelay ⇒ Object
Set TCP_NODELAY on the socket.
-
#options(uri, options = {}) ⇒ Object
Return the methods supported on the given URI.
-
#patch(uri, options = {}) ⇒ Object
Apply partial modifications to a resource.
- #persistent(host, timeout: 5) ⇒ Object
-
#post(uri, options = {}) ⇒ Object
Post to a resource.
-
#put(uri, options = {}) ⇒ Object
Put to a resource.
-
#request(*args) ⇒ Object
Make an HTTP request with the given verb.
-
#retriable(**options) ⇒ Object
Returns retriable client instance, which retries requests if they failed due to some socket errors or response status is
5xx
. - #timeout(options) ⇒ Object
-
#trace(uri, options = {}) ⇒ Object
Echo the request back to the client.
-
#use(*features) ⇒ Object
Turn on given features.
-
#via(*proxy) ⇒ Object
(also: #through)
Make a request through an HTTP proxy.
Methods included from Base64
Instance Method Details
#accept(type) ⇒ Object
Accept the given MIME type(s)
199 200 201 |
# File 'lib/http/chainable.rb', line 199 def accept(type) headers Headers::ACCEPT => MimeType.normalize(type) end |
#auth(value) ⇒ Object
Make a request with the given Authorization header
205 206 207 |
# File 'lib/http/chainable.rb', line 205 def auth(value) headers Headers::AUTHORIZATION => value.to_s end |
#basic_auth(opts) ⇒ Object
Make a request with the given Basic authorization header
214 215 216 217 218 219 220 |
# File 'lib/http/chainable.rb', line 214 def basic_auth(opts) user = opts.fetch(:user) pass = opts.fetch(:pass) creds = "#{user}:#{pass}" auth("Basic #{encode64(creds)}") end |
#build_request(*args) ⇒ Object
Prepare an HTTP request with the given verb
81 82 83 |
# File 'lib/http/chainable.rb', line 81 def build_request(*args) branch().build_request(*args) end |
#connect(uri, options = {}) ⇒ Object
Convert to a transparent TCP/IP tunnel
62 63 64 |
# File 'lib/http/chainable.rb', line 62 def connect(uri, = {}) request :connect, uri, end |
#cookies(cookies) ⇒ Object
Make a request with the given cookies
188 189 190 |
# File 'lib/http/chainable.rb', line 188 def () branch .() end |
#default_options ⇒ HTTP::Options
Get options for HTTP
224 225 226 |
# File 'lib/http/chainable.rb', line 224 def @default_options ||= HTTP::Options.new end |
#default_options=(opts) ⇒ HTTP::Options
Set options for HTTP
231 232 233 |
# File 'lib/http/chainable.rb', line 231 def (opts) @default_options = HTTP::Options.new(opts) end |
#delete(uri, options = {}) ⇒ Object
Delete a resource
41 42 43 |
# File 'lib/http/chainable.rb', line 41 def delete(uri, = {}) request :delete, uri, end |
#encoding(encoding) ⇒ Object
Force a specific encoding for response body
193 194 195 |
# File 'lib/http/chainable.rb', line 193 def encoding(encoding) branch .with_encoding(encoding) end |
#follow(options = {}) ⇒ HTTP::Client
Make client follow redirects.
177 178 179 |
# File 'lib/http/chainable.rb', line 177 def follow( = {}) branch .with_follow end |
#get(uri, options = {}) ⇒ Object
Get a resource
20 21 22 |
# File 'lib/http/chainable.rb', line 20 def get(uri, = {}) request :get, uri, end |
#head(uri, options = {}) ⇒ Object
Request a get sans response body
13 14 15 |
# File 'lib/http/chainable.rb', line 13 def head(uri, = {}) request :head, uri, end |
#headers(headers) ⇒ Object
Make a request with the given headers
183 184 185 |
# File 'lib/http/chainable.rb', line 183 def headers(headers) branch .with_headers(headers) end |
#nodelay ⇒ Object
Set TCP_NODELAY on the socket
236 237 238 |
# File 'lib/http/chainable.rb', line 236 def nodelay branch .with_nodelay(true) end |
#options(uri, options = {}) ⇒ Object
Return the methods supported on the given URI
55 56 57 |
# File 'lib/http/chainable.rb', line 55 def (uri, = {}) request :options, uri, end |
#patch(uri, options = {}) ⇒ Object
Apply partial modifications to a resource
69 70 71 |
# File 'lib/http/chainable.rb', line 69 def patch(uri, = {}) request :patch, uri, end |
#persistent(host, timeout: 5) ⇒ HTTP::Client #persistent(host, timeout: 5) {|client| ... } ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/http/chainable.rb', line 145 def persistent(host, timeout: 5) = {keep_alive_timeout: timeout} p_client = branch .merge().with_persistent host return p_client unless block_given? yield p_client ensure p_client&.close end |
#post(uri, options = {}) ⇒ Object
Post to a resource
27 28 29 |
# File 'lib/http/chainable.rb', line 27 def post(uri, = {}) request :post, uri, end |
#put(uri, options = {}) ⇒ Object
Put to a resource
34 35 36 |
# File 'lib/http/chainable.rb', line 34 def put(uri, = {}) request :put, uri, end |
#request(*args) ⇒ Object
Make an HTTP request with the given verb
75 76 77 |
# File 'lib/http/chainable.rb', line 75 def request(*args) branch().request(*args) end |
#retriable(**options) ⇒ Object
Returns retriable client instance, which retries requests if they failed
due to some socket errors or response status is 5xx
.
270 271 272 |
# File 'lib/http/chainable.rb', line 270 def retriable(**) Retriable::Client.new(Retriable::Performer.new(), ) end |
#timeout(options = {}) ⇒ Object #timeout(global_timeout) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/http/chainable.rb', line 94 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"] = .delete k end branch .merge( timeout_class: klass, timeout_options: ) end |
#trace(uri, options = {}) ⇒ Object
Echo the request back to the client
48 49 50 |
# File 'lib/http/chainable.rb', line 48 def trace(uri, = {}) request :trace, uri, end |
#use(*features) ⇒ Object
Turn on given features. Available features are:
- auto_inflate
- auto_deflate
- instrumentation
- logging
- normalize_uri
- raise_error
248 249 250 |
# File 'lib/http/chainable.rb', line 248 def use(*features) branch .with_features(features) end |
#via(*proxy) ⇒ Object Also known as: through
Make a request through an HTTP proxy
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/http/chainable.rb', line 158 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(RequestError, "invalid HTTP proxy: #{proxy_hash}") unless (2..5).cover?(proxy_hash.keys.size) branch .with_proxy(proxy_hash) end |