class Smartcar::Vehicle

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.

@attr [String] token Access token used to connect to Smartcar API. @attr [String] id Smartcar vehicle ID. @attr [Hash] options @attr unit_system [String] Unit system to represent the data in, defaults to Imperial @attr version [String] API version to be used.

Constants

METHODS

@private

Attributes

id[R]

Public Class Methods

new(token:, id:, options: { unit_system: METRIC, version: Smartcar.get_api_version }) click to toggle source
Calls superclass method Smartcar::Utils::new
# File lib/smartcar/vehicle.rb, line 72
def initialize(token:, id:, options: { unit_system: METRIC, version: Smartcar.get_api_version })
  super
  @token = token
  @id = id
  @unit_system = options[:unit_system]
  @version = options[:version]

  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

Public Instance Methods

batch(paths) click to toggle source

Method to get batch requests. API - smartcar.com/docs/api#post-batch-request @param paths [Array] Array of paths as strings. Ex ['/battery', '/odometer']

@return [OpenStruct] Object with one attribute per requested path that returns

an OpenStruct object of the requested attribute or taises if it is an error.
# File lib/smartcar/vehicle.rb, line 232
def batch(paths)
  request_body = { requests: paths.map { |path| { path: path } } }
  response, headers = post("/vehicles/#{id}/batch", request_body)
  process_batch_response(response, headers)
end
permissions(paging = {}) click to toggle source

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

@param paging [Hash] Optional filter parameters (check documentation)

@return [OpenStruct] And object representing the JSON response mentioned in smartcar.com/docs/api#get-application-permissions

and a meta attribute with the relevant items from response headers.
# File lib/smartcar/vehicle.rb, line 195
def permissions(paging = {})
  response, headers = fetch(path: METHODS.dig(:permissions, :path).call(id), query_params: paging)
  build_response(response, headers)
end
subscribe!(webhook_id) click to toggle source

Subscribe the vehicle to given webhook Id.

@param webhook_id [String] Webhook id to subscribe to

@return [OpenStruct] And object representing the JSON response and a meta attribute

with the relevant items from response headers.
# File lib/smartcar/vehicle.rb, line 206
def subscribe!(webhook_id)
  response, headers = post(METHODS.dig(:subscribe!, :path).call(id, webhook_id), {})
  build_aliases(build_response(response, headers), METHODS.dig(:subscribe!, :aliases))
end
unsubscribe!(amt, webhook_id) click to toggle source

Unubscribe the vehicle from given webhook Id.

@param amt [String] Application management token @param webhook_id [String] Webhook id to subscribe to

@return [OpenStruct] Meta attribute with the relevant items from response headers.

# File lib/smartcar/vehicle.rb, line 217
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))
  self.token = access_token
  build_response(response, headers)
end