Class: HTTP::Options

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Options


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

def initialize(options = {})
  @response  = options[:response]  || :auto
  @proxy     = options[:proxy]     || {}
  @body      = options[:body]
  @params    = options[:params]
  @form      = options[:form]
  @json      = options[:json]
  @follow    = options[:follow]

  @headers   = HTTP::Headers.coerce(options[:headers] || {})

  @socket_class     = options[:socket_class]     || self.class.default_socket_class
  @ssl_socket_class = options[:ssl_socket_class] || self.class.default_ssl_socket_class
  @ssl_context      = options[:ssl_context]
end

Class Attribute Details

.default_socket_classObject

Returns the value of attribute default_socket_class


43
44
45
# File 'lib/http/options.rb', line 43

def default_socket_class
  @default_socket_class
end

.default_ssl_socket_classObject

Returns the value of attribute default_ssl_socket_class


43
44
45
# File 'lib/http/options.rb', line 43

def default_ssl_socket_class
  @default_ssl_socket_class
end

Instance Attribute Details

#bodyObject

Explicit request body of the request


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

def body
  @body
end

#followObject

Follow redirects


35
36
37
# File 'lib/http/options.rb', line 35

def follow
  @follow
end

#formObject

Form data to embed in the request


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

def form
  @form
end

#headersObject

HTTP headers to include in the request


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

def headers
  @headers
end

#jsonObject

JSON data to embed in the request


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

def json
  @json
end

#paramsObject

Query string params to add to the url


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

def params
  @params
end

#proxyObject

HTTP proxy to route request


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

def proxy
  @proxy
end

#responseObject

How to format the response [:object, :body, :parse_body]


8
9
10
# File 'lib/http/options.rb', line 8

def response
  @response
end

#socket_classObject

Socket classes


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

def socket_class
  @socket_class
end

#ssl_contextObject

SSL context


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

def ssl_context
  @ssl_context
end

#ssl_socket_classObject

Socket classes


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

def ssl_socket_class
  @ssl_socket_class
end

Class Method Details

.new(options = {}) ⇒ Object


45
46
47
48
# File 'lib/http/options.rb', line 45

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

Instance Method Details

#[](option) ⇒ Object


81
82
83
# File 'lib/http/options.rb', line 81

def [](option)
  send(option) rescue nil
end

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

Yields:

  • (dupped)

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

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

#merge(other) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/http/options.rb', line 85

def merge(other)
  h1, h2 = to_hash, 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

#to_hashObject


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/http/options.rb', line 99

def to_hash
  # FIXME: hardcoding these fields blows! We should have a declarative
  # way of specifying all the options fields, and ensure they *all*
  # get serialized here, rather than manually having to add them each time
  {
    :response         => response,
    :headers          => headers.to_h,
    :proxy            => proxy,
    :params           => params,
    :form             => form,
    :json             => json,
    :body             => body,
    :follow           => follow,
    :socket_class     => socket_class,
    :ssl_socket_class => ssl_socket_class,
    :ssl_context      => ssl_context
  }
end

#with_headers(headers) ⇒ Object


67
68
69
70
71
# File 'lib/http/options.rb', line 67

def with_headers(headers)
  dup do |opts|
    opts.headers = self.headers.merge(headers)
  end
end