Skip to content

Commit

Permalink
Retrieve refresh_token from the OAuth login (#136)
Browse files Browse the repository at this point in the history
* Retrieve refresh_token from the OAuth login

* Please don't bump the gem version in a PR


Co-authored-by: Tim Dorr <timdorr@users.noreply.github.com>
  • Loading branch information
beni and timdorr committed Apr 30, 2019
1 parent 186b611 commit 8737125
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
13 changes: 9 additions & 4 deletions lib/tesla_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Client
headers 'User-Agent' => "github.com/timdorr/tesla-api v:#{VERSION}"
format :json

attr_reader :email, :token, :client_id, :client_secret
attr_reader :email, :token, :refresh_token, :client_id, :client_secret

def initialize(email, client_id = ENV['TESLA_CLIENT_ID'], client_secret = ENV['TESLA_CLIENT_SECRET'])
@email = email
Expand Down Expand Up @@ -48,13 +48,18 @@ def login!(password)
}
)

self.expires_in = response['expires_in']
self.created_at = response['created_at']
self.token = response['access_token']
self.expires_in = response['expires_in']
self.created_at = response['created_at']
self.token = response['access_token']
self.refresh_token = response['refresh_token']
end

def vehicles
self.class.get('/vehicles')['response'].map { |v| Vehicle.new(self.class, email, v['id'], v) }
end

private

attr_writer :refresh_token
end
end
4 changes: 2 additions & 2 deletions spec/cassettes/client-login.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions spec/lib/tesla_api/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
tesla_api.login!(ENV['TESLA_PASS'])
expect(tesla_api.token).to match(/[a-z0-9]{32}/)
end

it 'obtains a refresh token' do
tesla_api.login!(ENV['TESLA_PASS'])
expect(tesla_api.refresh_token).to match(/[a-z0-9]{32}/)
end
end

describe '#vehicles', vcr: {cassette_name: 'client-vehicles'} do
Expand Down

0 comments on commit 8737125

Please sign in to comment.