Class: Cocupu::Connection

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/cocupu.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, password, port = 80, host = 'localhost') ⇒ Connection

Returns a new instance of Connection.



14
15
16
17
18
19
20
# File 'lib/cocupu.rb', line 14

def initialize(email, password, port=80, host='localhost')
  self.host = host
  self.port = port
  response = self.class.post("http://#{host}:#{port}/api/v1/tokens", body: {email: email, password: password})
  raise "Error logging in: #{response}" unless response.code == 200
  self.token = response["token"]
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#get(path) ⇒ Object



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

def get(path)
    self.class.get(request_url(path))
end

#identitiesObject



38
39
40
41
42
43
# File 'lib/cocupu.rb', line 38

def identities
  return @identities if @identities
  response = self.class.get("http://#{host}:#{port}/identities?auth_token=#{token}")
  raise "Error getting identities: #{response}" unless response.code == 200
  @identities = response.map {|val| Identity.new(val, self)}
end

#identity(short_name) ⇒ Object



45
46
47
# File 'lib/cocupu.rb', line 45

def identity(short_name)
  identities.find{|i| i.short_name == short_name}
end

#post(path, args = {}) ⇒ Object



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

def post(path, args={})
    self.class.post(request_url(path), args)
end

#put(path, args = {}) ⇒ Object



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

def put(path, args={})
    self.class.put(request_url(path), args)
end

#request_url(path) ⇒ Object



34
35
36
# File 'lib/cocupu.rb', line 34

def request_url(path)
  "http://#{host}:#{port}#{path}?auth_token=#{token}"
end