module HTTP::Chainable

Public Instance Methods

accept(type) click to toggle source

Accept the given MIME type(s) @param type

# File lib/http/chainable.rb, line 198
def accept(type)
  headers Headers::ACCEPT => MimeType.normalize(type)
end
auth(value) click to toggle source

Make a request with the given Authorization header @param [#to_s] value Authorization header value

# File lib/http/chainable.rb, line 204
def auth(value)
  headers Headers::AUTHORIZATION => value.to_s
end
basic_auth(opts) click to toggle source

Make a request with the given Basic authorization header @see tools.ietf.org/html/rfc2617 @param [#fetch] opts @option opts [#to_s] :user @option opts [#to_s] :pass

# File lib/http/chainable.rb, line 213
def basic_auth(opts)
  user  = opts.fetch(:user)
  pass  = opts.fetch(:pass)
  creds = "#{user}:#{pass}"

  auth("Basic #{Base64.strict_encode64(creds)}")
end
build_request(*args) click to toggle source

Prepare an HTTP request with the given verb @param (see Client#build_request)

# File lib/http/chainable.rb, line 80
def build_request(*args)
  branch(default_options).build_request(*args)
end
connect(uri, options = {}) click to toggle source

Convert to a transparent TCP/IP tunnel @param uri @option options [Hash]

# File lib/http/chainable.rb, line 61
def connect(uri, options = {})
  request :connect, uri, options
end
cookies(cookies) click to toggle source

Make a request with the given cookies

# File lib/http/chainable.rb, line 187
def cookies(cookies)
  branch default_options.with_cookies(cookies)
end
default_options() click to toggle source

Get options for HTTP @return [HTTP::Options]

# File lib/http/chainable.rb, line 223
def default_options
  @default_options ||= HTTP::Options.new
end
default_options=(opts) click to toggle source

Set options for HTTP @param opts @return [HTTP::Options]

# File lib/http/chainable.rb, line 230
def default_options=(opts)
  @default_options = HTTP::Options.new(opts)
end
delete(uri, options = {}) click to toggle source

Delete a resource @param uri @option options [Hash]

# File lib/http/chainable.rb, line 40
def delete(uri, options = {})
  request :delete, uri, options
end
encoding(encoding) click to toggle source

Force a specific encoding for response body

# File lib/http/chainable.rb, line 192
def encoding(encoding)
  branch default_options.with_encoding(encoding)
end
follow(options = {}) click to toggle source

Make client follow redirects. @param options @return [HTTP::Client] @see Redirector#initialize

# File lib/http/chainable.rb, line 176
def follow(options = {})
  branch default_options.with_follow options
end
get(uri, options = {}) click to toggle source

Get a resource @param uri @option options [Hash]

# File lib/http/chainable.rb, line 19
def get(uri, options = {})
  request :get, uri, options
end
head(uri, options = {}) click to toggle source

Request a get sans response body @param uri @option options [Hash]

# File lib/http/chainable.rb, line 12
def head(uri, options = {})
  request :head, uri, options
end
headers(headers) click to toggle source

Make a request with the given headers @param headers

# File lib/http/chainable.rb, line 182
def headers(headers)
  branch default_options.with_headers(headers)
end
nodelay() click to toggle source

Set TCP_NODELAY on the socket

# File lib/http/chainable.rb, line 235
def nodelay
  branch default_options.with_nodelay(true)
end
options(uri, options = {}) click to toggle source

Return the methods supported on the given URI @param uri @option options [Hash]

# File lib/http/chainable.rb, line 54
def options(uri, options = {})
  request :options, uri, options
end
patch(uri, options = {}) click to toggle source

Apply partial modifications to a resource @param uri @option options [Hash]

# File lib/http/chainable.rb, line 68
def patch(uri, options = {})
  request :patch, uri, options
end
persistent(host, timeout: 5) { |p_client| ... } click to toggle source

@overload persistent(host, timeout: 5)

Flags as persistent
@param  [String] host
@option [Integer] timeout Keep alive timeout
@raise  [Request::Error] if Host is invalid
@return [HTTP::Client] Persistent client

@overload persistent(host, timeout: 5, &block)

Executes given block with persistent client and automatically closes
connection at the end of execution.

@example

    def keys(users)
      HTTP.persistent("https://github.com") do |http|
        users.map { |u| http.get("/#{u}.keys").to_s }
      end
    end

    # same as

    def keys(users)
      http = HTTP.persistent "https://github.com"
      users.map { |u| http.get("/#{u}.keys").to_s }
    ensure
      http.close if http
    end

@yieldparam [HTTP::Client] client Persistent client
@return [Object] result of last expression in the block
# File lib/http/chainable.rb, line 144
def persistent(host, timeout: 5)
  options  = {:keep_alive_timeout => timeout}
  p_client = branch default_options.merge(options).with_persistent host
  return p_client unless block_given?

  yield p_client
ensure
  p_client&.close
end
post(uri, options = {}) click to toggle source

Post to a resource @param uri @option options [Hash]

# File lib/http/chainable.rb, line 26
def post(uri, options = {})
  request :post, uri, options
end
put(uri, options = {}) click to toggle source

Put to a resource @param uri @option options [Hash]

# File lib/http/chainable.rb, line 33
def put(uri, options = {})
  request :put, uri, options
end
request(*args) click to toggle source

Make an HTTP request with the given verb @param (see Client#request)

# File lib/http/chainable.rb, line 74
def request(*args)
  branch(default_options).request(*args)
end
through(*proxy)
Alias for: via
timeout(options) click to toggle source

@overload timeout(options = {})

Adds per operation timeouts to the request
@param [Hash] options
@option options [Float] :read Read timeout
@option options [Float] :write Write timeout
@option options [Float] :connect Connect timeout

@overload timeout(global_timeout)

Adds a global timeout to the full request
@param [Numeric] global_timeout
# File lib/http/chainable.rb, line 93
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
trace(uri, options = {}) click to toggle source

Echo the request back to the client @param uri @option options [Hash]

# File lib/http/chainable.rb, line 47
def trace(uri, options = {})
  request :trace, uri, options
end
use(*features) click to toggle source

Turn on given features. Available features are:

  • auto_inflate

  • auto_deflate

  • instrumentation

  • logging

  • normalize_uri

@param features

# File lib/http/chainable.rb, line 246
def use(*features)
  branch default_options.with_features(features)
end
via(*proxy) click to toggle source

Make a request through an HTTP proxy @param [Array] proxy @raise [Request::Error] if HTTP proxy is invalid

# File lib/http/chainable.rb, line 157
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 default_options.with_proxy(proxy_hash)
end
Also aliased as: through