Class: Atmo::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/atmo/api.rb

Instance Method Summary collapse

Instance Method Details

#authenticateObject



3
4
5
6
7
8
9
# File 'lib/atmo/api.rb', line 3

def authenticate
  if @access_token && @expires_at > Time.now
    return true
  else
    get_token
  end
end

#get_station_dataObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/atmo/api.rb', line 36

def get_station_data
  response = connection.get '/api/getstationsdata',  access_token: @access_token

  if response.status == 200
    data = JSON.parse(response.body)
    data
  else
    raise "Unauthenticated"
  end
end

#get_tokenObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/atmo/api.rb', line 11

def get_token
  if @refresh_token
    response = connection.post '/oauth2/token' do |request|
      request.body = {
        grant_type: :refresh_token,
        refresh_token: @refresh_token,
        client_id: ENV['CLIENT_ID'],
        client_secret: ENV['CLIENT_SECRET']
      }
    end
  else
    response = connection.post '/oauth2/token' do |request|
      request.body = {
        grant_type: :password,
        client_id: ENV['CLIENT_ID'],
        client_secret: ENV['CLIENT_SECRET'],
        username: ENV['USERNAME'],
        password: ENV['PASSWORD'],
        scope: 'read_station read_thermostat'
      }
    end
  end
  store_token(response)
end