Class: TeslaApi::Client
- Inherits:
-
Object
- Object
- TeslaApi::Client
- Includes:
- HTTParty
- Defined in:
- lib/tesla_api/client.rb
Instance Attribute Summary collapse
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #created_at=(timestamp) ⇒ Object
- #expired? ⇒ Boolean
- #expired_at ⇒ Object
- #expires_in=(seconds) ⇒ Object
-
#initialize(email, client_id = ENV['TESLA_CLIENT_ID'], client_secret = ENV['TESLA_CLIENT_SECRET']) ⇒ Client
constructor
A new instance of Client.
- #login!(password) ⇒ Object
- #vehicles ⇒ Object
Constructor Details
#initialize(email, client_id = ENV['TESLA_CLIENT_ID'], client_secret = ENV['TESLA_CLIENT_SECRET']) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 |
# File 'lib/tesla_api/client.rb', line 10 def initialize(email, client_id = ENV['TESLA_CLIENT_ID'], client_secret = ENV['TESLA_CLIENT_SECRET']) @email = email @client_id = client_id @client_secret = client_secret end |
Instance Attribute Details
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
8 9 10 |
# File 'lib/tesla_api/client.rb', line 8 def client_id @client_id end |
#client_secret ⇒ Object (readonly)
Returns the value of attribute client_secret.
8 9 10 |
# File 'lib/tesla_api/client.rb', line 8 def client_secret @client_secret end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
8 9 10 |
# File 'lib/tesla_api/client.rb', line 8 def email @email end |
#token ⇒ Object
Returns the value of attribute token.
8 9 10 |
# File 'lib/tesla_api/client.rb', line 8 def token @token end |
Instance Method Details
#created_at=(timestamp) ⇒ Object
25 26 27 |
# File 'lib/tesla_api/client.rb', line 25 def created_at=() @created_at = Time.at(.to_f).to_datetime end |
#expired? ⇒ Boolean
34 35 36 37 |
# File 'lib/tesla_api/client.rb', line 34 def expired? return true unless defined?(@created_at) expired_at <= DateTime.now end |
#expired_at ⇒ Object
29 30 31 32 |
# File 'lib/tesla_api/client.rb', line 29 def expired_at return nil unless defined?(@created_at) (@created_at.to_time + @expires_in.to_f).to_datetime end |
#expires_in=(seconds) ⇒ Object
21 22 23 |
# File 'lib/tesla_api/client.rb', line 21 def expires_in=(seconds) @expires_in = seconds.to_f end |
#login!(password) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tesla_api/client.rb', line 39 def login!(password) response = self.class.post( 'https://owner-api.teslamotors.com/oauth/token', body: { grant_type: 'password', client_id: client_id, client_secret: client_secret, email: email, password: password } ) self.expires_in = response['expires_in'] self.created_at = response['created_at'] self.token = response['access_token'] end |