Class: Smartcar::Vehicle

Inherits:
Base
  • Object
show all
Defined in:
lib/smartcar/vehicle.rb

Overview

Vehicle class to connect to vehicle basic info,disconnect, lock unlock and get all vehicles API For ease of use, this also has methods define to be able to call other resources on a vehicle object For Ex. Vehicle object will be treate as an entity and doing vehicle_object. Battery should return Battery object.

Constant Summary

Constants inherited from Base

Base::BASIC

Instance Attribute Summary collapse

Attributes inherited from Base

#auth_type, #error, #url

Instance Method Summary collapse

Methods included from Utils

#build_aliases, #build_error, #build_meta, #build_response, #convert_path_to_attribute, #determine_mode, #get_config, #handle_error, #json_to_ostruct, #process_batch_response, #stringify_params

Constructor Details

#initialize(token:, id:, options: {}) ⇒ Vehicle

Returns a new instance of Vehicle.

Raises:



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/smartcar/vehicle.rb', line 94

def initialize(token:, id:, options: {})
  super
  @token = token
  @id = id
  @unit_system = options[:unit_system] || METRIC
  @version = options[:version] || Smartcar.get_api_version
  @service = options[:service]
  @query_params = { flags: stringify_params(options[:flags]) }

  raise InvalidParameterValue.new, "Invalid Units provided : #{@unit_system}" unless UNITS.include?(@unit_system)
  raise InvalidParameterValue.new, 'Vehicle ID (id) is a required field' if id.nil?
  raise InvalidParameterValue.new, 'Access Token(token) is a required field' if token.nil?
end

Instance Attribute Details

#flagsHash

Object of flags where key is the name of the flag and value is string or boolean value.

Returns:

  • (Hash)

    the current value of flags



16
17
18
# File 'lib/smartcar/vehicle.rb', line 16

def flags
  @flags
end

#idString

Smartcar vehicle ID.

Returns:

  • (String)

    the current value of id



16
17
18
# File 'lib/smartcar/vehicle.rb', line 16

def id
  @id
end

#optionsHash

Returns the current value of options.

Returns:

  • (Hash)

    the current value of options



16
17
18
# File 'lib/smartcar/vehicle.rb', line 16

def options
  @options
end

#serviceFaraday::Connection

An optional connection object to be used for requests.

Returns:

  • (Faraday::Connection)

    the current value of service



16
17
18
# File 'lib/smartcar/vehicle.rb', line 16

def service
  @service
end

#tokenString

Access token used to connect to Smartcar API.

Returns:

  • (String)

    the current value of token



16
17
18
# File 'lib/smartcar/vehicle.rb', line 16

def token
  @token
end

#unit_systemString

Unit system to represent the data in, defaults to Imperial

Returns:

  • (String)

    the current value of unit_system



16
17
18
# File 'lib/smartcar/vehicle.rb', line 16

def unit_system
  @unit_system
end

#versionString

API version to be used.

Returns:

  • (String)

    the current value of version



16
17
18
# File 'lib/smartcar/vehicle.rb', line 16

def version
  @version
end

Instance Method Details

#attributesOpenStruct

Returns make model year and id of the vehicle

API Documentation - https://smartcar.com/api#get-vehicle-attributes

Returns:



# File 'lib/smartcar/vehicle.rb', line 108

#batch(paths) ⇒ OpenStruct

Method to get batch requests. API - https://smartcar.com/docs/api#post-batch-request

Parameters:

  • paths (Array)

    Array of paths as strings. Ex ['/battery', '/odometer']

Returns:

  • (OpenStruct)

    Object with one attribute per requested path that returns an OpenStruct object of the requested attribute or taises if it is an error.



295
296
297
298
299
# File 'lib/smartcar/vehicle.rb', line 295

def batch(paths)
  request_body = { requests: paths.map { |path| { path: path } } }
  response, headers = post("/vehicles/#{id}/batch", @query_params, request_body)
  process_batch_response(response, headers)
end

#batteryOpenStruct

Returns the state of charge (SOC) and remaining range of an electric or plug-in hybrid vehicle's battery.

API Documentation https://smartcar.com/docs/api#get-ev-battery

Returns:



# File 'lib/smartcar/vehicle.rb', line 116

#battery_capacityOpenStruct

Returns the capacity of an electric or plug-in hybrid vehicle's battery.

API Documentation https://smartcar.com/docs/api#get-ev-battery-capacity

Returns:



# File 'lib/smartcar/vehicle.rb', line 124

#chargeOpenStruct

Returns the current charge status of the vehicle.

API Documentation https://smartcar.com/docs/api#get-ev-battery

Returns:



# File 'lib/smartcar/vehicle.rb', line 132

#engine_oilOpenStruct

Returns the remaining life span of a vehicle's engine oil

API Documentation https://smartcar.com/docs/api#get-engine-oil-life

Returns:



# File 'lib/smartcar/vehicle.rb', line 140

#fuelOpenStruct

Returns the status of the fuel remaining in the vehicle's gas tank.

API Documentation https://smartcar.com/docs/api#get-fuel-tank

Returns:



# File 'lib/smartcar/vehicle.rb', line 148

#locationOpenStruct

