Skip to content

Commit b96492c

Browse files
committed
Merge pull request #11 from timdorr/explicit-login
Explicit Login
2 parents 78a56e4 + 5cbddbf commit b96492c

File tree

8 files changed

+36
-55
lines changed

8 files changed

+36
-55
lines changed

lib/tesla_api/client.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ class Client
44
base_uri "https://owner-api.teslamotors.com/api/1"
55
format :json
66

7-
attr_reader :email
7+
attr_reader :email, :token, :client_id, :client_secret
88

9-
def initialize(email, password, client_id, client_secret)
9+
def initialize(email, client_id = ENV["TESLA_CLIENT_ID"], client_secret = ENV["TESLA_CLIENT_SECRET"])
1010
@email = email
11+
@client_id = client_id
12+
@client_secret = client_secret
13+
end
14+
15+
def token=(token)
16+
@token = token
17+
self.class.headers "Authorization" => "Bearer #{token}"
18+
end
19+
20+
def login!(password)
1121
response = self.class.post(
1222
"https://owner-api.teslamotors.com/oauth/token",
1323
body: {
@@ -18,7 +28,8 @@ def initialize(email, password, client_id, client_secret)
1828
"password" => password
1929
}
2030
)
21-
self.class.headers "Authorization" => "Bearer #{response["access_token"]}"
31+
32+
self.token = response["access_token"]
2233
end
2334

2435
def vehicles

lib/tesla_api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module TeslaApi
2-
VERSION = "1.0.0"
2+
VERSION = "1.1.0"
33
end

spec/cassettes/client-vehicles.yml

Lines changed: 0 additions & 45 deletions
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: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
require 'spec_helper'
22

33
RSpec.describe TeslaApi::Client do
4-
subject(:tesla_api) { TeslaApi::Client.new(ENV["TESLA_EMAIL"], ENV["TESLA_PASS"], ENV["TESLA_CLIENT_ID"], ENV["TESLA_CLIENT_SECRET"]) }
4+
subject(:tesla_api) { TeslaApi::Client.new(ENV["TESLA_EMAIL"]) }
55

6-
describe "#initialize", vcr: { cassette_name: "client-initialize" } do
6+
describe "#token=" do
7+
it "sets a Bearer token" do
8+
tesla_api.token = Faker::Lorem.characters(32)
9+
expect(tesla_api.class.headers).to include({"Authorization" => /Bearer [a-z0-9]{32}/})
10+
end
11+
end
12+
13+
describe "#login!", vcr: { cassette_name: "client-login" } do
714
it { is_expected.to be_a(TeslaApi::Client) }
815

916
it "logs into the API" do
10-
base_uri = URI.parse(tesla_api.class.base_uri)
11-
expect(a_request(:post, "https://#{base_uri.host}/oauth/token")).to have_been_made.once
17+
tesla_api.login!(ENV["TESLA_PASS"])
18+
expect(a_request(:post, "https://#{URI.parse(tesla_api.class.base_uri).host}/oauth/token")).to have_been_made.once
1219
end
1320

14-
it "sets a Bearer token" do
21+
it "obtains a Bearer token" do
22+
tesla_api.login!(ENV["TESLA_PASS"])
23+
expect(tesla_api.token).to match(/[a-z0-9]{32}/)
24+
end
25+
26+
it "sets a Bearer token header" do
27+
tesla_api.login!(ENV["TESLA_PASS"])
1528
expect(tesla_api.class.headers).to include({"Authorization" => /Bearer [a-z0-9]{32}/})
1629
end
1730
end

spec/lib/tesla_api/vehicle_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22

33
RSpec.describe TeslaApi::Vehicle do
4-
let(:tesla_api) { TeslaApi::Client.new(ENV["TESLA_EMAIL"], ENV["TESLA_PASS"], ENV["TESLA_CLIENT_ID"], ENV["TESLA_CLIENT_SECRET"]) }
4+
let(:tesla_api) { TeslaApi::Client.new(ENV["TESLA_EMAIL"]) }
55

66
subject(:vehicle) { tesla_api.vehicles.first }
77

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Coveralls::Output.silent = true
1212

1313
require 'dotenv'
14+
require 'faker'
1415
require 'vcr'
1516
require 'webmock/rspec'
1617

tesla_api.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
2323
spec.add_development_dependency "bundler", "~> 1.7"
2424
spec.add_development_dependency "rake", "~> 10.0"
2525
spec.add_development_dependency "rspec", "~> 3.1"
26+
spec.add_development_dependency "faker", "~> 1.4"
2627
spec.add_development_dependency "vcr", "~> 2.9"
2728
spec.add_development_dependency "webmock", "~> 1.20"
2829
spec.add_development_dependency "dotenv", "~> 1.0"

0 commit comments

Comments
 (0)