Class: HTTP::Options

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

Overview

rubocop:disable Metrics/ClassLength

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Options

Returns a new instance of Options.


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/http/options.rb', line 50

def initialize(options = {})
  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

Class Attribute Details

.available_featuresObject (readonly)

Returns the value of attribute available_features


17
18
19
# File 'lib/http/options.rb', line 17

def available_features
  @available_features
end

.default_socket_classObject

Returns the value of attribute default_socket_class


16
17
18
# File 'lib/http/options.rb', line 16

def default_socket_class
  @default_socket_class
end

.default_ssl_socket_classObject

Returns the value of attribute default_ssl_socket_class


16
17
18
# File 'lib/http/options.rb', line 16

def default_ssl_socket_class
  @default_ssl_socket_class
end

.default_timeout_classObject

Returns the value of attribute default_timeout_class


16
17
18
# File 'lib/http/options.rb', line 16

def default_timeout_class
  @default_timeout_class
end

Class Method Details

.defined_optionsObject


23
24
25
# File 'lib/http/options.rb', line 23

def defined_options
  @defined_options ||= []
end

.new(options = {}) ⇒ Object


19
20
21
# File 'lib/http/options.rb', line 19

def new(options = {})
  options.is_a?(self) ? options : super
end

.register_feature(name, impl) ⇒ Object


27
28
29
# File 'lib/http/options.rb', line 27

def register_feature(name, impl)
  @available_features[name] = impl
end

Instance Method Details

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

Yields:

  • (dupped)

172
173
174
175
176
# File 'lib/http/options.rb', line 172

def dup
  dupped = super
  yield(dupped) if block_given?
  dupped
end

#feature(name) ⇒ Object


178
179
180
# File 'lib/http/options.rb', line 178

def feature(name)
  features[name]
end

#features=(features) ⇒ Object


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

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

#follow=(value) ⇒ Object


129
130
131
132
133
134
135
136
137
# File 'lib/http/options.rb', line 129

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

#merge(other) ⇒ Object


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

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

#persistent=(value) ⇒ Object


141
142
143
# File 'lib/http/options.rb', line 141

def persistent=(value)
  @persistent = value ? HTTP::URI.parse(value).origin : nil
end

#persistent?Boolean

Returns:

  • (Boolean)

145
146
147
# File 'lib/http/options.rb', line 145

def persistent?
  !persistent.nil?
end

#to_hashObject


165
166
167
168
169
170
# File 'lib/http/options.rb', line 165

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