Class: Strobe::Connection

Inherits:
Object
  • Object
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, headers = {})
  @host, @port = self.class.host_and_port(url)
  @headers     = headers
  @user        = nil
  @password    = nil
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



22
23
24
# File 'lib/strobe/connection.rb', line 22

def host
  @host
end

#portObject (readonly)

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, headers = {})
  path   = path.dup
  method = method.to_s.upcase

  headers.keys.each do |key|
    headers[key.to_s.downcase] = headers.delete(key)
  end

  # add connection-global headers, which will be
  # overwritten by request-specific headers
  @headers.each do |k, v|
    headers[k.to_s.downcase] ||= v if v
  end

  headers['authorization'] ||= authorization_header
  headers['user-agent']      = Strobe.user_agent
  headers['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
      headers['content-type'] ||= 'application/json'
    end

    if headers['content-type'] == 'application/json'
      body = ActiveSupport::JSON.encode(body)
    end
  end

  request = Request.new(self, method, path, body, headers)

  Response.new(request.send, request)
end