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


160
161
162
# File 'lib/http/chainable.rb', line 160

def accept(type)
  with :accept => MimeType.normalize(type)
end

#auth(value, opts = nil) ⇒ Object

Make a request with the given Authorization header

Parameters:

  • value (#to_s)

    Authorization header value



166
167
168
169
170
171
# File 'lib/http/chainable.rb', line 166

def auth(value, opts = nil)
  # shim for deprecated auth(:basic, opts).
  # will be removed in 0.8.0
  return basic_auth(opts) if :basic == value
  with :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:



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

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

  auth("Basic " << Base64.strict_encode64("#{user}:#{pass}"))
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)


57
58
59
# File 'lib/http/chainable.rb', line 57

def connect(uri, options = {})
  request :connect, uri, options
end

#default_headersObject

Get headers of HTTP options



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

def default_headers
  default_options.headers
end

#default_headers=(headers) ⇒ Object

Set headers of HTTP options

Parameters:

  • headers


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

def default_headers=(headers)
  @default_options = default_options.dup do |opts|
    opts.headers = headers
  end
end

#default_optionsHTTP::Options

Get options for HTTP

Returns:



187
188
189
# File 'lib/http/chainable.rb', line 187

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

#default_options=(opts) ⇒ HTTP::Options

Set options for HTTP

Parameters:

  • opts

Returns:



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

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)


36
37
38
# File 'lib/http/chainable.rb', line 36

def delete(uri, options = {})
  request :delete, uri, options
end

#follow(opts = {}) ⇒ HTTP::Client Also known as: with_follow

Make client follow redirects.

Parameters:

  • opts (defaults to: {})

Returns:

See Also:



139
140
141
# File 'lib/http/chainable.rb', line 139

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

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

Get a resource

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


15
16
17
# File 'lib/http/chainable.rb', line 15

def get(uri, options = {})
  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)


8
9
10
# File 'lib/http/chainable.rb', line 8

def head(uri, options = {})
  request :head, uri, options
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)


50
51
52
# File 'lib/http/chainable.rb', line 50

def options(uri, options = {})
  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)


64
65
66
# File 'lib/http/chainable.rb', line 64

def patch(uri, options = {})
  request :patch, uri, options
end

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

Overloads:

  • #persistent(host) ⇒ HTTP::Client

    Flags as persistent

    Parameters:

    • host (String)

    Returns:

    Raises:

    • (Request::Error)

      if Host is invalid

  • #persistent(host) {|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



104
105
106
107
108
109
110
# File 'lib/http/chainable.rb', line 104

def persistent(host)
  p_client = branch default_options.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

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


22
23
24
# File 'lib/http/chainable.rb', line 22

def post(uri, options = {})
  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)


29
30
31
# File 'lib/http/chainable.rb', line 29

def put(uri, options = {})
  request :put, uri, options
end

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

Make an HTTP request with the given verb

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (Hash)


71
72
73
# File 'lib/http/chainable.rb', line 71

def request(verb, uri, options = {})
  branch(options).request verb, uri
end

#streamObject

Alias for with_response(:object)



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

def stream
  with_response(:object)
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)


43
44
45
# File 'lib/http/chainable.rb', line 43

def trace(uri, options = {})
  request :trace, uri, options
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



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/http/chainable.rb', line 115

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)

  if [2, 4].include?(proxy_hash.keys.size)
    branch default_options.with_proxy(proxy_hash)
  else
    fail(RequestError, "invalid HTTP proxy: #{proxy_hash}")
  end
end

#with_cache(cache) ⇒ Object



147
148
149
# File 'lib/http/chainable.rb', line 147

def with_cache(cache)
  branch default_options.with_cache(cache)
end

#with_headers(headers) ⇒ Object Also known as: with

Make a request with the given headers

Parameters:

  • headers


153
154
155
# File 'lib/http/chainable.rb', line 153

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