Class: Netatmo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/netatmo/client.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

BASE_URL =
'https://api.netatmo.net'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&config_block) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
# File 'lib/netatmo/client.rb', line 13

def initialize(&config_block)
  if block_given?
    config_block.call config
  else
    config
  end

  authenticate
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



9
10
11
# File 'lib/netatmo/client.rb', line 9

def access_token
  @access_token
end

#expires_atObject

Returns the value of attribute expires_at.



9
10
11
# File 'lib/netatmo/client.rb', line 9

def expires_at
  @expires_at
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



9
10
11
# File 'lib/netatmo/client.rb', line 9

def refresh_token
  @refresh_token
end

Instance Method Details

#authenticateObject



38
39
40
41
42
43
44
# File 'lib/netatmo/client.rb', line 38

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

#configObject



28
29
30
31
32
33
34
35
36
# File 'lib/netatmo/client.rb', line 28

def config
  @config ||= Config.new(
    ENV['NETATMO_CLIENT_ID'],
    ENV['NETATMO_CLIENT_SECRET'],
    ENV['NETATMO_USERNAME'],
    ENV['NETATMO_PASSWORD'],
    BASE_URL
  )
end

#configure(&config_block) ⇒ Object



23
24
25
26
# File 'lib/netatmo/client.rb', line 23

def configure(&config_block)
  config_block.call config if block_given?
  config
end

#fetch_tokenObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/netatmo/client.rb', line 46

def fetch_token
  response = if @refresh_token
               connection.post '/oauth2/token' do |request|
                 request.body = {
                   grant_type: :refresh_token,
                   refresh_token: @refresh_token,
                   client_id: config.client_id,
                   client_secret: config.client_secret
                 }
               end
             else
               connection.post '/oauth2/token' do |request|
                 request.body = {
                   grant_type: :password,
                   client_id: config.client_id,
                   client_secret: config.client_secret,
                   username: config.username,
                   password: config.password,
                   scope: 'read_station'
                 }
               end
             end
  store_token(response)
end

#get_station_dataObject

rubocop:disable Naming/AccessorMethodName



72
73
74
75
76
77
78
79
80
# File 'lib/netatmo/client.rb', line 72

def get_station_data
  raise 'Unauthenticated' unless authenticate

  response = connection.get('/api/getstationsdata', access_token: @access_token)

  raise 'Got unsuccessful response' unless response.status == 200

  Netatmo::Weather::StationData.new(JSON.parse(response.body)['body'])
end