Class: HTTP::Options
- Inherits:
-
Object
- Object
- HTTP::Options
- Defined in:
- lib/http/options.rb,
lib/http/options/definitions.rb,
sig/http.rbs
Overview
Configuration options for HTTP requests and clients
Class Attribute Summary collapse
-
.available_features ⇒ Hash
readonly
Registered feature implementations.
-
.default_socket_class ⇒ Class
Default TCP socket class.
-
.default_ssl_socket_class ⇒ Class
Default SSL socket class.
-
.default_timeout_class ⇒ Class
Default timeout handler class.
Instance Attribute Summary collapse
-
#base_uri ⇒ URI?
Returns the value of attribute base_uri.
-
#body ⇒ Object
Returns the value of attribute body.
-
#encoding ⇒ Encoding?
Returns the value of attribute encoding.
-
#features ⇒ Hash[Symbol, Feature]
Returns the value of attribute features.
-
#follow ⇒ Hash[Symbol, untyped]?
Returns the value of attribute follow.
-
#form ⇒ Object
Returns the value of attribute form.
-
#headers ⇒ Headers
Dynamic options generated by def_option.
-
#json ⇒ Object
Returns the value of attribute json.
-
#keep_alive_timeout ⇒ Integer
Returns the value of attribute keep_alive_timeout.
-
#nodelay ⇒ Boolean
Returns the value of attribute nodelay.
-
#params ⇒ Object
Returns the value of attribute params.
-
#persistent ⇒ String?
Returns the value of attribute persistent.
-
#proxy ⇒ Hash[Symbol, untyped]
Returns the value of attribute proxy.
-
#response ⇒ Symbol
Returns the value of attribute response.
-
#retriable ⇒ Hash[Symbol, untyped]?
Returns the value of attribute retriable.
-
#socket_class ⇒ Class
Returns the value of attribute socket_class.
-
#ssl ⇒ Hash[Symbol, untyped]
Returns the value of attribute ssl.
-
#ssl_context ⇒ OpenSSL::SSL::SSLContext?
Returns the value of attribute ssl_context.
-
#ssl_socket_class ⇒ Class
Returns the value of attribute ssl_socket_class.
-
#timeout_class ⇒ Class
Returns the value of attribute timeout_class.
-
#timeout_options ⇒ Hash[Symbol, Numeric]
Returns the value of attribute timeout_options.
Class Method Summary collapse
-
.defined_options ⇒ Array<Symbol>
Returns list of defined option names.
-
.new(options = nil, **kwargs) ⇒ HTTP::Options
Returns existing Options or creates new one.
-
.register_feature(name, impl) ⇒ Class
Registers a feature by name and implementation.
Instance Method Summary collapse
-
#argument_error!(message) ⇒ void
private
Raises an argument error with adjusted backtrace.
-
#assign_options(env) ⇒ void
private
Assigns all option values from the initialize binding.
-
#base_uri? ⇒ Boolean
Checks whether a base URI is set.
-
#dup {|dupped| ... } ⇒ HTTP::Options
Duplicates the options object.
-
#feature(name) ⇒ Feature?
Returns a feature by name.
-
#initialize(response: :auto, encoding: nil, nodelay: false, keep_alive_timeout: 5, proxy: {}, ssl: {}, headers: {}, features: {}, timeout_class: self.class.default_timeout_class, timeout_options: {}, socket_class: self.class.default_socket_class, ssl_socket_class: self.class.default_ssl_socket_class, params: nil, form: nil, json: nil, body: nil, follow: nil, retriable: nil, base_uri: nil, persistent: nil, ssl_context: nil) ⇒ HTTP::Options
constructor
Initializes options with keyword arguments.
-
#merge(other) ⇒ HTTP::Options
Merges two Options objects.
-
#parse_base_uri(value) ⇒ HTTP::URI
private
Parses and validates a base URI value.
-
#persistent? ⇒ Boolean
Checks whether persistent connection is enabled.
-
#resolve_base_uri(base, relative) ⇒ HTTP::URI
private
Resolves a relative URI against an existing base URI.
-
#to_hash ⇒ Hash
Converts options to a Hash.
-
#validate_base_uri_and_persistent! ⇒ void
private
Validates that base URI and persistent origin are compatible.
- #with_base_uri ⇒ Options
- #with_body ⇒ Options
- #with_encoding ⇒ Options
- #with_features ⇒ Options
- #with_follow ⇒ Options
- #with_form ⇒ Options
- #with_headers ⇒ Options
- #with_json ⇒ Options
- #with_keep_alive_timeout ⇒ Options
- #with_nodelay ⇒ Options
- #with_params ⇒ Options
- #with_persistent ⇒ Options
- #with_proxy ⇒ Options
- #with_response ⇒ Options
- #with_retriable ⇒ Options
- #with_socket_class ⇒ Options
- #with_ssl ⇒ Options
- #with_ssl_context ⇒ Options
- #with_ssl_socket_class ⇒ Options
- #with_timeout_class ⇒ Options
- #with_timeout_options ⇒ Options
Constructor Details
#initialize(response: :auto, encoding: nil, nodelay: false, keep_alive_timeout: 5, proxy: {}, ssl: {}, headers: {}, features: {}, timeout_class: self.class.default_timeout_class, timeout_options: {}, socket_class: self.class.default_socket_class, ssl_socket_class: self.class.default_ssl_socket_class, params: nil, form: nil, json: nil, body: nil, follow: nil, retriable: nil, base_uri: nil, persistent: nil, ssl_context: nil) ⇒ HTTP::Options
Initializes options with keyword arguments
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/http/options.rb', line 134 def initialize( response: :auto, encoding: nil, nodelay: false, keep_alive_timeout: 5, proxy: {}, ssl: {}, headers: {}, features: {}, timeout_class: self.class.default_timeout_class, timeout_options: {}, socket_class: self.class.default_socket_class, ssl_socket_class: self.class.default_ssl_socket_class, params: nil, form: nil, json: nil, body: nil, follow: nil, retriable: nil, base_uri: nil, persistent: nil, ssl_context: nil ) (binding) end |
Class Attribute Details
.available_features ⇒ Hash (readonly)
Registered feature implementations
51 52 53 |
# File 'lib/http/options.rb', line 51 def available_features @available_features end |
.default_socket_class ⇒ Class
Default TCP socket class
24 25 26 |
# File 'lib/http/options.rb', line 24 def default_socket_class @default_socket_class end |
.default_ssl_socket_class ⇒ Class
Default SSL socket class
33 34 35 |
# File 'lib/http/options.rb', line 33 def default_ssl_socket_class @default_ssl_socket_class end |
.default_timeout_class ⇒ Class
Default timeout handler class
42 43 44 |
# File 'lib/http/options.rb', line 42 def default_timeout_class @default_timeout_class end |
Instance Attribute Details
#base_uri ⇒ URI?
Returns the value of attribute base_uri.
685 686 687 |
# File 'sig/http.rbs', line 685 def base_uri @base_uri end |
#body ⇒ Object
Returns the value of attribute body.
672 673 674 |
# File 'sig/http.rbs', line 672 def body @body end |
#encoding ⇒ Encoding?
Returns the value of attribute encoding.
682 683 684 |
# File 'sig/http.rbs', line 682 def encoding @encoding end |
#features ⇒ Hash[Symbol, Feature]
Returns the value of attribute features.
653 654 655 |
# File 'sig/http.rbs', line 653 def features @features end |
#follow ⇒ Hash[Symbol, untyped]?
Returns the value of attribute follow.
683 684 685 |
# File 'sig/http.rbs', line 683 def follow @follow end |
#form ⇒ Object
Returns the value of attribute form.
670 671 672 |
# File 'sig/http.rbs', line 670 def form @form end |
#headers ⇒ Headers
Dynamic options generated by def_option
667 668 669 |
# File 'sig/http.rbs', line 667 def headers @headers end |
#json ⇒ Object
Returns the value of attribute json.
671 672 673 |
# File 'sig/http.rbs', line 671 def json @json end |
#keep_alive_timeout ⇒ Integer
Returns the value of attribute keep_alive_timeout.
679 680 681 |
# File 'sig/http.rbs', line 679 def keep_alive_timeout @keep_alive_timeout end |
#nodelay ⇒ Boolean
Returns the value of attribute nodelay.
675 676 677 |
# File 'sig/http.rbs', line 675 def nodelay @nodelay end |
#params ⇒ Object
Returns the value of attribute params.
669 670 671 |
# File 'sig/http.rbs', line 669 def params @params end |
#persistent ⇒ String?
Returns the value of attribute persistent.
686 687 688 |
# File 'sig/http.rbs', line 686 def persistent @persistent end |
#proxy ⇒ Hash[Symbol, untyped]
Returns the value of attribute proxy.
668 669 670 |
# File 'sig/http.rbs', line 668 def proxy @proxy end |
#response ⇒ Symbol
Returns the value of attribute response.
673 674 675 |
# File 'sig/http.rbs', line 673 def response @response end |
#retriable ⇒ Hash[Symbol, untyped]?
Returns the value of attribute retriable.
684 685 686 |
# File 'sig/http.rbs', line 684 def retriable @retriable end |
#socket_class ⇒ Class
Returns the value of attribute socket_class.
674 675 676 |
# File 'sig/http.rbs', line 674 def socket_class @socket_class end |
#ssl ⇒ Hash[Symbol, untyped]
Returns the value of attribute ssl.
678 679 680 |
# File 'sig/http.rbs', line 678 def ssl @ssl end |
#ssl_context ⇒ OpenSSL::SSL::SSLContext?
Returns the value of attribute ssl_context.
677 678 679 |
# File 'sig/http.rbs', line 677 def ssl_context @ssl_context end |
#ssl_socket_class ⇒ Class
Returns the value of attribute ssl_socket_class.
676 677 678 |
# File 'sig/http.rbs', line 676 def ssl_socket_class @ssl_socket_class end |
#timeout_class ⇒ Class
Returns the value of attribute timeout_class.
680 681 682 |
# File 'sig/http.rbs', line 680 def timeout_class @timeout_class end |
#timeout_options ⇒ Hash[Symbol, Numeric]
Returns the value of attribute timeout_options.
681 682 683 |
# File 'sig/http.rbs', line 681 def @timeout_options end |
Class Method Details
.defined_options ⇒ Array<Symbol>
Returns list of defined option names
74 75 76 |
# File 'lib/http/options.rb', line 74 def @defined_options ||= [] end |
.new(options = nil, **kwargs) ⇒ HTTP::Options
Returns existing Options or creates new one
61 62 63 64 65 |
# File 'lib/http/options.rb', line 61 def new( = nil, **kwargs) return if .is_a?(self) super(**( || kwargs)) # steep:ignore end |
.register_feature(name, impl) ⇒ Class
Registers a feature by name and implementation
87 88 89 |
# File 'lib/http/options.rb', line 87 def register_feature(name, impl) @available_features[name] = impl end |
Instance Method Details
#argument_error!(message) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Raises an argument error with adjusted backtrace
233 234 235 236 237 |
# File 'lib/http/options.rb', line 233 def argument_error!() error = Error.new() error.set_backtrace(caller(1) || []) raise error end |
#assign_options(env) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Assigns all option values from the initialize binding
221 222 223 224 225 226 227 |
# File 'lib/http/options.rb', line 221 def (env) self.class..each do |name| value = env.local_variable_get(name) value = Headers.coerce(value) if name.eql?(:headers) __send__(:"#{name}=", value) end end |
#base_uri? ⇒ Boolean
Checks whether a base URI is set
113 114 115 |
# File 'lib/http/options/definitions.rb', line 113 def base_uri? !base_uri.nil? end |
#dup {|dupped| ... } ⇒ HTTP::Options
Duplicates the options object
195 196 197 198 199 |
# File 'lib/http/options.rb', line 195 def dup dupped = super yield(dupped) if block_given? dupped end |
#feature(name) ⇒ Feature?
Returns a feature by name
210 211 212 |
# File 'lib/http/options.rb', line 210 def feature(name) features[name] end |
#merge(other) ⇒ HTTP::Options
Merges two Options objects
168 169 170 171 172 173 174 |
# File 'lib/http/options.rb', line 168 def merge(other) merged = to_hash.merge(other.to_hash) do |k, v1, v2| k == :headers ? v1.merge(v2) : v2 end self.class.new(**merged) end |
#parse_base_uri(value) ⇒ HTTP::URI
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parses and validates a base URI value
148 149 150 151 152 153 154 155 156 |
# File 'lib/http/options/definitions.rb', line 148 def parse_base_uri(value) uri = URI.parse(value) base = @base_uri return resolve_base_uri(base, uri) if base argument_error!(format("Invalid base URI: %s", value)) unless uri.scheme uri end |
#persistent? ⇒ Boolean
Checks whether persistent connection is enabled
137 138 139 |
# File 'lib/http/options/definitions.rb', line 137 def persistent? !persistent.nil? end |
#resolve_base_uri(base, relative) ⇒ HTTP::URI
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolves a relative URI against an existing base URI
164 165 166 167 168 169 170 171 |
# File 'lib/http/options/definitions.rb', line 164 def resolve_base_uri(base, relative) unless base.path.end_with?("/") base = base.dup base.path = "#{base.path}/" end URI.parse(base.join(relative)) end |
#to_hash ⇒ Hash
Converts options to a Hash
183 184 185 |
# File 'lib/http/options.rb', line 183 def to_hash self.class..to_h { |opt_name| [opt_name, public_send(opt_name)] } end |
#validate_base_uri_and_persistent! ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Validates that base URI and persistent origin are compatible
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/http/options/definitions.rb', line 177 def validate_base_uri_and_persistent! base = @base_uri persistent = @persistent return unless base && persistent return if base.origin.eql?(persistent) argument_error!( format("Persistence origin (%s) conflicts with base URI origin (%s)", persistent, base.origin) ) end |
#with_base_uri ⇒ Options
693 |
# File 'sig/http.rbs', line 693
def with_base_uri: (String | URI value) -> Options
|
#with_encoding ⇒ Options
689 |
# File 'sig/http.rbs', line 689
def with_encoding: (String | Encoding value) -> Options
|
#with_features ⇒ Options
690 |
# File 'sig/http.rbs', line 690
def with_features: (Array[Symbol | Hash[Symbol, untyped]] value) -> Options
|
#with_follow ⇒ Options
691 |
# File 'sig/http.rbs', line 691
def with_follow: (Hash[Symbol, untyped] | bool value) -> Options
|
#with_headers ⇒ Options
688 |
# File 'sig/http.rbs', line 688
def with_headers: (Hash[String | Symbol, untyped] | Headers value) -> Options
|
#with_keep_alive_timeout ⇒ Options
706 |
# File 'sig/http.rbs', line 706
def with_keep_alive_timeout: (Integer value) -> Options
|
#with_nodelay ⇒ Options
702 |
# File 'sig/http.rbs', line 702
def with_nodelay: (bool value) -> Options
|
#with_params ⇒ Options
696 |
# File 'sig/http.rbs', line 696
def with_params: (Hash[String | Symbol, untyped] value) -> Options
|
#with_persistent ⇒ Options
694 |
# File 'sig/http.rbs', line 694
def with_persistent: (String value) -> Options
|
#with_proxy ⇒ Options
695 |
# File 'sig/http.rbs', line 695
def with_proxy: (Hash[Symbol, untyped] value) -> Options
|
#with_response ⇒ Options
700 |
# File 'sig/http.rbs', line 700
def with_response: (Symbol value) -> Options
|
#with_retriable ⇒ Options
692 |
# File 'sig/http.rbs', line 692
def with_retriable: (Hash[Symbol, untyped] | bool value) -> Options
|
#with_socket_class ⇒ Options
701 |
# File 'sig/http.rbs', line 701
def with_socket_class: (Class value) -> Options
|
#with_ssl ⇒ Options
705 |
# File 'sig/http.rbs', line 705
def with_ssl: (Hash[Symbol, untyped] value) -> Options
|
#with_ssl_context ⇒ Options
704 |
# File 'sig/http.rbs', line 704
def with_ssl_context: (OpenSSL::SSL::SSLContext? value) -> Options
|
#with_ssl_socket_class ⇒ Options
703 |
# File 'sig/http.rbs', line 703
def with_ssl_socket_class: (Class value) -> Options
|
#with_timeout_class ⇒ Options
707 |
# File 'sig/http.rbs', line 707
def with_timeout_class: (Class value) -> Options
|
#with_timeout_options ⇒ Options
708 |
# File 'sig/http.rbs', line 708
def with_timeout_options: (Hash[Symbol, Numeric] value) -> Options
|