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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/http/options.rb', line 48

def initialize(options = {})
  @response  = options[:response]  || :auto
  @headers   = options[:headers]   || {}
  @proxy     = options[:proxy]     || {}
  @callbacks = options[:callbacks] || {:request => [], :response => []}
  @body      = options[:body]
  @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.



40
41
42
# File 'lib/http/options.rb', line 40

def default_socket_class
  @default_socket_class
end

.default_ssl_socket_classObject

Returns the value of attribute default_ssl_socket_class.



40
41
42
# File 'lib/http/options.rb', line 40

def default_ssl_socket_class
  @default_ssl_socket_class
end

Instance Attribute Details

#bodyObject

Explicit request body of the request



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

def body
  @body
end

#callbacksObject

Before callbacks



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

def callbacks
  @callbacks
end

#followObject

Follow redirects



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

def follow
  @follow
end

#formObject

Form data to embed in the request



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

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

#proxyObject

Http proxy to route request



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

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



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

def socket_class
  @socket_class
end

#ssl_contextObject

SSL context



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

def ssl_context
  @ssl_context
end

#ssl_socket_classObject

Socket classes



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

def ssl_socket_class
  @ssl_socket_class
end

Class Method Details

.new(options = {}) ⇒ Object



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

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

Instance Method Details

#[](option) ⇒ Object



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

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

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

Yields:

  • (dupped)


151
152
153
154
155
# File 'lib/http/options.rb', line 151

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

#merge(other) ⇒ Object



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

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
  Options.new(merged)
end

#to_hashObject



141
142
143
144
145
146
147
148
149
# File 'lib/http/options.rb', line 141

def to_hash
  {:response  => response,
   :headers   => headers,
   :proxy     => proxy,
   :form      => form,
   :body      => body,
   :callbacks => callbacks,
   :follow    => follow}
end

#with_body(body) ⇒ Object



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

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

#with_callback(event, callback) ⇒ Object



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

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



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

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

#with_form(form) ⇒ Object



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

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

#with_headers(headers) ⇒ Object



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

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_proxy(proxy_hash) ⇒ Object



82
83
84
85
86
# File 'lib/http/options.rb', line 82

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

#with_response(response) ⇒ Object



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

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