class Netatmo::Client

Constants

BASE_URL
Config

Attributes

access_token[RW]
expires_at[RW]
refresh_token[RW]

Public Class Methods

new(&config_block) click to toggle source
# File lib/netatmo/client.rb, line 13
def initialize(&config_block)
  if block_given?
    config_block.call config
  else
    config
  end

  authenticate
end

Public Instance Methods

authenticate() click to toggle source
# File lib/netatmo/client.rb, line 38
def authenticate
  if @access_token && @expires_at > Time.now
    true
  else
    fetch_token
  end
end
config() click to toggle source
# 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) click to toggle source
# File lib/netatmo/client.rb, line 23
def configure(&config_block)
  config_block.call config if block_given?
  config
end
fetch_token() click to toggle source
# 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_data() click to toggle source

rubocop:disable Naming/AccessorMethodName

# 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

Private Instance Methods

connection() click to toggle source

rubocop:enable Naming/AccessorMethodName

# File lib/netatmo/client.rb, line 85
def connection
  @connection ||= Faraday::Connection.new(url: config.base_url) do |builder|
    builder.request :url_encoded
    builder.adapter :net_http
  end
end
store_token(response) click to toggle source
# File lib/netatmo/client.rb, line 92
def store_token(response)
  return unless response.status == 200

  data = JSON.parse(response.body)
  @access_token = data['access_token']
  @refresh_token = data['refresh_token']
  @expires_at = Time.now + data['expires_in']
  puts "successfully stored new access_token: #{@access_token} - expires at #{@expires_at}"
end