Class: TeslaApi::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, client_id = ENV["TESLA_CLIENT_ID"], client_secret = ENV["TESLA_CLIENT_SECRET"]) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
# File 'lib/tesla_api/client.rb', line 9

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

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



7
8
9
# File 'lib/tesla_api/client.rb', line 7

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



7
8
9
# File 'lib/tesla_api/client.rb', line 7

def client_secret
  @client_secret
end

#emailObject (readonly)

Returns the value of attribute email.



7
8
9
# File 'lib/tesla_api/client.rb', line 7

def email
  @email
end

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/tesla_api/client.rb', line 7

def token
  @token
end

Instance Method Details

#login!(password) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tesla_api/client.rb', line 20

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

  self.token = response["access_token"]
end

#vehiclesObject



35
36
37
# File 'lib/tesla_api/client.rb', line 35

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