Returns the last known location of the vehicle in geographic coordinates.

API Documentation https://smartcar.com/docs/api#get-location

Returns:



# File 'lib/smartcar/vehicle.rb', line 156

#lock_statusOpenStruct

Returns the lock status for a vehicle and the open status of its doors, windows, storage units, sunroof and charging port where available. The open status array(s) will be empty if a vehicle has partial support. The request will error if lock status can not be retrieved from the vehicle or the brand is not supported.

API Documentation https://smartcar.com/docs/api#get-security

Returns:



# File 'lib/smartcar/vehicle.rb', line 188

#odometerOpenStruct

Returns the vehicle's last known odometer reading.

API Documentation https://smartcar.com/docs/api#get-odometer

Returns:



# File 'lib/smartcar/vehicle.rb', line 164

#permissions(paging = {}) ⇒ OpenStruct

Method to fetch the list of permissions that this application has been granted for this vehicle. API - https://smartcar.com/docs/api#get-application-permissions

Parameters:

  • paging (Hash) (defaults to: {})

    Optional filter parameters (check documentation)

Returns:



230
231
232
233
# File 'lib/smartcar/vehicle.rb', line 230

def permissions(paging = {})
  response, headers = get(METHODS.dig(:permissions, :path).call(id), @query_params.merge(paging))
  build_response(response, headers)
end

#request(method, path, body = {}, headers = {}) ⇒ OpenStruct

General purpose method to make requests to the Smartcar API - can be used to make requests to brand specific endpoints.

Parameters:

  • method (String)

    The HTTP request method to use.

  • path (String)

    The path to make the request to.

  • body (Hash) (defaults to: {})

    The request body.

  • headers (Hash) (defaults to: {})

    The headers to inlcude in the request.

Returns:

  • (OpenStruct)

    An object with a "body" attribute that contains the response body and a "meta" attribute with the relevant items from response headers.



311
312
313
314
315
316
# File 'lib/smartcar/vehicle.rb', line 311

def request(method, path, body = {}, headers = {})
  path = "/vehicles/#{id}/#{path}"
  raw_response, headers = send(method.downcase, path, @query_params, body, headers)
  meta = build_meta(headers)
  json_to_ostruct({ body: raw_response, meta: meta })
end

#send_destination!(latitude, longitude) ⇒ OpenStruct

Returns Meta attribute with the relevant items from response headers.

Returns:

  • (OpenStruct)

    Meta attribute with the relevant items from response headers



281
282
283
284
285
286
287
# File 'lib/smartcar/vehicle.rb', line 281

def send_destination!(latitude, longitude)
  method_config = METHODS[:send_destination!]

  response, headers = post(method_config[:path].call(id), @query_params,
                           method_config[:body].call(latitude, longitude))
  build_response(response, headers)
end

#set_charge_limit!(limit) ⇒ OpenStruct

Set the charge limit for a given vehicle

Parameters:

  • limit (float)

    A value between 0 and 1 denoting the charge limit to be set.

Returns:

  • (OpenStruct)

    Meta attribute with the relevant items from response headers.



267
268
269
270
271
272
273
# File 'lib/smartcar/vehicle.rb', line 267

def set_charge_limit!(limit)
  path = METHODS.dig(:set_charge_limit!, :path).call(id)
  body = METHODS.dig(:set_charge_limit!, :body).call(limit)

  response, headers = post(path, {}, body)
  build_response(response, headers)
end

#subscribe!(webhook_id) ⇒ OpenStruct

Subscribe the vehicle to given webhook Id.

Parameters:

  • webhook_id (String)

    Webhook id to subscribe to

Returns:

  • (OpenStruct)

    An object representing the JSON response and a meta attribute with the relevant items from response headers.



241
242
243
244
# File 'lib/smartcar/vehicle.rb', line 241

def subscribe!(webhook_id)
  response, headers = post(METHODS.dig(:subscribe!, :path).call(id, webhook_id), @query_params)
  build_aliases(build_response(response, headers), METHODS.dig(:subscribe!, :aliases))
end

#tire_pressureOpenStruct

Returns the air pressure of each of the vehicle's tires.

API Documentation https://smartcar.com/docs/api#get-tire-pressure

Returns:



# File 'lib/smartcar/vehicle.rb', line 172

#unsubscribe!(amt, webhook_id) ⇒ OpenStruct

Unubscribe the vehicle from given webhook Id.

Parameters:

  • amt (String)

    Application management token

  • webhook_id (String)

    Webhook id to subscribe to

Returns:

  • (OpenStruct)

    Meta attribute with the relevant items from response headers.



252
253
254
255
256
257
258
259
260
# File 'lib/smartcar/vehicle.rb', line 252

def unsubscribe!(amt, webhook_id)
  # swapping off the token with amt for unsubscribe.
  access_token = token
  self.token = amt
  response, headers = delete(METHODS.dig(:unsubscribe!, :path).call(id, webhook_id),
                             @query_params)
  self.token = access_token
  build_response(response, headers)
end

#vinOpenStruct

Returns the vehicle's manufacturer identifier (VIN).

API Documentation https://smartcar.com/docs/api#get-vin

Returns:



# File 'lib/smartcar/vehicle.rb', line 180