Class: Insnergy::Client::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/insnergy-api-ruby-client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain: nil, oauth_key: nil, oauth_secert: nil, refresh_token: nil) ⇒ Token

Returns a new instance of Token.



11
12
13
14
15
16
17
18
19
20
# File 'lib/insnergy-api-ruby-client.rb', line 11

def initialize(domain: nil, oauth_key: nil, oauth_secert: nil, refresh_token: nil)
  @domain = domain
   @oauth_key = oauth_key
  @oauth_secert = oauth_secert
  @refresh_token = refresh_token
   @access_token = nil
   @user_id = nil
  token!
   user_id!
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



9
10
11
# File 'lib/insnergy-api-ruby-client.rb', line 9

def access_token
  @access_token
end

#domainObject

Returns the value of attribute domain.



8
9
10
# File 'lib/insnergy-api-ruby-client.rb', line 8

def domain
  @domain
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



9
10
11
# File 'lib/insnergy-api-ruby-client.rb', line 9

def expires_at
  @expires_at
end

#oauth_keyObject

Returns the value of attribute oauth_key.



8
9
10
# File 'lib/insnergy-api-ruby-client.rb', line 8

def oauth_key
  @oauth_key
end

#oauth_secertObject

Returns the value of attribute oauth_secert.



8
9
10
# File 'lib/insnergy-api-ruby-client.rb', line 8

def oauth_secert
  @oauth_secert
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



8
9
10
# File 'lib/insnergy-api-ruby-client.rb', line 8

def refresh_token
  @refresh_token
end

#user_idObject (readonly)

Returns the value of attribute user_id.



9
10
11
# File 'lib/insnergy-api-ruby-client.rb', line 9

def user_id
  @user_id
end

Instance Method Details

#ok?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/insnergy-api-ruby-client.rb', line 37

def ok?
  begin
    JSON.parse(RestClient.get "#{@domain}/if/3/user/me" ,{:Authorization => "Bearer #{@access_token}"})
    return true
  rescue Exception => e
    if %w(401\ Unauthorized 7104).include? e.message
      return false
    else
      raise e
    end
  end
end

#token!Object



22
23
24
25
26
27
28
# File 'lib/insnergy-api-ruby-client.rb', line 22

def token!
   response = JSON.parse(RestClient.post "#{@domain}/if/oauth/token" ,{:client_id => @oauth_key, :client_secret => @oauth_secert, :absytem => 'IFA', :grant_type => 'refresh_token', :refresh_token => @refresh_token }, :accept => :json)
   raise "<no got refresh_token>\n#{response}" unless response.key?('refresh_token')
   raise "<no got access_token>\n#{response}" unless response.key?('access_token')
   @refresh_token = response['refresh_token']
   @access_token = response['access_token']		
end

#user_id!Object



30
31
32
33
34
35
# File 'lib/insnergy-api-ruby-client.rb', line 30

def user_id!
   response = JSON.parse(RestClient.get "#{@domain}/if/3/user/me" ,{:Authorization => "Bearer #{@access_token}"})
   raise "<no got user_id>\n#{response}" unless response.key?('user') && response['user'].key?('user_id')
   @user_id = response['user']['user_id']
   @expires_at = Time.at(response['token']['expires_at']/1000)
end