-
Notifications
You must be signed in to change notification settings - Fork 318
/
options.rb
196 lines (161 loc) · 4.88 KB
/
options.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
51
52
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# frozen_string_literal: true
require "http/headers"
require "openssl"
require "socket"
require "http/uri"
module HTTP
class Options # rubocop:disable Metrics/ClassLength
@default_socket_class = TCPSocket
@default_ssl_socket_class = OpenSSL::SSL::SSLSocket
@default_timeout_class = HTTP::Timeout::Null
@available_features = {}
class << self
attr_accessor :default_socket_class, :default_ssl_socket_class, :default_timeout_class
attr_reader :available_features
def new(options = {})
return options if options.is_a?(self)
super
end
def defined_options
@defined_options ||= []
end
def register_feature(name, impl)
@available_features[name] = impl
end
protected
def def_option(name, reader_only: false, &interpreter)
defined_options << name.to_sym
interpreter ||= lambda { |v| v }
if reader_only
attr_reader name
else
attr_accessor name
protected :"#{name}="
end
define_method(:"with_#{name}") do |value|
dup { |opts| opts.send(:"#{name}=", instance_exec(value, &interpreter)) }
end
end
end
def initialize(options = {}) # rubocop:disable Style/OptionHash
defaults = {
:response => :auto,
:proxy => {},
:timeout_class => self.class.default_timeout_class,
:timeout_options => {},
:socket_class => self.class.default_socket_class,
:nodelay => false,
:ssl_socket_class => self.class.default_ssl_socket_class,
:ssl => {},
:keep_alive_timeout => 5,
:headers => {},
:cookies => {},
:encoding => nil,
:features => {}
}
opts_w_defaults = defaults.merge(options)
opts_w_defaults[:headers] = HTTP::Headers.coerce(opts_w_defaults[:headers])
opts_w_defaults.each { |(k, v)| self[k] = v }
end
def_option :headers do |new_headers|
headers.merge(new_headers)
end
def_option :cookies do |new_cookies|
new_cookies.each_with_object cookies.dup do |(k, v), jar|
cookie = k.is_a?(Cookie) ? k : Cookie.new(k.to_s, v.to_s)
jar[cookie.name] = cookie.cookie_value
end
end
def_option :encoding do |encoding|
self.encoding = Encoding.find(encoding)
end
def_option :features, :reader_only => true do |new_features|
# Normalize features from:
#
# [{feature_one: {opt: 'val'}}, :feature_two]
#
# into:
#
# {feature_one: {opt: 'val'}, feature_two: {}}
normalized_features = new_features.each_with_object({}) do |feature, h|
if feature.is_a?(Hash)
h.merge!(feature)
else
h[feature] = {}
end
end
features.merge(normalized_features)
end
def features=(features)
@features = features.each_with_object({}) do |(name, opts_or_feature), h|
h[name] = if opts_or_feature.is_a?(Feature)
opts_or_feature
else
unless (feature = self.class.available_features[name])
argument_error! "Unsupported feature: #{name}"
end
feature.new(opts_or_feature)
end
end
end
%w[
proxy params form json body response
socket_class nodelay ssl_socket_class ssl_context ssl
keep_alive_timeout timeout_class timeout_options
].each do |method_name|
def_option method_name
end
def_option :follow, :reader_only => true
def follow=(value)
@follow =
case
when !value then nil
when true == value then {}
when value.respond_to?(:fetch) then value
else argument_error! "Unsupported follow options: #{value}"
end
end
def_option :persistent, :reader_only => true
def persistent=(value)
@persistent = value ? HTTP::URI.parse(value).origin : nil
end
def persistent?
!persistent.nil?
end
def merge(other)
h1 = to_hash
h2 = other.to_hash
merged = h1.merge(h2) do |k, v1, v2|
case k
when :headers
v1.merge(v2)
else
v2
end
end
self.class.new(merged)
end
def to_hash
hash_pairs = self.class.
defined_options.
flat_map { |opt_name| [opt_name, send(opt_name)] }
Hash[*hash_pairs]
end
def dup
dupped = super
yield(dupped) if block_given?
dupped
end
def feature(name)
features[name]
end
protected
def []=(option, val)
send(:"#{option}=", val)
end
private
def argument_error!(message)
raise(Error, message, caller(1..-1))
end
end
end