Class: Strobe::Connection
- Inherits:
-
Object
- Object
- Strobe::Connection
show all
- Defined in:
- lib/strobe/connection.rb
Defined Under Namespace
Classes: Request, Response
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(url, headers = {}) ⇒ Connection
Returns a new instance of Connection.
24
25
26
27
28
29
|
# File 'lib/strobe/connection.rb', line 24
def initialize(url, = {})
@host, @port = self.class.host_and_port(url)
@headers =
@user = nil
@password = nil
end
|
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
22
23
24
|
# File 'lib/strobe/connection.rb', line 22
def host
@host
end
|
#port ⇒ Object
Returns the value of attribute port.
22
23
24
|
# File 'lib/strobe/connection.rb', line 22
def port
@port
end
|
Class Method Details
.host_and_port(uri) ⇒ Object
16
17
18
19
20
|
# File 'lib/strobe/connection.rb', line 16
def self.host_and_port(uri)
uri = "http://#{uri}" unless uri =~ %r[^https?://]
uri = URI(uri)
[ uri.host, uri.port ]
end
|
Instance Method Details
#authenticate_with_token(token) ⇒ Object
31
32
33
|
# File 'lib/strobe/connection.rb', line 31
def authenticate_with_token(token)
authenticate_with_user_and_password(token, '')
end
|
#authenticate_with_user_and_password(user, password) ⇒ Object
35
36
37
38
39
|
# File 'lib/strobe/connection.rb', line 35
def authenticate_with_user_and_password(user, password)
@json_connection = nil
@reg_connection = nil
@user, @password = user, password
end
|
#delete(*args) ⇒ Object
53
54
55
|
# File 'lib/strobe/connection.rb', line 53
def delete(*args)
request :delete, *args
end
|
#get(*args) ⇒ Object
41
42
43
|
# File 'lib/strobe/connection.rb', line 41
def get(*args)
request :get, *args
end
|
#post(*args) ⇒ Object
45
46
47
|
# File 'lib/strobe/connection.rb', line 45
def post(*args)
request :post, *args
end
|
#put(*args) ⇒ Object
49
50
51
|
# File 'lib/strobe/connection.rb', line 49
def put(*args)
request :put, *args
end
|
#request(method, path, body = nil, headers = {}) ⇒ Object
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/strobe/connection.rb', line 191
def request(method, path, body = nil, = {})
path = path.dup
method = method.to_s.upcase
.keys.each do |key|
[key.to_s.downcase] = .delete(key)
end
@headers.each do |k, v|
[k.to_s.downcase] ||= v if v
end
['authorization'] ||=
['user-agent'] = Strobe.user_agent
['accept'] = 'application/json'
if method == "GET" || method == "HEAD"
case body
when Hash
qs = body.map { |k,v| "#{k}=#{v}" }.join('&')
if path.include?('?')
path << "&#{qs}"
else
path << "?#{qs}"
end
end
body = nil
end
if body
if Hash === body
['content-type'] ||= 'application/json'
end
if ['content-type'] == 'application/json'
body = ActiveSupport::JSON.encode(body)
end
end
request = Request.new(self, method, path, body, )
Response.new(request.send, request)
end
|