Class: HTTP::Options

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Options

rubocop:disable Style/OptionHash



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

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

Class Attribute Details

.available_featuresObject (readonly)

Returns the value of attribute available_features.



25
26
27
# File 'lib/http/options.rb', line 25

def available_features
  @available_features
end

.default_socket_classObject

Returns the value of attribute default_socket_class.



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

def default_socket_class
  @default_socket_class
end

.default_ssl_socket_classObject

Returns the value of attribute default_ssl_socket_class.



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

def default_ssl_socket_class
  @default_ssl_socket_class
end

.default_timeout_classObject

Returns the value of attribute default_timeout_class.



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

def default_timeout_class
  @default_timeout_class
end

Class Method Details

.defined_optionsObject



32
33
34
# File 'lib/http/options.rb', line 32

def defined_options
  @defined_options ||= []
end

.new(options = {}) ⇒ Object

rubocop:disable Style/OptionHash



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

def new(options = {}) # rubocop:disable Style/OptionHash
  return options if options.is_a?(self)
  super
end

Instance Method Details

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

Yields:

  • (dupped)


169
170
171
172
173
# File 'lib/http/options.rb', line 169

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

#feature(name) ⇒ Object



175
176
177
# File 'lib/http/options.rb', line 175

def feature(name)
  features[name]
end

#features=(features) ⇒ Object



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

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



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

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/http/options.rb', line 146

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



138
139
140
# File 'lib/http/options.rb', line 138

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

#persistent?Boolean

Returns:

  • (Boolean)


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

def persistent?
  !persistent.nil?
end

#to_hashObject



162
163
164
165
166
167
# File 'lib/http/options.rb', line 162

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