Skip to content

Commit c3ebbf6

Browse files
authored
Merge pull request timdorr#40 from josephpage/add_token_expiry_information
Add token expiry status
2 parents 669f2b8 + cf34eab commit c3ebbf6

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

lib/tesla_api/client.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ def token=(token)
1717
self.class.headers "Authorization" => "Bearer #{token}"
1818
end
1919

20+
def expires_in=(seconds)
21+
@expires_in = seconds.to_f
22+
end
23+
24+
def created_at=(timestamp)
25+
@created_at = Time.at(timestamp.to_f).to_datetime
26+
end
27+
28+
def expired_at
29+
return nil unless defined?(@created_at)
30+
(@created_at.to_time + @expires_in.to_f).to_datetime
31+
end
32+
33+
def expired?
34+
return true unless defined?(@created_at)
35+
expired_at <= DateTime.now
36+
end
37+
2038
def login!(password)
2139
response = self.class.post(
2240
"https://owner-api.teslamotors.com/oauth/token",
@@ -28,8 +46,10 @@ def login!(password)
2846
"password" => password
2947
}
3048
)
31-
32-
self.token = response["access_token"]
49+
50+
self.expires_in = response["expires_in"]
51+
self.created_at = response["created_at"]
52+
self.token = response["access_token"]
3353
end
3454

3555
def vehicles

spec/cassettes/client-login.yml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/lib/tesla_api/client_spec.rb

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
RSpec.describe TeslaApi::Client do
44
subject(:tesla_api) { TeslaApi::Client.new(ENV["TESLA_EMAIL"]) }
55

6+
describe "#new client" do
7+
it "has no expiry date" do
8+
expect(tesla_api.expired_at).to eq(nil)
9+
end
10+
11+
it "has a expiry status set to true" do
12+
expect(tesla_api.expired?).to eq(true)
13+
end
14+
end
15+
616
describe "#token=" do
717
it "sets a Bearer token" do
818
tesla_api.token = Faker::Lorem.characters(32)
@@ -18,15 +28,32 @@
1828
expect(a_request(:post, "https://#{URI.parse(tesla_api.class.base_uri).host}/oauth/token")).to have_been_made.once
1929
end
2030

21-
it "obtains a Bearer token" do
31+
it "set a expiry date" do
2232
tesla_api.login!(ENV["TESLA_PASS"])
23-
expect(tesla_api.token).to match(/[a-z0-9]{32}/)
33+
expect(tesla_api.expired_at).to eq(Time.at(1475777133 + 7776000).to_datetime)
34+
end
35+
36+
it "expose expiry status" do
37+
tesla_api.login!(ENV["TESLA_PASS"])
38+
tesla_api.created_at = (Time.now - 1).to_i
39+
expect(tesla_api.expired?).to eq(false)
40+
end
41+
42+
it "is expired when has a 90+ days old date" do
43+
tesla_api.login!(ENV["TESLA_PASS"])
44+
tesla_api.created_at = (Time.now - 7776000 - 1).to_i
45+
expect(tesla_api.expired?).to eq(true)
2446
end
2547

2648
it "sets a Bearer token header" do
2749
tesla_api.login!(ENV["TESLA_PASS"])
2850
expect(tesla_api.class.headers).to include({"Authorization" => /Bearer [a-z0-9]{32}/})
2951
end
52+
53+
it "obtains a Bearer token" do
54+
tesla_api.login!(ENV["TESLA_PASS"])
55+
expect(tesla_api.token).to match(/[a-z0-9]{32}/)
56+
end
3057
end
3158

3259
describe "#vehicles", vcr: {cassette_name: "client-vehicles"} do

0 commit comments

Comments
 (0)