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.



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

def initialize(options = {})
  @response  = options[:response]  || :auto
  @headers   = options[:headers]   || {}
  @proxy     = options[:proxy]     || {}
  @callbacks = options[:callbacks] || {:request => [], :response => []}
  @body      = options[:body]
  @params      = options[:params]
  @form      = options[:form]
  @follow    = options[:follow]

  @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]

  @headers["User-Agent"] ||= "RubyHTTPGem/#{HTTP::VERSION}"
end

Class Attribute Details

.default_socket_classObject

Returns the value of attribute default_socket_class.



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

def default_socket_class
  @default_socket_class
end

.default_ssl_socket_classObject

Returns the value of attribute default_ssl_socket_class.



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

def default_ssl_socket_class
  @default_ssl_socket_class
end

Instance Attribute Details

#bodyObject

Explicit request body of the request



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

def body
  @body
end

#callbacksObject

Before callbacks



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

def callbacks
  @callbacks
end

#followObject

Follow redirects



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

def follow
  @follow
end

#formObject

Form data to embed in the request



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

def form
  @form
end

#headersObject

HTTP headers to include in the request



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

def headers
  @headers
end

#paramsObject

Query string params to add to the url



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

def params
  @params
end

#proxyObject

HTTP proxy to route request



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

def proxy
  @proxy
end

#responseObject

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



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

def response
  @response
end

#socket_classObject

Socket classes



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

def socket_class
  @socket_class
end

#ssl_contextObject

SSL context



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

def ssl_context
  @ssl_context
end

#ssl_socket_classObject

Socket classes



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

def ssl_socket_class
  @ssl_socket_class
end

Class Method Details

.new(options = {}) ⇒ Object



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

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

Instance Method Details

#[](option) ⇒ Object



133
134
135
# File 'lib/http/options.rb', line 133

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

#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

#merge(other) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/http/options.rb', line 137

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)
    when :callbacks
      v1.merge(v2){|event,l,r| (l+r).uniq}
    else
      v2
    end
  end

  self.class.new(merged)
end

#to_hashObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/http/options.rb', line 153

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,
    :proxy            => proxy,
    :params           => params,
    :form             => form,
    :body             => body,
    :callbacks        => callbacks,
    :follow           => follow,
    :socket_class     => socket_class,
    :ssl_socket_class => ssl_socket_class,
    :ssl_context      => ssl_context
 }
end

#with_body(body) ⇒ Object



105
106
107
108
109
# File 'lib/http/options.rb', line 105

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

#with_callback(event, callback) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/http/options.rb', line 117

def with_callback(event, callback)
  unless callback.respond_to?(:call)
    argument_error! "invalid callback: #{callback}"
  end
  unless callback.respond_to?(:arity) and callback.arity == 1
    argument_error! "callback must accept only one argument"
  end
  unless [:request, :response].include?(event)
    argument_error! "invalid callback event: #{event}"
  end
  dup do |opts|
    opts.callbacks = callbacks.dup
    opts.callbacks[event] = (callbacks[event].dup << callback)
  end
end

#with_follow(follow) ⇒ Object



111
112
113
114
115
# File 'lib/http/options.rb', line 111

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

#with_form(form) ⇒ Object



99
100
101
102
103
# File 'lib/http/options.rb', line 99

def with_form(form)
  dup do |opts|
    opts.form = form
  end
end

#with_headers(headers) ⇒ Object



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

def with_headers(headers)
  unless headers.respond_to?(:to_hash)
    argument_error! "invalid headers: #{headers}"
  end
  dup do |opts|
    opts.headers = self.headers.merge(headers.to_hash)
  end
end

#with_params(params) ⇒ Object



93
94
95
96
97
# File 'lib/http/options.rb', line 93

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

#with_proxy(proxy_hash) ⇒ Object



87
88
89
90
91
# File 'lib/http/options.rb', line 87

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

#with_response(response) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/http/options.rb', line 69

def with_response(response)
  unless [:auto, :object, :body, :parsed_body].include?(response)
    argument_error! "invalid response type: #{response}"
  end
  dup do |opts|
    opts.response = response
  end
end