module HTTP::Chainable
Public Instance Methods
Accept the given MIME type(s) @param type
# File lib/http/chainable.rb, line 204 def accept(type) headers Headers::ACCEPT => MimeType.normalize(type) end
Make a request with the given Authorization header @param [#to_s] value Authorization header value
# File lib/http/chainable.rb, line 210 def auth(value) headers Headers::AUTHORIZATION => value.to_s end
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 219 def basic_auth(opts) user = opts.fetch :user pass = opts.fetch :pass auth("Basic " + Base64.strict_encode64("#{user}:#{pass}")) end
Prepare an HTTP request with the given verb @param verb @param uri @option options [Hash]
# File lib/http/chainable.rb, line 83 def build_request(verb, uri, options = {}) # rubocop:disable Style/OptionHash branch(options).build_request verb, uri end
Convert to a transparent TCP/IP tunnel @param uri @option options [Hash]
# File lib/http/chainable.rb, line 60 def connect(uri, options = {}) # rubocop:disable Style/OptionHash request :connect, uri, options end
Get options for HTTP @return [HTTP::Options]
# File lib/http/chainable.rb, line 228 def default_options @default_options ||= HTTP::Options.new end
Set options for HTTP @param opts @return [HTTP::Options]
# File lib/http/chainable.rb, line 235 def default_options=(opts) @default_options = HTTP::Options.new(opts) end
Delete a resource @param uri @option options [Hash]
# File lib/http/chainable.rb, line 39 def delete(uri, options = {}) # rubocop:disable Style/OptionHash request :delete, uri, options end
Force a specific encoding for response body
# File lib/http/chainable.rb, line 198 def encoding(encoding) branch default_options.with_encoding(encoding) end
Make client follow redirects. @param opts @return [HTTP::Client] @see Redirector#initialize
# File lib/http/chainable.rb, line 182 def follow(options = {}) # rubocop:disable Style/OptionHash branch default_options.with_follow options end
Get a resource @param uri @option options [Hash]
# File lib/http/chainable.rb, line 18 def get(uri, options = {}) # rubocop:disable Style/OptionHash request :get, uri, options end
Request a get sans response body @param uri @option options [Hash]
# File lib/http/chainable.rb, line 11 def head(uri, options = {}) # rubocop:disable Style/OptionHash request :head, uri, options end
Make a request with the given headers @param headers
# File lib/http/chainable.rb, line 188 def headers(headers) branch default_options.with_headers(headers) end
Set TCP_NODELAY on the socket
# File lib/http/chainable.rb, line 240 def nodelay branch default_options.with_nodelay(true) end
Return the methods supported on the given URI @param uri @option options [Hash]
# File lib/http/chainable.rb, line 53 def options(uri, options = {}) # rubocop:disable Style/OptionHash request :options, uri, options end
Apply partial modifications to a resource @param uri @option options [Hash]
# File lib/http/chainable.rb, line 67 def patch(uri, options = {}) # rubocop:disable Style/OptionHash request :patch, uri, options end
@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 151 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 to a resource @param uri @option options [Hash]
# File lib/http/chainable.rb, line 25 def post(uri, options = {}) # rubocop:disable Style/OptionHash request :post, uri, options end
Put to a resource @param uri @option options [Hash]
# File lib/http/chainable.rb, line 32 def put(uri, options = {}) # rubocop:disable Style/OptionHash request :put, uri, options end
Make an HTTP request with the given verb @param verb @param uri @option options [Hash]
# File lib/http/chainable.rb, line 75 def request(verb, uri, options = {}) # rubocop:disable Style/OptionHash branch(options).request verb, uri end
@overload timeout(options = {})
Syntax sugar for `timeout(:per_operation, options)`
@overload timeout(klass, options = {})
Adds a timeout to the request. @param [#to_sym] klass either :null, :global, or :per_operation @param [Hash] options @option options [Float] :read Read timeout @option options [Float] :write Write timeout @option options [Float] :connect Connect timeout
# File lib/http/chainable.rb, line 97 def timeout(klass, options = {}) # rubocop:disable Style/OptionHash if klass.is_a? Hash options = 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 %[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
Echo the request back to the client @param uri @option options [Hash]
# File lib/http/chainable.rb, line 46 def trace(uri, options = {}) # rubocop:disable Style/OptionHash request :trace, uri, options end
Turn on given features. Available features are:
-
auto_inflate
-
auto_deflate
@param features
# File lib/http/chainable.rb, line 248 def use(*features) branch default_options.with_features(features) end
Make a request through an HTTP proxy @param [Array] proxy @raise [Request::Error] if HTTP proxy is invalid
# File lib/http/chainable.rb, line 163 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