Class: TeslaApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tesla_api/client.rb

Constant Summary collapse

BASE_URI =
'https://owner-api.teslamotors.com/api/1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email: nil, access_token: nil, access_token_expires_at: nil, refresh_token: nil, client_id: ENV['TESLA_CLIENT_ID'], client_secret: ENV['TESLA_CLIENT_SECRET']) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tesla_api/client.rb', line 7

def initialize(
    email: nil,
    access_token: nil,
    access_token_expires_at: nil,
    refresh_token: nil,
    client_id: ENV['TESLA_CLIENT_ID'],
    client_secret: ENV['TESLA_CLIENT_SECRET']
)
  @email = email

  @client_id = client_id
  @client_secret = client_secret

  @access_token = access_token
  @access_token_expires_at = access_token_expires_at
  @refresh_token = refresh_token

  @api = Faraday.new(
    BASE_URI,
    headers: { 'User-Agent' => "github.com/timdorr/tesla-api v:#{VERSION}" }
  ) do |conn|
    conn.request :json
    conn.response :json
    conn.response :raise_error
    conn.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



3
4
5
# File 'lib/tesla_api/client.rb', line 3

def access_token
  @access_token
end

#access_token_expires_atObject (readonly)

Returns the value of attribute access_token_expires_at.



3
4
5
# File 'lib/tesla_api/client.rb', line 3

def access_token_expires_at
  @access_token_expires_at
end

#apiObject (readonly)

Returns the value of attribute api.



3
4
5
# File 'lib/tesla_api/client.rb', line 3

def api
  @api
end

#client_idObject (readonly)

Returns the value of attribute client_id.



3
4
5
# File 'lib/tesla_api/client.rb', line 3

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



3
4
5
# File 'lib/tesla_api/client.rb', line 3

def client_secret
  @client_secret
end

#emailObject (readonly)

Returns the value of attribute email.



3
4
5
# File 'lib/tesla_api/client.rb', line 3

def email
  @email
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



3
4
5
# File 'lib/tesla_api/client.rb', line 3

def refresh_token
  @refresh_token
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/tesla_api/client.rb', line 72

def expired?
  return true if access_token_expires_at.nil?
  access_token_expires_at <= DateTime.now
end

#get(url) ⇒ Object



77
78
79
# File 'lib/tesla_api/client.rb', line 77

def get(url)
  api.get(url.sub(/^\//, ''), nil, { 'Authorization' => "Bearer #{access_token}" }).body
end

#login!(password) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tesla_api/client.rb', line 53

def login!(password)
  response = api.post(
    'https://owner-api.teslamotors.com/oauth/token',
    {
      grant_type: 'password',
      client_id: client_id,
      client_secret: client_secret,
      email: email,
      password: password
    }
  ).body

  @access_token = response['access_token']
  @access_token_expires_at = Time.at(response['created_at'].to_f + response['expires_in'].to_f).to_datetime
  @refresh_token = response['refresh_token']

  response
end

#post(url, body: nil) ⇒ Object



81
82
83
# File 'lib/tesla_api/client.rb', line 81

def post(url, body: nil)
  api.post(url.sub(/^\//, ''), body, { 'Authorization' => "Bearer #{access_token}" }).body
end

#refresh_access_tokenObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tesla_api/client.rb', line 35

def refresh_access_token
  response = api.post(
    'https://owner-api.teslamotors.com/oauth/token',
    {
      grant_type: 'refresh_token',
      client_id: client_id,
      client_secret: client_secret,
      refresh_token: refresh_token
    }
  ).body

  @access_token = response['access_token']
  @access_token_expires_at = Time.at(response['created_at'].to_f + response['expires_in'].to_f).to_datetime
  @refresh_token = response['refresh_token']

  response
end

#vehicle(id) ⇒ Object



89
90
91
# File 'lib/tesla_api/client.rb', line 89

def vehicle(id)
  Vehicle.new(self, email, id, self.class.get("/vehicles/#{id}")['response'])
end

#vehiclesObject



85
86
87
# File 'lib/tesla_api/client.rb', line 85

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