Class: Ecobee::Token
- Inherits:
-
Object
- Object
- Ecobee::Token
- Defined in:
- lib/ecobee/token.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#access_token_expire ⇒ Object
readonly
Returns the value of attribute access_token_expire.
-
#app_key ⇒ Object
readonly
Returns the value of attribute app_key.
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#pin ⇒ Object
readonly
Returns the value of attribute pin.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#token_file ⇒ Object
readonly
Returns the value of attribute token_file.
-
#token_type ⇒ Object
readonly
Returns the value of attribute token_type.
Instance Method Summary collapse
- #access_token_expired? ⇒ Boolean
- #authorization ⇒ Object
- #config_load ⇒ Object
- #config_save ⇒ Object
-
#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
constructor
A new instance of Token.
- #pin_is_valid ⇒ Object
- #pin_message ⇒ Object
- #refresh_access_token ⇒ Object
- #register_callback(type, *callback, &block) ⇒ Object
- #wait(timeout: nil) ⇒ Object
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 = 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_token ⇒ Object (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_expire ⇒ Object (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_key ⇒ Object (readonly)
Returns the value of attribute app_key.
4 5 6 |
# File 'lib/ecobee/token.rb', line 4 def app_key @app_key end |
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
4 5 6 |
# File 'lib/ecobee/token.rb', line 4 def callbacks @callbacks end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
4 5 6 |
# File 'lib/ecobee/token.rb', line 4 def http @http end |
#pin ⇒ Object (readonly)
Returns the value of attribute pin.
4 5 6 |
# File 'lib/ecobee/token.rb', line 4 def pin @pin end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
4 5 6 |
# File 'lib/ecobee/token.rb', line 4 def refresh_token @refresh_token end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
4 5 6 |
# File 'lib/ecobee/token.rb', line 4 def result @result end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
4 5 6 |
# File 'lib/ecobee/token.rb', line 4 def scope @scope end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
4 5 6 |
# File 'lib/ecobee/token.rb', line 4 def status @status end |
#token_file ⇒ Object (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_type ⇒ Object (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
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 |
#authorization ⇒ Object
88 89 90 |
# File 'lib/ecobee/token.rb', line 88 def "#{@token_type} #{access_token}" end |
#config_load ⇒ Object
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_save ⇒ Object
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_valid ⇒ Object
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_message ⇒ Object
114 115 116 117 |
# File 'lib/ecobee/token.rb', line 114 def "Log into Ecobee web portal, select My Apps widget, Add Application, " + "enter the PIN #{@pin || ''}" end |
#refresh_access_token ⇒ Object
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 |