Class: MyTado::OAuthClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/my_tado/oauth_client.rb

Overview

Gets the authorization credentials we need to interact with the main API

Defined Under Namespace

Classes: NoCredentialsError

Instance Method Summary collapse

Constructor Details

#initialize(client_secret:, username:, password:) ⇒ OAuthClient

Returns a new instance of OAuthClient.



12
13
14
15
16
17
# File 'lib/my_tado/oauth_client.rb', line 12

def initialize(client_secret:, username:, password:)
  @default_options = { client_id: "tado-web-app", scope: "home.user", client_secret: client_secret }
  @refresh_token = nil
  @username = username
  @password = password
end

Instance Method Details

#access_token(preferred_method: "refresh") ⇒ Object

Doesn’t cache it or do anything clever yet



20
21
22
23
24
25
26
27
28
# File 'lib/my_tado/oauth_client.rb', line 20

def access_token(preferred_method: "refresh")
  if preferred_method == "refresh" && @refresh_token
    using_refresh_token["access_token"]
  elsif @username && @password
    using_username_and_password["access_token"]
  else
    raise NoCredentialsError
  end
end

#using_refresh_tokenObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/my_tado/oauth_client.rb', line 30

def using_refresh_token
  response = self.class.post(
    "/token",
    options({
      grant_type: "refresh_token",
      refresh_token: @refresh_token,
    }),
  )

  @refresh_token = response["refresh_token"]
  response
end

#using_username_and_passwordObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/my_tado/oauth_client.rb', line 43

def using_username_and_password
  response = self.class.post(
    "/token",
    options({
      grant_type: "password",
      username: @username,
      password: @password,
    }),
  )

  @refresh_token = response["refresh_token"]
  response
end