Class: HTTPX::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/options.rb

Constant Summary collapse

WINDOW_SIZE =

16K

1 << 14
MAX_BODY_THRESHOLD_SIZE =

112K

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Options

Returns a new instance of Options.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/httpx/options.rb', line 53

def initialize(options = {})
  defaults = {
    :debug => ENV.key?("HTTPX_DEBUG") ? $stderr : nil,
    :debug_level => (ENV["HTTPX_DEBUG"] || 1).to_i,
    :ssl => {},
    :http2_settings => { settings_enable_push: 0 },
    :fallback_protocol => "http/1.1",
    :timeout => Timeout.new,
    :headers => {},
    :window_size => WINDOW_SIZE,
    :body_threshold_size => MAX_BODY_THRESHOLD_SIZE,
    :request_class => Class.new(Request),
    :response_class => Class.new(Response),
    :headers_class => Class.new(Headers),
    :request_body_class => Class.new(Request::Body),
    :response_body_class => Class.new(Response::Body),
    :connection_class => Class.new(Connection),
    :transport => nil,
    :transport_options => nil,
    :persistent => false,
    :resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym,
    :resolver_options => { cache: true },
  }

  defaults.merge!(options)
  defaults.each do |(k, v)|
    __send__(:"#{k}=", v)
  end
end

Class Method Details

.def_option(name, &interpreter) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/httpx/options.rb', line 26

def def_option(name, &interpreter)
  defined_options << name.to_sym

  attr_reader name

  if interpreter
    define_method(:"#{name}=") do |value|
      return if value.nil?

      instance_variable_set(:"@#{name}", instance_exec(value, &interpreter))
    end

    define_method(:"with_#{name}") do |value|
      merge(name => instance_exec(value, &interpreter))
    end
  else
    attr_writer name

    define_method(:"with_#{name}") do |value|
      merge(name => value)
    end
  end

  protected :"#{name}="
end

.defined_optionsObject



22
23
24
# File 'lib/httpx/options.rb', line 22

def defined_options
  @defined_options ||= []
end

.inherited(klass) ⇒ Object



9
10
11
12
# File 'lib/httpx/options.rb', line 9

def inherited(klass)
  super
  klass.instance_variable_set(:@defined_options, @defined_options.dup)
end

.new(options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/httpx/options.rb', line 14

def new(options = {})
  # let enhanced options go through
  return options if self == Options && options.class > self
  return options if options.is_a?(self)

  super
end

Instance Method Details

#==(other) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/httpx/options.rb', line 133

def ==(other)
  ivars = instance_variables | other.instance_variables
  ivars.all? do |ivar|
    case ivar
    when :@headers
      headers = instance_variable_get(ivar)
      headers.same_headers?(other.instance_variable_get(ivar))
    when *REQUEST_IVARS
      true
    else
      instance_variable_get(ivar) == other.instance_variable_get(ivar)
    end
  end
end

#freezeObject



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/httpx/options.rb', line 182

def freeze
  super

  headers.freeze
  ssl.freeze
  request_class.freeze
  response_class.freeze
  headers_class.freeze
  request_body_class.freeze
  response_body_class.freeze
  connection_class.freeze
end

#initialize_dup(other) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/httpx/options.rb', line 171

def initialize_dup(other)
  self.headers             = other.headers.dup
  self.ssl                 = other.ssl.dup
  self.request_class       = other.request_class.dup
  self.response_class      = other.response_class.dup
  self.headers_class       = other.headers_class.dup
  self.request_body_class  = other.request_body_class.dup
  self.response_body_class = other.response_body_class.dup
  self.connection_class    = other.connection_class.dup
end

#merge(other) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/httpx/options.rb', line 148

def merge(other)
  h1 = to_hash
  h2 = other.to_hash

  merged = h1.merge(h2) do |k, v1, v2|
    case k
    when :headers, :ssl, :http2_settings, :timeout
      v1.merge(v2)
    else
      v2
    end
  end

  self.class.new(merged)
end

#to_hashObject



164
165
166
167
168
169
# File 'lib/httpx/options.rb', line 164

def to_hash
  hash_pairs = self.class
                   .defined_options
                   .flat_map { |opt_name| [opt_name, send(opt_name)] }
  Hash[*hash_pairs]
end