Class: Cocupu::Connection
- Inherits:
-
Object
- Object
- Cocupu::Connection
- Includes:
- HTTMultiParty
- Defined in:
- lib/cocupu.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #get(path) ⇒ Object
- #identities ⇒ Object
- #identity(short_name) ⇒ Object
-
#initialize(email, password, port = 80, host = 'localhost') ⇒ Connection
constructor
A new instance of Connection.
- #post(path, args = {}) ⇒ Object
- #put(path, args = {}) ⇒ Object
- #request_url(path) ⇒ Object
Constructor Details
#initialize(email, password, port = 80, host = 'localhost') ⇒ Connection
Returns a new instance of Connection.
15 16 17 18 19 20 21 |
# File 'lib/cocupu.rb', line 15 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
#host ⇒ Object
Returns the value of attribute host.
13 14 15 |
# File 'lib/cocupu.rb', line 13 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
13 14 15 |
# File 'lib/cocupu.rb', line 13 def port @port end |
#token ⇒ Object
Returns the value of attribute token.
13 14 15 |
# File 'lib/cocupu.rb', line 13 def token @token end |
Instance Method Details
#get(path) ⇒ Object
23 24 25 |
# File 'lib/cocupu.rb', line 23 def get(path) self.class.get(request_url(path)) end |
#identities ⇒ Object
39 40 41 42 43 44 |
# File 'lib/cocupu.rb', line 39 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)} end |
#identity(short_name) ⇒ Object
46 47 48 |
# File 'lib/cocupu.rb', line 46 def identity(short_name) identities.find{|i| i.short_name == short_name} end |
#post(path, args = {}) ⇒ Object
31 32 33 |
# File 'lib/cocupu.rb', line 31 def post(path, args={}) self.class.post(request_url(path), args) end |
#put(path, args = {}) ⇒ Object
27 28 29 |
# File 'lib/cocupu.rb', line 27 def put(path, args={}) self.class.put(request_url(path), args) end |
#request_url(path) ⇒ Object
35 36 37 |
# File 'lib/cocupu.rb', line 35 def request_url(path) "http://#{host}:#{port}#{path}?auth_token=#{token}" end |