Class: Insnergy::Client

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

Defined Under Namespace

Classes: Power, Sensor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
# File 'lib/insnergy-api-ruby-client.rb', line 7

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
  @user_id = nil
  refresh!
   user_id!
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



5
6
7
# File 'lib/insnergy-api-ruby-client.rb', line 5

def access_token
  @access_token
end

#domainObject

Returns the value of attribute domain.



4
5
6
# File 'lib/insnergy-api-ruby-client.rb', line 4

def domain
  @domain
end

#oauth_keyObject

Returns the value of attribute oauth_key.



4
5
6
# File 'lib/insnergy-api-ruby-client.rb', line 4

def oauth_key
  @oauth_key
end

#oauth_secertObject

Returns the value of attribute oauth_secert.



4
5
6
# File 'lib/insnergy-api-ruby-client.rb', line 4

def oauth_secert
  @oauth_secert
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



4
5
6
# File 'lib/insnergy-api-ruby-client.rb', line 4

def refresh_token
  @refresh_token
end

#user_idObject (readonly)

Returns the value of attribute user_id.



5
6
7
# File 'lib/insnergy-api-ruby-client.rb', line 5

def user_id
  @user_id
end

Instance Method Details

#power(device_id: nil, start_time: nil, end_time: nil) ⇒ Object



35
36
37
38
# File 'lib/insnergy-api-ruby-client.rb', line 35

def power(device_id: nil, start_time: nil, end_time: nil)
  client = Client::Power.new(client: self, device_id: device_id, start_time: start_time, end_time: end_time)
  client.get_device
end

#refresh!Object



17
18
19
20
21
22
23
# File 'lib/insnergy-api-ruby-client.rb', line 17

def refresh!
   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 "<refresh_token is nil>\n#{response}" unless response['refresh_token'] != nil
   raise "<access_token is nil>\n#{response}" unless response['access_token'] != nil
   @refresh_token = response['refresh_token']
   @access_token = response['access_token']   
end

#sensorObject



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

def sensor
 Client::Sensor.new(client: self, category: 'sensor')
end

#user_id!Object



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

def user_id!
   response = JSON.parse(RestClient.get "#{@domain}/if/3/user/me" ,{:Authorization => "Bearer #{@access_token}"})
   raise "<user_id is nil>\n#{response}" unless response['user']['user_id'] != nil
   @user_id = response['user']['user_id']
end