Skip to content

Commit

Permalink
Single quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
timdorr committed Nov 11, 2018
1 parent 7cceec4 commit bfa6ec1
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 228 deletions.
28 changes: 14 additions & 14 deletions lib/tesla_api/client.rb
@@ -1,21 +1,21 @@
module TeslaApi
class Client
include HTTParty
base_uri "https://owner-api.teslamotors.com/api/1"
headers "User-Agent" => "github.com/timdorr/tesla-api v:#{VERSION}"
base_uri 'https://owner-api.teslamotors.com/api/1'
headers 'User-Agent' => "github.com/timdorr/tesla-api v:#{VERSION}"
format :json

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

def initialize(email, client_id = ENV["TESLA_CLIENT_ID"], client_secret = ENV["TESLA_CLIENT_SECRET"])
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

def token=(token)
@token = token
self.class.headers "Authorization" => "Bearer #{token}"
self.class.headers 'Authorization' => "Bearer #{token}"
end

def expires_in=(seconds)
Expand All @@ -38,23 +38,23 @@ def expired?

def login!(password)
response = self.class.post(
"https://owner-api.teslamotors.com/oauth/token",
'https://owner-api.teslamotors.com/oauth/token',
body: {
"grant_type" => "password",
"client_id" => client_id,
"client_secret" => client_secret,
"email" => email,
"password" => password
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"]
self.expires_in = response['expires_in']
self.created_at = response['created_at']
self.token = response['access_token']
end

def vehicles
self.class.get("/vehicles")["response"].map { |v| Vehicle.new(self.class, v["id"], v) }
self.class.get('/vehicles')['response'].map { |v| Vehicle.new(self.class, v['id'], v) }
end
end
end
10 changes: 5 additions & 5 deletions lib/tesla_api/stream.rb
Expand Up @@ -6,7 +6,7 @@ def stream(&reciever)
attributes = chunk.split(",")

reciever.call({
time: DateTime.strptime((attributes[0].to_i/1000).to_s, "%s"),
time: DateTime.strptime((attributes[0].to_i/1000).to_s, '%s'),
speed: attributes[1].to_f,
odometer: attributes[2].to_f,
soc: attributes[3].to_f,
Expand All @@ -31,23 +31,23 @@ def stream(&reciever)

def request
@request ||= EventMachine::HttpRequest.new(
"#{stream_endpoint}/stream/#{self["vehicle_id"]}/?values=#{stream_params}")
"#{stream_endpoint}/stream/#{self['vehicle_id']}/?values=#{stream_params}")
end

def http
request.get(
head: {
"authorization" => [email, self["tokens"].first]
'authorization' => [email, self['tokens'].first]
},
inactivity_timeout: 15)
end

def stream_endpoint
"https://streaming.vn.teslamotors.com"
'https://streaming.vn.teslamotors.com'
end

def stream_params
"speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state,range,est_range,heading"
'speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state,range,est_range,heading'
end
end
end
64 changes: 32 additions & 32 deletions lib/tesla_api/vehicle.rb
Expand Up @@ -27,133 +27,133 @@ def method_missing(name)
# State

def data
api.get("/vehicles/#{id}/data")["response"]
api.get("/vehicles/#{id}/data")['response']
end

def mobile_enabled
api.get("/vehicles/#{id}/mobile_enabled")["response"]
api.get("/vehicles/#{id}/mobile_enabled")['response']
end

def gui_settings
data_request("gui_settings")["response"]
data_request('gui_settings')['response']
end

def charge_state
data_request("charge_state")["response"]
data_request('charge_state')['response']
end

def climate_state
data_request("climate_state")["response"]
data_request('climate_state')['response']
end

def drive_state
data_request("drive_state")["response"]
data_request('drive_state')['response']
end

def vehicle_state
data_request("vehicle_state")["response"]
data_request('vehicle_state')['response']
end

# Commands

def wake_up
@vehicle = api.post("/vehicles/#{id}/wake_up")["response"]
@vehicle = api.post("/vehicles/#{id}/wake_up")['response']
end

def set_valet_mode(on, password=nil)
command("set_valet_mode", body: {on: on, password: password})["response"]
command('set_valet_mode', body: {on: on, password: password})['response']
end

def reset_valet_pin
command("reset_valet_pin")["response"]
command('reset_valet_pin')['response']
end

def charge_port_door_open
command("charge_port_door_open")["response"]
command('charge_port_door_open')['response']
end

def charge_standard
command("charge_standard")["response"]
command('charge_standard')['response']
end

def charge_max_range
command("charge_max_range")["response"]
command('charge_max_range')['response']
end

def set_charge_limit(percent)
command("set_charge_limit", body: {percent: percent})["response"]
command('set_charge_limit', body: {percent: percent})['response']
end

def charge_start
command("charge_start")["response"]
command('charge_start')['response']
end

def charge_stop
command("charge_stop")["response"]
command('charge_stop')['response']
end

def flash_lights
command("flash_lights")["response"]
command('flash_lights')['response']
end

def honk_horn
command("honk_horn")["response"]
command('honk_horn')['response']
end

def door_unlock
command("door_unlock")["response"]
command('door_unlock')['response']
end

def door_lock
command("door_lock")["response"]
command('door_lock')['response']
end

def set_temps(driver_temp, passenger_temp)
command("set_temps", body: {driver_temp: driver_temp, passenger_temp: passenger_temp})["response"]
command('set_temps', body: {driver_temp: driver_temp, passenger_temp: passenger_temp})['response']
end

def auto_conditioning_start
command("auto_conditioning_start")["response"]
command('auto_conditioning_start')['response']
end

def auto_conditioning_stop
command("auto_conditioning_stop")["response"]
command('auto_conditioning_stop')['response']
end

def sun_roof_control(state)
command("sun_roof_control", body: {state: state})["response"]
command('sun_roof_control', body: {state: state})['response']
end

def sun_roof_move(percent)
command("sun_roof_control", body: {state: "move", percent: percent})["response"]
command('sun_roof_control', body: {state: 'move', percent: percent})['response']
end

def remote_start_drive(password)
command("remote_start_drive", body: {password: password})["response"]
command('remote_start_drive', body: {password: password})['response']
end

def open_trunk
command("actuate_trunk", body: {which_trunk: "rear"})["response"]
command('actuate_trunk', body: {which_trunk: 'rear'})['response']
end

def open_frunk
command("actuate_trunk", body: {which_trunk: "front"})["response"]
command('actuate_trunk', body: {which_trunk: 'front'})['response']
end

def activate_speed_limit(pin)
command("speed_limit_activate", body: {pin: pin})["response"]
command('speed_limit_activate', body: {pin: pin})['response']
end

def deactivate_speed_limit(pin)
command("speed_limit_deactivate", body: {pin: pin})["response"]
command('speed_limit_deactivate', body: {pin: pin})['response']
end

def set_speed_limit(limit_mph)
command("speed_limit_set_limit", body: {limit_mph: limit_mph})["response"]
command('speed_limit_set_limit', body: {limit_mph: limit_mph})['response']
end

def clear_speed_limit_pin(pin)
command("speed_limit_clear_pin", body: {pin: pin})["response"]
command('speed_limit_clear_pin', body: {pin: pin})['response']
end

private
Expand Down
46 changes: 23 additions & 23 deletions spec/lib/tesla_api/client_spec.rb
@@ -1,63 +1,63 @@
require 'spec_helper'

RSpec.describe TeslaApi::Client do
subject(:tesla_api) { TeslaApi::Client.new(ENV["TESLA_EMAIL"]) }
subject(:tesla_api) { TeslaApi::Client.new(ENV['TESLA_EMAIL']) }

describe "#new client" do
it "has no expiry date" do
describe '#new client' do
it 'has no expiry date' do
expect(tesla_api.expired_at).to eq(nil)
end

it "has a expiry status set to true" do
it 'has a expiry status set to true' do
expect(tesla_api.expired?).to eq(true)
end
end

describe "#token=" do
it "sets a Bearer token" do
describe '#token=' do
it 'sets a Bearer token' do
tesla_api.token = Faker::Lorem.characters(32)
expect(tesla_api.class.headers).to include({"Authorization" => /Bearer [a-z0-9]{32}/})
expect(tesla_api.class.headers).to include({'Authorization' => /Bearer [a-z0-9]{32}/})
end
end

describe "#login!", vcr: { cassette_name: "client-login" } do
describe '#login!', vcr: { cassette_name: 'client-login' } do
it { is_expected.to be_a(TeslaApi::Client) }

it "logs into the API" do
tesla_api.login!(ENV["TESLA_PASS"])
it 'logs into the API' do
tesla_api.login!(ENV['TESLA_PASS'])
expect(a_request(:post, "https://#{URI.parse(tesla_api.class.base_uri).host}/oauth/token")).to have_been_made.once
end

it "set a expiry date" do
tesla_api.login!(ENV["TESLA_PASS"])
it 'set a expiry date' do
tesla_api.login!(ENV['TESLA_PASS'])
expect(tesla_api.expired_at).to eq(Time.at(1475777133 + 7776000).to_datetime)
end

it "expose expiry status" do
tesla_api.login!(ENV["TESLA_PASS"])
it 'expose expiry status' do
tesla_api.login!(ENV['TESLA_PASS'])
tesla_api.created_at = (Time.now - 1).to_i
expect(tesla_api.expired?).to eq(false)
end

it "is expired when has a 90+ days old date" do
tesla_api.login!(ENV["TESLA_PASS"])
it 'is expired when has a 90+ days old date' do
tesla_api.login!(ENV['TESLA_PASS'])
tesla_api.created_at = (Time.now - 7776000 - 1).to_i
expect(tesla_api.expired?).to eq(true)
end

it "sets a Bearer token header" do
tesla_api.login!(ENV["TESLA_PASS"])
expect(tesla_api.class.headers).to include({"Authorization" => /Bearer [a-z0-9]{32}/})
it 'sets a Bearer token header' do
tesla_api.login!(ENV['TESLA_PASS'])
expect(tesla_api.class.headers).to include({'Authorization' => /Bearer [a-z0-9]{32}/})
end

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

describe "#vehicles", vcr: {cassette_name: "client-vehicles"} do
it "lists the vehicles on the account" do
describe '#vehicles', vcr: {cassette_name: 'client-vehicles'} do
it 'lists the vehicles on the account' do
expect(tesla_api.vehicles).to include(TeslaApi::Vehicle)
end
end
Expand Down

0 comments on commit bfa6ec1

Please sign in to comment.