Class: Strobe::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/strobe/connection.rb

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
12
# File 'lib/strobe/connection.rb', line 8

def initialize(url)
  @url      = URI(url)
  @user     = nil
  @password = nil
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/strobe/connection.rb', line 6

def url
  @url
end

Instance Method Details

#authenticate_with_token(token) ⇒ Object



14
15
16
# File 'lib/strobe/connection.rb', line 14

def authenticate_with_token(token)
  authenticate_with_user_and_password(token, '')
end

#authenticate_with_user_and_password(user, password) ⇒ Object



18
19
20
21
22
# File 'lib/strobe/connection.rb', line 18

def authenticate_with_user_and_password(user, password)
  @json_connection = nil
  @reg_connection  = nil
  @user, @password = user, password
end

#delete(*args) ⇒ Object



36
37
38
# File 'lib/strobe/connection.rb', line 36

def delete(*args)
  request :delete, *args
end

#get(*args) ⇒ Object



24
25
26
# File 'lib/strobe/connection.rb', line 24

def get(*args)
  request :get, *args
end

#post(*args) ⇒ Object



28
29
30
# File 'lib/strobe/connection.rb', line 28

def post(*args)
  request :post, *args
end

#put(*args) ⇒ Object



32
33
34
# File 'lib/strobe/connection.rb', line 32

def put(*args)
  request :put, *args
end

#request(method, path, body = nil, headers = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/strobe/connection.rb', line 99

def request(method, path, body = nil, headers = {})
  headers.keys.each do |key|
    headers[key.downcase] = headers.delete(key)
  end

  headers['authorization'] ||= authorization_header

  if body
    headers['content-type'] ||= 'application/json'

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

  http    = build_http
  request = Net::HTTPGenericRequest.new(
    method.to_s.upcase, !!body, true, path, headers)

  if body.respond_to?(:read)
    request.body_stream = body
    body = nil
  end

  Response.new(http.request(request, body))
end