Class: Smartcar::Vehicle
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
Instance Attribute Summary collapse
-
#flags ⇒ Hash
Object of flags where key is the name of the flag and value is string or boolean value.
-
#id ⇒ String
Smartcar vehicle ID.
-
#options ⇒ Hash
The current value of options.
-
#service ⇒ Faraday::Connection
An optional connection object to be used for requests.
-
#token ⇒ String
Access token used to connect to Smartcar API.
-
#unit_system ⇒ String
Unit system to represent the data in, defaults to Imperial.
-
#version ⇒ String
API version to be used.
Attributes inherited from Base
Instance Method Summary collapse
-
#attributes ⇒ OpenStruct
Returns make model year and id of the vehicle.
-
#batch(paths) ⇒ OpenStruct
Method to get batch requests.
-
#battery ⇒ OpenStruct
Returns the state of charge (SOC) and remaining range of an electric or plug-in hybrid vehicle's battery.
-
#battery_capacity ⇒ OpenStruct
Returns the capacity of an electric or plug-in hybrid vehicle's battery.
-
#charge ⇒ OpenStruct
Returns the current charge status of the vehicle.
-
#engine_oil ⇒ OpenStruct
Returns the remaining life span of a vehicle's engine oil.
-
#fuel ⇒ OpenStruct
Returns the status of the fuel remaining in the vehicle's gas tank.
-
#initialize(token:, id:, options: {}) ⇒ Vehicle
constructor
A new instance of Vehicle.
-
#location ⇒ OpenStruct
Returns the last known location of the vehicle in geographic coordinates.
-
#lock_status ⇒ OpenStruct
Returns the lock status for a vehicle and the open status of its doors, windows, storage units, sunroof and charging port where available.
-
#odometer ⇒ OpenStruct
Returns the vehicle's last known odometer reading.
-
#permissions(paging = {}) ⇒ OpenStruct
Method to fetch the list of permissions that this application has been granted for this vehicle.
-
#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.
-
#send_destination!(latitude, longitude) ⇒ OpenStruct
Meta attribute with the relevant items from response headers.
-
#set_charge_limit!(limit) ⇒ OpenStruct
Set the charge limit for a given vehicle.
-
#subscribe!(webhook_id) ⇒ OpenStruct
Subscribe the vehicle to given webhook Id.
-
#tire_pressure ⇒ OpenStruct
Returns the air pressure of each of the vehicle's tires.
-
#unsubscribe!(amt, webhook_id) ⇒ OpenStruct
Unubscribe the vehicle from given webhook Id.
-
#vin ⇒ OpenStruct
Returns the vehicle's manufacturer identifier (VIN).
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.
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 = [:unit_system] || METRIC @version = [:version] || Smartcar.get_api_version @service = [:service] @query_params = { flags: stringify_params([: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
#flags ⇒ Hash
Object of flags where key is the name of the flag and value is string or boolean value.
16 17 18 |
# File 'lib/smartcar/vehicle.rb', line 16 def flags @flags end |
#id ⇒ String
Smartcar vehicle ID.
16 17 18 |
# File 'lib/smartcar/vehicle.rb', line 16 def id @id end |
#options ⇒ Hash
Returns the current value of options.
16 17 18 |
# File 'lib/smartcar/vehicle.rb', line 16 def @options end |
#service ⇒ Faraday::Connection
An optional connection object to be used for requests.
16 17 18 |
# File 'lib/smartcar/vehicle.rb', line 16 def service @service end |
#token ⇒ String
Access token used to connect to Smartcar API.
16 17 18 |
# File 'lib/smartcar/vehicle.rb', line 16 def token @token end |
#unit_system ⇒ String
Unit system to represent the data in, defaults to Imperial
16 17 18 |
# File 'lib/smartcar/vehicle.rb', line 16 def unit_system @unit_system end |
#version ⇒ String
API version to be used.
16 17 18 |
# File 'lib/smartcar/vehicle.rb', line 16 def version @version end |
Instance Method Details
#attributes ⇒ OpenStruct
Returns make model year and id of the vehicle
API Documentation - https://smartcar.com/api#get-vehicle-attributes
|
# File 'lib/smartcar/vehicle.rb', line 108
|
#batch(paths) ⇒ OpenStruct
Method to get batch requests. API - https://smartcar.com/docs/api#post-batch-request
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 |
#battery ⇒ OpenStruct
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
|
# File 'lib/smartcar/vehicle.rb', line 116
|
#battery_capacity ⇒ OpenStruct
Returns the capacity of an electric or plug-in hybrid vehicle's battery.
API Documentation https://smartcar.com/docs/api#get-ev-battery-capacity
|
# File 'lib/smartcar/vehicle.rb', line 124
|
#charge ⇒ OpenStruct
Returns the current charge status of the vehicle.
API Documentation https://smartcar.com/docs/api#get-ev-battery
|
# File 'lib/smartcar/vehicle.rb', line 132
|
#engine_oil ⇒ OpenStruct
Returns the remaining life span of a vehicle's engine oil
API Documentation https://smartcar.com/docs/api#get-engine-oil-life
|
# File 'lib/smartcar/vehicle.rb', line 140
|
#fuel ⇒ OpenStruct
Returns the status of the fuel remaining in the vehicle's gas tank.
API Documentation https://smartcar.com/docs/api#get-fuel-tank
|
# File 'lib/smartcar/vehicle.rb', line 148
|
#location ⇒ OpenStruct
Returns the last known location of the vehicle in geographic coordinates.
API Documentation https://smartcar.com/docs/api#get-location
|
# File 'lib/smartcar/vehicle.rb', line 156
|
#lock_status ⇒ OpenStruct
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
|
# File 'lib/smartcar/vehicle.rb', line 188
|
#odometer ⇒ OpenStruct
Returns the vehicle's last known odometer reading.
API Documentation https://smartcar.com/docs/api#get-odometer
|
# 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
230 231 232 233 |
# File 'lib/smartcar/vehicle.rb', line 230 def (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.
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) = (headers) json_to_ostruct({ body: raw_response, meta: }) end |
#send_destination!(latitude, longitude) ⇒ OpenStruct
Returns 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
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.
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_pressure ⇒ OpenStruct
Returns the air pressure of each of the vehicle's tires.
API Documentation https://smartcar.com/docs/api#get-tire-pressure
|
# File 'lib/smartcar/vehicle.rb', line 172
|
#unsubscribe!(amt, webhook_id) ⇒ OpenStruct
Unubscribe the vehicle from given webhook Id.
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 |
#vin ⇒ OpenStruct
Returns the vehicle's manufacturer identifier (VIN).
API Documentation https://smartcar.com/docs/api#get-vin
|
# File 'lib/smartcar/vehicle.rb', line 180
|