Class: Ecobee::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/ecobee/token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token: nil, access_token_expire: nil, app_key: nil, callbacks: {}, log_file: nil, refresh_token: nil, scope: SCOPES[0], token_file: DEFAULT_FILES) ⇒ Token

Returns a new instance of Token.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ecobee/token.rb', line 24

def initialize(
  access_token: nil,
  access_token_expire: nil,
  app_key: nil,
  callbacks: {},
  log_file: nil,
  refresh_token: nil,
  scope: SCOPES[0],
  token_file: DEFAULT_FILES
)
  @access_token = access_token
  @access_token_expire = access_token_expire
  @app_key = app_key
  @callbacks = callbacks
  @refresh_token = refresh_token
  @scope = scope
  @token_file = expand_files token_file

  @authorization_thread, @pin, @status, @token_type = nil
  @poll_interval = DEFAULT_POLL_INTERVAL

  @http = Ecobee::HTTP.new(log_file: log_file, token: self)

  @refresh_pad = REFRESH_PAD + rand(REFRESH_PAD)

  config_load
  access_token()
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def access_token
  @access_token
end

#access_token_expireObject (readonly)

Returns the value of attribute access_token_expire.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def access_token_expire
  @access_token_expire
end

#app_keyObject (readonly)

Returns the value of attribute app_key.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def app_key
  @app_key
end

#callbacksObject (readonly)

Returns the value of attribute callbacks.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def callbacks
  @callbacks
end

#httpObject (readonly)

Returns the value of attribute http.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def http
  @http
end

#pinObject (readonly)

Returns the value of attribute pin.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def pin
  @pin
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def refresh_token
  @refresh_token
end

#resultObject (readonly)

Returns the value of attribute result.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def result
  @result
end

#scopeObject (readonly)

Returns the value of attribute scope.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def scope
  @scope
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def status
  @status
end

#token_fileObject (readonly)

Returns the value of attribute token_file.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def token_file
  @token_file
end

#token_typeObject (readonly)

Returns the value of attribute token_type.



4
5
6
# File 'lib/ecobee/token.rb', line 4

def token_type
  @token_type
end

Instance Method Details

#access_token_expired?Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/ecobee/token.rb', line 83

def access_token_expired?
  return true unless @access_token_expire
  Time.now.to_i > @access_token_expire - @refresh_pad
end

#authorizationObject



88
89
90
# File 'lib/ecobee/token.rb', line 88

def authorization
  "#{@token_type} #{access_token}"
end

#config_loadObject



92
93
94
95
96
97
98
# File 'lib/ecobee/token.rb', line 92

def config_load
  config = config_read_our_section
  if @callbacks[:load].respond_to? :call
    config = @callbacks[:load].call(config)
  end
  config_load_to_memory config
end

#config_saveObject



100
101
102
103
104
105
106
# File 'lib/ecobee/token.rb', line 100

def config_save
  config = config_dump()
  if @callbacks[:save].respond_to? :call
    config = @callbacks[:save].call(config)
  end
  config_write_section config
end

#pin_is_validObject



108
109
110
111
112
# File 'lib/ecobee/token.rb', line 108

def pin_is_valid
  if @pin && @access_token && @access_token_expire
    @access_token_expire.to_i >= Time.now.to_i
  end
end

#pin_messageObject



114
115
116
117
# File 'lib/ecobee/token.rb', line 114

def pin_message
  "Log into Ecobee web portal, select My Apps widget, Add Application, " +
  "enter the PIN #{@pin || ''}"
end

#refresh_access_tokenObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ecobee/token.rb', line 119

def refresh_access_token
  arg = sprintf("?grant_type=refresh_token&refresh_token=%s&client_id=%s",
                @refresh_token,
                @app_key)
  result = @http.post(arg: arg,
                      no_auth: true,
                      resource_prefix: 'token',
                      validate_status: false)
  if result.key? 'error'
    @access_token, @access_token_expire, @pin, @scope, @refresh_token = nil
    config_save
    raise Ecobee::AuthError.new(
      "Result Error: (%s) %s" % [result['error'],
                                 result['error_description']]
    )
  else
    @access_token = result['access_token']
    @access_token_expire = Time.now.to_i + result['expires_in']
    @pin = nil
    @refresh_token = result['refresh_token']
    @scope = result['scope']
    @token_type = result['token_type']
    @status = :ready
    config_save
    @access_token
  end 
end

#register_callback(type, *callback, &block) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/ecobee/token.rb', line 147

def register_callback(type, *callback, &block)
  if block_given?
    puts "Registering #{type}"
    @callbacks[type] = block
  else
    @callbacks[type] = callback[0] if callback.length > 0
  end
end

#wait(timeout: nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/ecobee/token.rb', line 156

def wait(timeout: nil)
  if timeout
    Timeout::timeout(timeout) { wait(timeout: nil) }
  else
    sleep 0.01 while @status == :authorization_pending
  end
rescue Timeout::Error
ensure
  @status
end