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(verb, uri, options = {}) ⇒ 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(verb, uri, options = {}) ⇒ Object
Make an HTTP request with the given verb.
- #timeout(klass, 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.
Instance Method Details
#accept(type) ⇒ Object
Accept the given MIME type(s)
205 206 207 |
# File 'lib/http/chainable.rb', line 205 def accept(type) headers Headers::ACCEPT => MimeType.normalize(type) end |
#auth(value) ⇒ Object
Make a request with the given Authorization header
211 212 213 |
# File 'lib/http/chainable.rb', line 211 def auth(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 |
#build_request(verb, uri, options = {}) ⇒ Object
Prepare an HTTP request with the given verb
84 85 86 |
# File 'lib/http/chainable.rb', line 84 def build_request(verb, uri, = {}) # rubocop:disable Style/OptionHash branch().build_request verb, uri end |
#connect(uri, options = {}) ⇒ Object
Convert to a transparent TCP/IP tunnel
61 62 63 |
# File 'lib/http/chainable.rb', line 61 def connect(uri, = {}) # rubocop:disable Style/OptionHash request :connect, uri, end |
#cookies(cookies) ⇒ Object
Make a request with the given cookies
194 195 196 |
# File 'lib/http/chainable.rb', line 194 def () branch .() 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
40 41 42 |
# File 'lib/http/chainable.rb', line 40 def delete(uri, = {}) # rubocop:disable Style/OptionHash request :delete, uri, end |
#encoding(encoding) ⇒ Object
Force a specific encoding for response body
199 200 201 |
# File 'lib/http/chainable.rb', line 199 def encoding(encoding) branch .with_encoding(encoding) end |
#follow(options = {}) ⇒ HTTP::Client
Make client follow redirects.
183 184 185 |
# File 'lib/http/chainable.rb', line 183 def follow( = {}) # rubocop:disable Style/OptionHash branch .with_follow end |
#get(uri, options = {}) ⇒ Object
Get a resource
19 20 21 |
# File 'lib/http/chainable.rb', line 19 def get(uri, = {}) # rubocop:disable Style/OptionHash request :get, uri, end |
#head(uri, options = {}) ⇒ Object
Request a get sans response body
12 13 14 |
# File 'lib/http/chainable.rb', line 12 def head(uri, = {}) # rubocop:disable Style/OptionHash request :head, uri, end |
#headers(headers) ⇒ Object
Make a request with the given headers
189 190 191 |
# File 'lib/http/chainable.rb', line 189 def headers(headers) branch .with_headers(headers) end |
#nodelay ⇒ Object
Set TCP_NODELAY on the socket
241 242 243 |
# File 'lib/http/chainable.rb', line 241 def nodelay branch .with_nodelay(true) end |
#options(uri, options = {}) ⇒ Object
Return the methods supported on the given URI
54 55 56 |
# File 'lib/http/chainable.rb', line 54 def (uri, = {}) # rubocop:disable Style/OptionHash request :options, uri, end |
#patch(uri, options = {}) ⇒ Object
Apply partial modifications to a resource
68 69 70 |
# File 'lib/http/chainable.rb', line 68 def patch(uri, = {}) # rubocop:disable Style/OptionHash request :patch, uri, end |
#persistent(host, timeout: 5) ⇒ HTTP::Client #persistent(host, timeout: 5) {|client| ... } ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/http/chainable.rb', line 152 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 if p_client end |
#post(uri, options = {}) ⇒ Object
Post to a resource
26 27 28 |
# File 'lib/http/chainable.rb', line 26 def post(uri, = {}) # rubocop:disable Style/OptionHash request :post, uri, end |
#put(uri, options = {}) ⇒ Object
Put to a resource
33 34 35 |
# File 'lib/http/chainable.rb', line 33 def put(uri, = {}) # rubocop:disable Style/OptionHash request :put, uri, end |
#request(verb, uri, options = {}) ⇒ Object
Make an HTTP request with the given verb
76 77 78 |
# File 'lib/http/chainable.rb', line 76 def request(verb, uri, = {}) # rubocop:disable Style/OptionHash branch().request verb, uri end |
#timeout(options = {}) ⇒ Object #timeout(klass, options = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/http/chainable.rb', line 98 def timeout(klass, = {}) # rubocop:disable Style/OptionHash if klass.is_a? Hash = klass klass = :per_operation end 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 raise ArgumentError, "Unsupported Timeout class: #{klass}" end %i[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
47 48 49 |
# File 'lib/http/chainable.rb', line 47 def trace(uri, = {}) # rubocop:disable Style/OptionHash request :trace, uri, end |
#use(*features) ⇒ Object
Turn on given features. Available features are:
- auto_inflate
- auto_deflate
249 250 251 |
# File 'lib/http/chainable.rb', line 249 def use(*features) branch .with_features(features) end |
#via(*proxy) ⇒ Object Also known as: through
Make a request through an HTTP proxy
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/http/chainable.rb', line 164 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 |