class HTTPX::Options

  1. lib/httpx/options.rb
Superclass: Object

Constants

MAX_BODY_THRESHOLD_SIZE = (1 << 10) * 112  
REQUEST_IVARS = %i[@params @form @json @body].freeze  
WINDOW_SIZE = 1 << 14  

Public Class methods

def_option (name, &interpreter)
[show source]
   # File lib/httpx/options.rb
26 def def_option(name, &interpreter)
27   defined_options << name.to_sym
28   interpreter ||= ->(v) { v }
29 
30   attr_reader name
31 
32   define_method(:"#{name}=") do |value|
33     return if value.nil?
34 
35     instance_variable_set(:"@#{name}", instance_exec(value, &interpreter))
36   end
37 
38   protected :"#{name}="
39 
40   define_method(:"with_#{name}") do |value|
41     other = dup
42     other.send(:"#{name}=", other.instance_exec(value, &interpreter))
43     other
44   end
45 end
defined_options ()
[show source]
   # File lib/httpx/options.rb
22 def defined_options
23   @defined_options ||= []
24 end
inherited (klass)
[show source]
   # File lib/httpx/options.rb
 9 def inherited(klass)
10   super
11   klass.instance_variable_set(:@defined_options, @defined_options.dup)
12 end
new (options = {})
[show source]
   # File lib/httpx/options.rb
14 def new(options = {})
15   # let enhanced options go through
16   return options if self == Options && options.class > self
17   return options if options.is_a?(self)
18 
19   super
20 end
new (options = {})
[show source]
   # File lib/httpx/options.rb
48 def initialize(options = {})
49   defaults = {
50     :debug => ENV.key?("HTTPX_DEBUG") ? $stderr : nil,
51     :debug_level => (ENV["HTTPX_DEBUG"] || 1).to_i,
52     :ssl => {},
53     :http2_settings => { settings_enable_push: 0 },
54     :fallback_protocol => "http/1.1",
55     :timeout => Timeout.new,
56     :headers => {},
57     :window_size => WINDOW_SIZE,
58     :body_threshold_size => MAX_BODY_THRESHOLD_SIZE,
59     :request_class => Class.new(Request),
60     :response_class => Class.new(Response),
61     :headers_class => Class.new(Headers),
62     :request_body_class => Class.new(Request::Body),
63     :response_body_class => Class.new(Response::Body),
64     :connection_class => Class.new(Connection),
65     :transport => nil,
66     :transport_options => nil,
67     :persistent => false,
68     :resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym,
69     :resolver_options => { cache: true },
70   }
71 
72   defaults.merge!(options)
73   defaults[:headers] = Headers.new(defaults[:headers])
74   defaults.each do |(k, v)|
75     __send__(:"#{k}=", v)
76   end
77 end

Public Instance methods

== (other)
[show source]
    # File lib/httpx/options.rb
130 def ==(other)
131   ivars = instance_variables | other.instance_variables
132   ivars.all? do |ivar|
133     case ivar
134     when :@headers
135       headers = instance_variable_get(ivar)
136       headers.same_headers?(other.instance_variable_get(ivar))
137     when *REQUEST_IVARS
138       true
139     else
140       instance_variable_get(ivar) == other.instance_variable_get(ivar)
141     end
142   end
143 end
freeze ()
[show source]
    # File lib/httpx/options.rb
179 def freeze
180   super
181 
182   headers.freeze
183   ssl.freeze
184   request_class.freeze
185   response_class.freeze
186   headers_class.freeze
187   request_body_class.freeze
188   response_body_class.freeze
189   connection_class.freeze
190 end
initialize_dup (other)
[show source]
    # File lib/httpx/options.rb
168 def initialize_dup(other)
169   self.headers             = other.headers.dup
170   self.ssl                 = other.ssl.dup
171   self.request_class       = other.request_class.dup
172   self.response_class      = other.response_class.dup
173   self.headers_class       = other.headers_class.dup
174   self.request_body_class  = other.request_body_class.dup
175   self.response_body_class = other.response_body_class.dup
176   self.connection_class    = other.connection_class.dup
177 end
merge (other)
[show source]
    # File lib/httpx/options.rb
145 def merge(other)
146   h1 = to_hash
147   h2 = other.to_hash
148 
149   merged = h1.merge(h2) do |k, v1, v2|
150     case k
151     when :headers, :ssl, :http2_settings, :timeout
152       v1.merge(v2)
153     else
154       v2
155     end
156   end
157 
158   self.class.new(merged)
159 end
to_hash ()
[show source]
    # File lib/httpx/options.rb
161 def to_hash
162   hash_pairs = self.class
163                    .defined_options
164                    .flat_map { |opt_name| [opt_name, send(opt_name)] }
165   Hash[*hash_pairs]
166 end