Sha256: b340c90840ddf3d4fd2943edc838346ec82ba5bcd5e0673a9d7f1c4ad8d33d66

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

module TeslaApi
  class Client
    include HTTParty
    base_uri "https://owner-api.teslamotors.com/api/1"
    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"])
      @email = email
      @client_id = client_id
      @client_secret = client_secret
    end

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

    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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tesla_api-1.2.0 lib/tesla_api/client.rb
tesla_api-1.1.0 lib/tesla_api/client.rb