Class: HTTPX::Options

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

Constant Summary collapse

BUFFER_SIZE =
1 << 14
WINDOW_SIZE =

16K

1 << 14
MAX_BODY_THRESHOLD_SIZE =

112K

(1 << 10) * 112
CONNECT_TIMEOUT =
60
OPERATION_TIMEOUT =
60
KEEP_ALIVE_TIMEOUT =
20
SETTINGS_TIMEOUT =
10
READ_TIMEOUT =
WRITE_TIMEOUT = REQUEST_TIMEOUT = Float::INFINITY
DEFAULT_OPTIONS =
{
  :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 => {
    connect_timeout: CONNECT_TIMEOUT,
    settings_timeout: SETTINGS_TIMEOUT,
    operation_timeout: OPERATION_TIMEOUT,
    keep_alive_timeout: KEEP_ALIVE_TIMEOUT,
    read_timeout: READ_TIMEOUT,
    write_timeout: WRITE_TIMEOUT,
    request_timeout: REQUEST_TIMEOUT,
  },
  :headers => {},
  :window_size => WINDOW_SIZE,
  :buffer_size => BUFFER_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),
  :options_class => Class.new(self),
  :transport => nil,
  :transport_options => nil,
  :addresses => nil,
  :persistent => false,
  :resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym,
  :resolver_options => { cache: true },
  :ip_families => ip_address_families,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Options

Returns a new instance of Options.



133
134
135
136
# File 'lib/httpx/options.rb', line 133

def initialize(options = {})
  __initialize__(options)
  freeze
end

Class Method Details

.def_option(optname, *args, &block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/httpx/options.rb', line 104

def def_option(optname, *args, &block)
  if args.empty? && !block
    class_eval(<<-OUT, __FILE__, __LINE__ + 1)
      def option_#{optname}(v); v; end # def option_smth(v); v; end
    OUT
    return
  end

  deprecated_def_option(optname, *args, &block)
end

.deprecated_def_option(optname, layout = nil, &interpreter) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/httpx/options.rb', line 115

def deprecated_def_option(optname, layout = nil, &interpreter)
  warn "DEPRECATION WARNING: using `def_option(#{optname})` for setting options is deprecated. " \
       "Define module OptionsMethods and `def option_#{optname}(val)` instead."

  if layout
    class_eval(<<-OUT, __FILE__, __LINE__ + 1)
      def option_#{optname}(value)  # def option_origin(v)
        #{layout}                   #   URI(v)
      end                           # end
    OUT
  elsif interpreter
    define_method(:"option_#{optname}") do |value|
      instance_exec(value, &interpreter)
    end
  end
end

.method_added(meth) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/httpx/options.rb', line 94

def method_added(meth)
  super

  return unless meth =~ /^option_(.+)$/

  optname = Regexp.last_match(1).to_sym

  attr_reader(optname)
end

.new(options = {}) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/httpx/options.rb', line 86

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



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/httpx/options.rb', line 230

def ==(other)
  ivars = instance_variables | other.instance_variables
  ivars.all? do |ivar|
    case ivar
    when :@headers
      # currently, this is used to pick up an available matching connection.
      # the headers do not play a role, as they are relevant only for the request.
      true
    when *REQUEST_IVARS
      true
    else
      instance_variable_get(ivar) == other.instance_variable_get(ivar)
    end
  end
end

#freezeObject



138
139
140
141
142
143
144
145
# File 'lib/httpx/options.rb', line 138

def freeze
  super
  @origin.freeze
  @base_path.freeze
  @timeout.freeze
  @headers.freeze
  @addresses.freeze
end

#initialize_dup(other) ⇒ Object



274
275
276
277
278
# File 'lib/httpx/options.rb', line 274

def initialize_dup(other)
  instance_variables.each do |ivar|
    instance_variable_set(ivar, other.instance_variable_get(ivar).dup)
  end
end

#merge(other) ⇒ Object

Raises:

  • (ArgumentError)


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/httpx/options.rb', line 246

def merge(other)
  raise ArgumentError, "#{other} is not a valid set of options" unless other.respond_to?(:to_hash)

  h2 = other.to_hash
  return self if h2.empty?

  h1 = to_hash

  return self if h1 >= h2

  merged = h1.merge(h2) do |_k, v1, v2|
    if v1.respond_to?(:merge) && v2.respond_to?(:merge)
      v1.merge(v2)
    else
      v2
    end
  end

  self.class.new(merged)
end

#option_addresses(value) ⇒ Object



209
210
211
# File 'lib/httpx/options.rb', line 209

def option_addresses(value)
  Array(value)
end

#option_base_path(value) ⇒ Object



151
152
153
# File 'lib/httpx/options.rb', line 151

def option_base_path(value)
  String(value)
end

#option_body_threshold_size(value) ⇒ Object



198
199
200
# File 'lib/httpx/options.rb', line 198

def option_body_threshold_size(value)
  Integer(value)
end

#option_buffer_size(value) ⇒ Object

Raises:

  • (TypeError)


190
191
192
193
194
195
196
# File 'lib/httpx/options.rb', line 190

def option_buffer_size(value)
  value = Integer(value)

  raise TypeError, ":buffer_size must be positive" unless value.positive?

  value
end

#option_headers(value) ⇒ Object



155
156
157
# File 'lib/httpx/options.rb', line 155

def option_headers(value)
  Headers.new(value)
end

#option_ip_families(value) ⇒ Object



213
214
215
# File 'lib/httpx/options.rb', line 213

def option_ip_families(value)
  Array(value)
end

#option_max_concurrent_requests(value) ⇒ Object

Raises:

  • (TypeError)


170
171
172
173
174
# File 'lib/httpx/options.rb', line 170

def option_max_concurrent_requests(value)
  raise TypeError, ":max_concurrent_requests must be positive" unless value.positive?

  value
end

#option_max_requests(value) ⇒ Object

Raises:

  • (TypeError)


176
177
178
179
180
# File 'lib/httpx/options.rb', line 176

def option_max_requests(value)
  raise TypeError, ":max_requests must be positive" unless value.positive?

  value
end

#option_origin(value) ⇒ Object



147
148
149
# File 'lib/httpx/options.rb', line 147

def option_origin(value)
  URI(value)
end

#option_timeout(value) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/httpx/options.rb', line 159

def option_timeout(value)
  timeouts = Hash[value]

  if timeouts.key?(:loop_timeout)
    warn ":loop_timeout is deprecated, use :operation_timeout instead"
    timeouts[:operation_timeout] = timeouts.delete(:loop_timeout)
  end

  timeouts
end

#option_transport(value) ⇒ Object

Raises:

  • (TypeError)


202
203
204
205
206
207
# File 'lib/httpx/options.rb', line 202

def option_transport(value)
  transport = value.to_s
  raise TypeError, "#{transport} is an unsupported transport type" unless %w[unix].include?(transport)

  transport
end

#option_window_size(value) ⇒ Object

Raises:

  • (TypeError)


182
183
184
185
186
187
188
# File 'lib/httpx/options.rb', line 182

def option_window_size(value)
  value = Integer(value)

  raise TypeError, ":window_size must be positive" unless value.positive?

  value
end

#to_hashObject



267
268
269
270
271
# File 'lib/httpx/options.rb', line 267

def to_hash
  instance_variables.each_with_object({}) do |ivar, hs|
    hs[ivar[1..-1].to_sym] = instance_variable_get(ivar)
  end
end