Class: HTTPX::Options

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

Constant Summary collapse

MAX_CONCURRENT_REQUESTS =
100
WINDOW_SIZE =

16K

1 << 14
MAX_BODY_THRESHOLD_SIZE =

112K

(1 << 10) * 112

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Options

Returns a new instance of Options.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/httpx/options.rb', line 39

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                  => {},
    :max_concurrent_requests  => MAX_CONCURRENT_REQUESTS,
    :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),
    :transport                => nil,
    :transport_options        => nil,
    :resolver_class           => (ENV["HTTPX_RESOLVER"] || :native).to_sym,
  }

  defaults.merge!(options)
  defaults[:headers] = Headers.new(defaults[:headers])
  defaults.each { |(k, v)| self[k] = v }
end

Class Method Details

.def_option(name, &interpreter) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/httpx/options.rb', line 26

def def_option(name, &interpreter)
  defined_options << name.to_sym
  interpreter ||= ->(v) { v }

  attr_accessor name
  protected :"#{name}="

  define_method(:"with_#{name}") do |value|
    dup { |opts| opts.send(:"#{name}=", instance_exec(value, &interpreter)) }
  end
end

.defined_optionsObject



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

def defined_options
  @defined_options ||= []
end

.inherited(klass) ⇒ Object



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

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

.new(options = {}) ⇒ Object



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

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

#dup {|dupped| ... } ⇒ Object

Yields:

  • (dupped)


126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/httpx/options.rb', line 126

def dup
  dupped = super
  dupped.headers             = headers.dup
  dupped.ssl                 = ssl.dup
  dupped.request_class       = request_class.dup
  dupped.response_class      = response_class.dup
  dupped.headers_class       = headers_class.dup
  dupped.request_body_class  = request_body_class.dup
  dupped.response_body_class = response_body_class.dup
  yield(dupped) if block_given?
  dupped
end

#merge(other) ⇒ Object



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

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



119
120
121
122
123
124
# File 'lib/httpx/options.rb', line 119

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