Module: HTTP::Chainable
Instance Method Summary collapse
-
#accept(type) ⇒ Object
Accept the given MIME type(s).
-
#auth(value, opts = nil) ⇒ Object
Make a request with the given Authorization header.
-
#basic_auth(opts) ⇒ Object
Make a request with the given Basic authorization header.
- #cache(cache) ⇒ Object (also: #with_cache)
-
#connect(uri, options = {}) ⇒ Object
Convert to a transparent TCP/IP tunnel.
-
#cookies(cookies) ⇒ Object
Make a request with the given cookies.
-
#default_headers ⇒ Object
deprecated
Deprecated.
Will be removed in 1.0.0; Use
#default_options#headers -
#default_headers=(headers) ⇒ Object
deprecated
Deprecated.
Will be removed in 1.0.0; Use
#headers -
#default_options ⇒ HTTP::Options
Get options for HTTP.
-
#default_options=(opts) ⇒ HTTP::Options
Set options for HTTP.
-
#delete(uri, options = {}) ⇒ Object
Delete a resource.
-
#follow(opts = {}) ⇒ HTTP::Client
(also: #with_follow)
Make client follow redirects.
-
#get(uri, options = {}) ⇒ Object
Get a resource.
-
#head(uri, options = {}) ⇒ Object
Request a get sans response body.
-
#headers(headers) ⇒ Object
(also: #with, #with_headers)
Make a request with the given headers.
-
#options(uri, options = {}) ⇒ Object
Return the methods supported on the given URI.
-
#patch(uri, options = {}) ⇒ Object
Apply partial modifications to a resource.
- #persistent(host) ⇒ Object
-
#post(uri, options = {}) ⇒ Object
Post to a resource.
-
#put(uri, options = {}) ⇒ Object
Put to a resource.
-
#request(verb, uri, options = {}) ⇒ Object
Make an HTTP request with the given verb.
-
#timeout(klass, options = {}) ⇒ Object
@overload(options = {}) Syntax sugar for
timeout(:per_operation, options)@overload(klass, options = {}) @param [#to_sym] klass @param [Hash] options @option options [Float] :read Read timeout @option options [Float] :write Write timeout @option options [Float] :connect Connect timeout. -
#trace(uri, options = {}) ⇒ Object
Echo the request back to the client.
-
#via(*proxy) ⇒ Object
(also: #through)
Make a request through an HTTP proxy.
Instance Method Details
#accept(type) ⇒ Object
Accept the given MIME type(s)
202 203 204 |
# File 'lib/http/chainable.rb', line 202 def accept(type) headers Headers::ACCEPT => MimeType.normalize(type) end |
#auth(value, opts = nil) ⇒ Object
Make a request with the given Authorization header
208 209 210 211 212 213 |
# File 'lib/http/chainable.rb', line 208 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 headers Headers::AUTHORIZATION => value.to_s end |
#basic_auth(opts) ⇒ Object
Make a request with the given Basic authorization header
220 221 222 223 224 225 |
# File 'lib/http/chainable.rb', line 220 def basic_auth(opts) user = opts.fetch :user pass = opts.fetch :pass auth("Basic " << Base64.strict_encode64("#{user}:#{pass}")) end |
#cache(cache) ⇒ Object Also known as: with_cache
173 174 175 |
# File 'lib/http/chainable.rb', line 173 def cache(cache) branch .with_cache(cache) end |
#connect(uri, options = {}) ⇒ Object
Convert to a transparent TCP/IP tunnel
59 60 61 |
# File 'lib/http/chainable.rb', line 59 def connect(uri, = {}) request :connect, uri, end |
#cookies(cookies) ⇒ Object
Make a request with the given cookies
196 197 198 |
# File 'lib/http/chainable.rb', line 196 def () branch .() end |
#default_headers ⇒ Object
Will be removed in 1.0.0; Use #default_options#headers
Get headers of HTTP options
242 243 244 |
# File 'lib/http/chainable.rb', line 242 def default_headers .headers end |
#default_headers=(headers) ⇒ Object
Will be removed in 1.0.0; Use #headers
Set headers of HTTP options
249 250 251 252 253 |
# File 'lib/http/chainable.rb', line 249 def default_headers=(headers) @default_options = .dup do |opts| opts.headers = headers end end |
#default_options ⇒ HTTP::Options
Get options for HTTP
229 230 231 |
# File 'lib/http/chainable.rb', line 229 def @default_options ||= HTTP::Options.new end |
#default_options=(opts) ⇒ HTTP::Options
Set options for HTTP
236 237 238 |
# File 'lib/http/chainable.rb', line 236 def (opts) @default_options = HTTP::Options.new(opts) end |
#delete(uri, options = {}) ⇒ Object
Delete a resource
38 39 40 |
# File 'lib/http/chainable.rb', line 38 def delete(uri, = {}) request :delete, uri, end |
#follow(opts = {}) ⇒ HTTP::Client Also known as: with_follow
Make client follow redirects.
165 166 167 |
# File 'lib/http/chainable.rb', line 165 def follow(opts = {}) branch .with_follow opts end |
#get(uri, options = {}) ⇒ Object
Get a resource
17 18 19 |
# File 'lib/http/chainable.rb', line 17 def get(uri, = {}) request :get, uri, end |
#head(uri, options = {}) ⇒ Object
Request a get sans response body
10 11 12 |
# File 'lib/http/chainable.rb', line 10 def head(uri, = {}) request :head, uri, end |
#headers(headers) ⇒ Object Also known as: with, with_headers
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 |
#options(uri, options = {}) ⇒ Object
Return the methods supported on the given URI
52 53 54 |
# File 'lib/http/chainable.rb', line 52 def (uri, = {}) request :options, uri, end |
#patch(uri, options = {}) ⇒ Object
Apply partial modifications to a resource
66 67 68 |
# File 'lib/http/chainable.rb', line 66 def patch(uri, = {}) request :patch, uri, end |
#persistent(host) ⇒ HTTP::Client #persistent(host) {|client| ... } ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/http/chainable.rb', line 135 def persistent(host) p_client = branch .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
24 25 26 |
# File 'lib/http/chainable.rb', line 24 def post(uri, = {}) request :post, uri, end |
#put(uri, options = {}) ⇒ Object
Put to a resource
31 32 33 |
# File 'lib/http/chainable.rb', line 31 def put(uri, = {}) request :put, uri, end |
#request(verb, uri, options = {}) ⇒ Object
Make an HTTP request with the given verb
73 74 75 |
# File 'lib/http/chainable.rb', line 73 def request(verb, uri, = {}) branch().request verb, uri end |
#timeout(klass, options = {}) ⇒ Object
@overload(options = {})
Syntax sugar for timeout(:per_operation, options)
@overload(klass, options = {})
@param [#to_sym] klass
@param [Hash] options
@option options [Float] :read Read timeout
@option options [Float] :write Write timeout
@option options [Float] :connect Connect timeout
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/http/chainable.rb', line 85 def timeout(klass, = {}) klass, = :per_operation, klass if klass.is_a? Hash klass = case klass.to_sym when :null then HTTP::Timeout::Null when :global then HTTP::Timeout::Global when :per_operation then HTTP::Timeout::PerOperation else fail ArgumentError, "Unsupported Timeout class: #{klass}" end [: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 |
#trace(uri, options = {}) ⇒ Object
Echo the request back to the client
45 46 47 |
# File 'lib/http/chainable.rb', line 45 def trace(uri, = {}) request :trace, uri, end |
#via(*proxy) ⇒ Object Also known as: through
Make a request through an HTTP proxy
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/http/chainable.rb', line 146 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 .with_proxy(proxy_hash) else fail(RequestError, "invalid HTTP proxy: #{proxy_hash}") end end |