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

Returns a new instance of Options


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/http/options.rb', line 39

def initialize(options = {})
  defaults = {
    :response           => :auto,
    :proxy              => {},
    :timeout_class      => self.class.default_timeout_class,
    :timeout_options    => {},
    :socket_class       => self.class.default_socket_class,
    :ssl_socket_class   => self.class.default_ssl_socket_class,
    :ssl                => {},
    :keep_alive_timeout => 5,
    :headers            => {},
    :cookies            => {}
  }

  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

.default_socket_classObject

Returns the value of attribute default_socket_class


13
14
15
# File 'lib/http/options.rb', line 13

def default_socket_class
  @default_socket_class
end

.default_ssl_socket_classObject

Returns the value of attribute default_ssl_socket_class


13
14
15
# File 'lib/http/options.rb', line 13

def default_ssl_socket_class
  @default_ssl_socket_class
end

.default_timeout_classObject

Returns the value of attribute default_timeout_class


13
14
15
# File 'lib/http/options.rb', line 13

def default_timeout_class
  @default_timeout_class
end

Class Method Details

.defined_optionsObject


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

def defined_options
  @defined_options ||= []
end

.new(options = {}) ⇒ Object


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

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

Instance Method Details

#[](option) ⇒ Object

Deprecated.

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

def [](option)
  send(option)
rescue
  warn "[DEPRECATED] `HTTP::Options#[:#{option}]` was deprecated. " \
    "Use `HTTP::Options##{option}` instead."
  nil
end

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

Yields:

  • (dupped)

126
127
128
129
130
# File 'lib/http/options.rb', line 126

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

#follow=(value) ⇒ Object


77
78
79
80
81
82
83
84
# File 'lib/http/options.rb', line 77

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


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/http/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
      v1.merge(v2)
    else
      v2
    end
  end

  self.class.new(merged)
end

#persistent=(value) ⇒ Object


86
87
88
# File 'lib/http/options.rb', line 86

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

#persistent?Boolean

Returns:

  • (Boolean)

90
91
92
# File 'lib/http/options.rb', line 90

def persistent?
  !persistent.nil?
end

#to_hashObject


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

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