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

Returns a new instance of 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



109
110
111
# File 'lib/http/options.rb', line 109

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

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

Yields:

  • (dupped)


146
147
148
149
150
# File 'lib/http/options.rb', line 146

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

#merge(other) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/http/options.rb', line 113

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/http/options.rb', line 127

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_body(body) ⇒ Object



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

def with_body(body)
  dup do |opts|
    opts.body = body
  end
end

#with_follow(follow) ⇒ Object



103
104
105
106
107
# File 'lib/http/options.rb', line 103

def with_follow(follow)
  dup do |opts|
    opts.follow = follow
  end
end

#with_form(form) ⇒ Object



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

def with_form(form)
  dup do |opts|
    opts.form = form
  end
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

#with_json(data) ⇒ Object



91
92
93
94
95
# File 'lib/http/options.rb', line 91

def with_json(data)
  dup do |opts|
    opts.json = data
  end
end

#with_params(params) ⇒ Object



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

def with_params(params)
  dup do |opts|
    opts.params = params
  end
end

#with_proxy(proxy_hash) ⇒ Object



73
74
75
76
77
# File 'lib/http/options.rb', line 73

def with_proxy(proxy_hash)
  dup do |opts|
    opts.proxy = proxy_hash
  end
end