Class: Antaeus::UserAPIClient
- Defined in:
- lib/antaeus-sdk/user_api_client.rb
Instance Method Summary collapse
- #authenticate(login, pass) ⇒ Object
- #authenticated? ⇒ Boolean
- #connect ⇒ Object
-
#get_token(login) ⇒ Object
Retrieve the token for a given user.
-
#set_token(login, token) ⇒ Object
Set the token in memory for the given user.
Methods inherited from APIClient
#connected?, #delete, #get, #initialize, instance, #patch, #post, #put, #raw, #refresh_token
Constructor Details
This class inherits a constructor from Antaeus::APIClient
Instance Method Details
#authenticate(login, pass) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/antaeus-sdk/user_api_client.rb', line 3 def authenticate(login, pass) @login ||= login begin raw_token_data = RestClient.post( "#{Antaeus.config.base_url}/users/authenticate", { login: @login, password: pass }.to_json, content_type: :json, accept: :json ) token_data = JSON.load(raw_token_data) set_token(@login, token_data['api_token']) true rescue RestClient::Exception => e raise Exceptions::AuthenticationFailure, e.response end end |
#authenticated? ⇒ Boolean
38 39 40 41 42 43 44 |
# File 'lib/antaeus-sdk/user_api_client.rb', line 38 def authenticated? if @login && @user_data[@login] && @user_data[@login][:token] true else false end end |
#connect ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/antaeus-sdk/user_api_client.rb', line 46 def connect return true if connected? if authenticated? @rest_client = RestClient::Resource.new( Antaeus.config.base_url, content_type: :json, accept: :json, headers: { 'X-API-Token:': @user_data[@login][:token] } ) else raise Exceptions::LoginRequired end end |
#get_token(login) ⇒ Object
Retrieve the token for a given user
30 31 32 33 34 35 36 |
# File 'lib/antaeus-sdk/user_api_client.rb', line 30 def get_token(login) return nil unless @login return nil unless @login == login return nil unless @user_data return nil unless @user_data[login] @user_data[login][:token] ? @user_data[login][:token].dup : nil end |
#set_token(login, token) ⇒ Object
Set the token in memory for the given user
21 22 23 24 25 26 27 |
# File 'lib/antaeus-sdk/user_api_client.rb', line 21 def set_token(login, token) # this should probably be improved to handle race conditions @login ||= login @user_data ||= {} @user_data[login] ||= {} @user_data[login][:token] = token end |