Module: HTTP::Chainable

Included in:
HTTP, Client
Defined in:
lib/http/chainable.rb

Instance Method Summary collapse

Instance Method Details

#accept(type) ⇒ Object

Accept the given MIME type(s)

Parameters:

  • type


200
201
202
# File 'lib/http/chainable.rb', line 200

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

#auth(value) ⇒ Object

Make a request with the given Authorization header

Parameters:

  • value (#to_s)

    Authorization header value



206
207
208
# File 'lib/http/chainable.rb', line 206

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

#basic_auth(opts) ⇒ Object

Make a request with the given Basic authorization header

Parameters:

  • opts (#fetch)

Options Hash (opts):

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

See Also:



215
216
217
218
219
220
# File 'lib/http/chainable.rb', line 215

def basic_auth(opts)
  user = opts.fetch :user
  pass = opts.fetch :pass

  auth("Basic " + Base64.strict_encode64("#{user}:#{pass}"))
end

#build_request(verb, uri, options = {}) ⇒ Object

Prepare an HTTP request with the given verb

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


84
85
86
# File 'lib/http/chainable.rb', line 84

def build_request(verb, uri, options = {}) # rubocop:disable Style/OptionHash
  branch(options).build_request verb, uri
end

#connect(uri, options = {}) ⇒ Object

Convert to a transparent TCP/IP tunnel

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


61
62
63
# File 'lib/http/chainable.rb', line 61

def connect(uri, options = {}) # rubocop:disable Style/OptionHash
  request :connect, uri, options
end

#cookies(cookies) ⇒ Object

Make a request with the given cookies



189
190
191
# File 'lib/http/chainable.rb', line 189

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

#default_optionsHTTP::Options

Get options for HTTP

Returns:



224
225
226
# File 'lib/http/chainable.rb', line 224

def default_options
  @default_options ||= HTTP::Options.new
end

#default_options=(opts) ⇒ HTTP::Options

Set options for HTTP

Parameters:

  • opts

Returns:



231
232
233
# File 'lib/http/chainable.rb', line 231

def default_options=(opts)
  @default_options = HTTP::Options.new(opts)
end

#delete(uri, options = {}) ⇒ Object

Delete a resource

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


40
41
42
# File 'lib/http/chainable.rb', line 40

def delete(uri, options = {}) # rubocop:disable Style/OptionHash
  request :delete, uri, options
end

#encoding(encoding) ⇒ Object

Force a specific encoding for response body



194
195
196
# File 'lib/http/chainable.rb', line 194

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

#follow(options = {}) ⇒ HTTP::Client

Make client follow redirects.

Parameters:

  • opts

Returns:

See Also:



178
179
180
# File 'lib/http/chainable.rb', line 178

def follow(options = {}) # rubocop:disable Style/OptionHash
  branch default_options.with_follow options
end

#get(uri, options = {}) ⇒ Object

Get a resource

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


19
20
21
# File 'lib/http/chainable.rb', line 19

def get(uri, options = {}) # rubocop:disable Style/OptionHash
  request :get, uri, options
end

#head(uri, options = {}) ⇒ Object

Request a get sans response body

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


12
13
14
# File 'lib/http/chainable.rb', line 12

def head(uri, options = {}) # rubocop:disable Style/OptionHash
  request :head, uri, options
end

#headers(headers) ⇒ Object

Make a request with the given headers

Parameters:

  • headers


184
185
186
# File 'lib/http/chainable.rb', line 184

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

#nodelayObject

Set TCP_NODELAY on the socket



236
237
238
# File 'lib/http/chainable.rb', line 236

def nodelay
  branch default_options.with_nodelay(true)
end

#options(uri, options = {}) ⇒ Object

Return the methods supported on the given URI

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


54
55
56
# File 'lib/http/chainable.rb', line 54

def options(uri, options = {}) # rubocop:disable Style/OptionHash
  request :options, uri, options
end

#patch(uri, options = {}) ⇒ Object

Apply partial modifications to a resource

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


68
69
70
# File 'lib/http/chainable.rb', line 68

def patch(uri, options = {}) # rubocop:disable Style/OptionHash
  request :patch, uri, options
end

#persistent(host, timeout: 5) ⇒ HTTP::Client #persistent(host, timeout: 5) {|client| ... } ⇒ Object

Overloads:

  • #persistent(host, timeout: 5) ⇒ HTTP::Client

    Flags as persistent

    Parameters:

    • host (String)

    Returns:

    Raises:

    • (Request::Error)

      if Host is invalid

  • #persistent(host, timeout: 5) {|client| ... } ⇒ Object

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

    Examples:

    
    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

    Yield Parameters:

    Returns:

    • (Object)

      result of last expression in the block



147
148
149
150
151
152
153
154
# File 'lib/http/chainable.rb', line 147

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 if p_client
end

#post(uri, options = {}) ⇒ Object

Post to a resource

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


26
27
28
# File 'lib/http/chainable.rb', line 26

def post(uri, options = {}) # rubocop:disable Style/OptionHash
  request :post, uri, options
end

#put(uri, options = {}) ⇒ Object

Put to a resource

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


33
34
35
# File 'lib/http/chainable.rb', line 33

def put(uri, options = {}) # rubocop:disable Style/OptionHash
  request :put, uri, options
end

#request(verb, uri, options = {}) ⇒ Object

Make an HTTP request with the given verb

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


76
77
78
# File 'lib/http/chainable.rb', line 76

def request(verb, uri, options = {}) # rubocop:disable Style/OptionHash
  branch(options).request verb, uri
end

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

Overloads:

  • #timeout(options = {}) ⇒ Object

    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

  • #timeout(global_timeout) ⇒ Object

    Adds a global timeout to the full request

    Parameters:

    • global_timeout (Numeric)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/http/chainable.rb', line 97

def timeout(options)
  klass, options = case options
                   when Numeric then [HTTP::Timeout::Global, {:global => options}]
                   when Hash    then [HTTP::Timeout::PerOperation, options]
                   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 = {}) ⇒ Object

Echo the request back to the client

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


47
48
49
# File 'lib/http/chainable.rb', line 47

def trace(uri, options = {}) # rubocop:disable Style/OptionHash
  request :trace, uri, options
end

#use(*features) ⇒ Object

Turn on given features. Available features are:

  • auto_inflate
  • auto_deflate

Parameters:

  • features


244
245
246
# File 'lib/http/chainable.rb', line 244

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

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

Make a request through an HTTP proxy

Parameters:

  • proxy (Array)

Raises:

  • (Request::Error)

    if HTTP proxy is invalid



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/http/chainable.rb', line 159

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