smartcar 2.5.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +4 -0
- data/.travis.yml +1 -2
- data/Gemfile.lock +56 -21
- data/README.md +38 -36
- data/lib/open_struct_extensions.rb +21 -0
- data/lib/smartcar.rb +158 -57
- data/lib/smartcar/auth_client.rb +154 -0
- data/lib/smartcar/base.rb +12 -22
- data/lib/smartcar/utils.rb +82 -21
- data/lib/smartcar/vehicle.rb +211 -54
- data/lib/smartcar/version.rb +1 -1
- data/lib/smartcar_error.rb +49 -0
- data/ruby-sdk.gemspec +3 -0
- metadata +47 -18
- data/lib/smartcar/battery.rb +0 -15
- data/lib/smartcar/battery_capacity.rb +0 -11
- data/lib/smartcar/charge.rb +0 -15
- data/lib/smartcar/engine_oil.rb +0 -14
- data/lib/smartcar/fuel.rb +0 -17
- data/lib/smartcar/location.rb +0 -12
- data/lib/smartcar/oauth.rb +0 -127
- data/lib/smartcar/odometer.rb +0 -11
- data/lib/smartcar/permissions.rb +0 -11
- data/lib/smartcar/tire_pressure.rb +0 -20
- data/lib/smartcar/user.rb +0 -38
- data/lib/smartcar/vehicle_attributes.rb +0 -14
- data/lib/smartcar/vehicle_utils/actions.rb +0 -61
- data/lib/smartcar/vehicle_utils/batch.rb +0 -83
- data/lib/smartcar/vehicle_utils/data.rb +0 -110
- data/lib/smartcar/vin.rb +0 -12
data/lib/smartcar/user.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Smartcar
|
4
|
-
# Class to get to user API.
|
5
|
-
# @attr [String] id Smartcar user id.
|
6
|
-
# @attr [String] token Access token used to connect to Smartcar API.
|
7
|
-
class User < Base
|
8
|
-
# Path for hitting user end point
|
9
|
-
USER_PATH = '/user'
|
10
|
-
attr_reader :id
|
11
|
-
|
12
|
-
# Class method Used to get user id
|
13
|
-
# EX : Smartcar::User.user_id
|
14
|
-
# API - https://smartcar.com/docs/api#get-user
|
15
|
-
# @param token [String] Access token
|
16
|
-
#
|
17
|
-
# @return [String] User ID
|
18
|
-
def self.user_id(token:, version: Smartcar.get_api_version)
|
19
|
-
# @deprecated Please use {#get} instead
|
20
|
-
warn '[DEPRECATION] `Smartcar::User.user_id` is deprecated and will be removed in next major version update.
|
21
|
-
Please use `Smartcar::User.get` instead.'
|
22
|
-
get(token: token, version: version).id
|
23
|
-
end
|
24
|
-
|
25
|
-
# Class method Used to get user id
|
26
|
-
# EX : Smartcar::User.get
|
27
|
-
# API - https://smartcar.com/docs/api#get-user
|
28
|
-
# @param token [String] Access token
|
29
|
-
#
|
30
|
-
# @return [User] User object
|
31
|
-
def self.get(token:, version: Smartcar.get_api_version)
|
32
|
-
user = new(token: token, version: version)
|
33
|
-
body, _meta = user.fetch(path: USER_PATH)
|
34
|
-
user.instance_variable_set('@id', body['id'])
|
35
|
-
user
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Smartcar
|
4
|
-
# class to represent Vehicle attributes like make model year
|
5
|
-
# @attr [String] id Smartcar vehicle ID
|
6
|
-
# @attr [String] make Manufacturer of the vehicle.
|
7
|
-
# @attr [String] model Model of the vehicle.
|
8
|
-
# @attr [Number] year Model year of the vehicle.
|
9
|
-
class VehicleAttributes < Base
|
10
|
-
# Path Proc for hitting vehicle attributes end point
|
11
|
-
PATH = proc { |id| "/vehicles/#{id}" }
|
12
|
-
attr_accessor :id, :make, :model, :year
|
13
|
-
end
|
14
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Smartcar
|
4
|
-
module VehicleUtils
|
5
|
-
# Actions module has all the action methods here to declutter Vehicle class
|
6
|
-
module Actions
|
7
|
-
# Method Used toRevoke access for the current requesting application
|
8
|
-
# API - https://smartcar.com/docs/api#delete-disconnect
|
9
|
-
#
|
10
|
-
# @return [Boolean] true if success
|
11
|
-
def disconnect!
|
12
|
-
response, = delete("#{Smartcar::Vehicle::PATH.call(id)}/application")
|
13
|
-
response['status'] == SUCCESS
|
14
|
-
end
|
15
|
-
|
16
|
-
# Methods Used to lock car
|
17
|
-
# API - https://smartcar.com/docs/api#post-security
|
18
|
-
#
|
19
|
-
# @return [Boolean] true if success
|
20
|
-
def lock!
|
21
|
-
lock_or_unlock!(action: Smartcar::LOCK)
|
22
|
-
end
|
23
|
-
|
24
|
-
# Methods Used to unlock car
|
25
|
-
# API - https://smartcar.com/docs/api#post-security
|
26
|
-
#
|
27
|
-
# @return [Boolean] true if success
|
28
|
-
def unlock!
|
29
|
-
lock_or_unlock!(action: Smartcar::UNLOCK)
|
30
|
-
end
|
31
|
-
|
32
|
-
# Method used to start charging a car
|
33
|
-
#
|
34
|
-
#
|
35
|
-
# @return [Boolean] true if success
|
36
|
-
def start_charge!
|
37
|
-
start_or_stop_charge!(action: Smartcar::START_CHARGE)
|
38
|
-
end
|
39
|
-
|
40
|
-
# Method used to stop charging a car
|
41
|
-
#
|
42
|
-
#
|
43
|
-
# @return [Boolean] true if success
|
44
|
-
def stop_charge!
|
45
|
-
start_or_stop_charge!(action: Smartcar::STOP_CHARGE)
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
def lock_or_unlock!(action:)
|
51
|
-
response, = post("#{Smartcar::Vehicle::PATH.call(id)}/security", { action: action })
|
52
|
-
response['status'] == SUCCESS
|
53
|
-
end
|
54
|
-
|
55
|
-
def start_or_stop_charge!(action:)
|
56
|
-
response, = post("#{Smartcar::Vehicle::PATH.call(id)}/charge", { action: action })
|
57
|
-
response['status'] == SUCCESS
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1,83 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Smartcar
|
4
|
-
module VehicleUtils
|
5
|
-
# Batch module , has all the batch related methods here to declutter Vehicle class
|
6
|
-
module Batch
|
7
|
-
DATA_CLASSES = {
|
8
|
-
battery: Smartcar::Battery,
|
9
|
-
charge: Smartcar::Charge,
|
10
|
-
engine_oil: Smartcar::EngineOil,
|
11
|
-
fuel: Smartcar::Fuel,
|
12
|
-
location: Smartcar::Location,
|
13
|
-
odometer: Smartcar::Odometer,
|
14
|
-
permissions: Smartcar::Permissions,
|
15
|
-
tire_pressure: Smartcar::TirePressure,
|
16
|
-
vin: Smartcar::Vin
|
17
|
-
}.freeze
|
18
|
-
# Method to get batch requests
|
19
|
-
# API - https://smartcar.com/docs/api#post-batch-request
|
20
|
-
# @param attributes [Array] Array of strings or symbols of attributes to be fetched together
|
21
|
-
#
|
22
|
-
# @return [Hash] Hash with key as requested attribute(symbol)
|
23
|
-
# and value as Error OR Object of the requested attribute
|
24
|
-
def batch(attributes = [])
|
25
|
-
raise InvalidParameterValue.new, 'vin is a required field' if attributes.nil?
|
26
|
-
|
27
|
-
request_body = get_batch_request_body(attributes)
|
28
|
-
response, _meta = post("#{Smartcar::Vehicle::PATH.call(id)}/batch", request_body)
|
29
|
-
process_batch_response(response)
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def allowed_attributes
|
35
|
-
@allowed_attributes ||= DATA_CLASSES.transform_values { |klass| get_path(klass) }
|
36
|
-
end
|
37
|
-
|
38
|
-
def path_to_class
|
39
|
-
@path_to_class ||= DATA_CLASSES.transform_keys { |key| get_path(DATA_CLASSES[key]) }
|
40
|
-
end
|
41
|
-
|
42
|
-
# @private
|
43
|
-
BatchItemResponse = Struct.new(:body, :status, :headers) do
|
44
|
-
def body_with_meta
|
45
|
-
body.merge(meta: headers)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def get_batch_request_body(attributes)
|
50
|
-
attributes = validated_attributes(attributes)
|
51
|
-
requests = attributes.each_with_object([]) do |item, all_requests|
|
52
|
-
all_requests << { path: allowed_attributes[item] }
|
53
|
-
end
|
54
|
-
{ requests: requests }
|
55
|
-
end
|
56
|
-
|
57
|
-
def process_batch_response(responses)
|
58
|
-
inverted_map = allowed_attributes.invert
|
59
|
-
responses['responses'].each_with_object({}) do |response, result|
|
60
|
-
item_response = BatchItemResponse.new(response['body'], response['code'], response['headers'])
|
61
|
-
error = get_error(item_response)
|
62
|
-
path = response['path']
|
63
|
-
result[inverted_map[path]] = error || path_to_class[path].new(item_response.body_with_meta)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def validated_attributes(attributes)
|
68
|
-
attributes.map!(&:to_sym)
|
69
|
-
unsupported_attributes = (attributes - allowed_attributes.keys) || []
|
70
|
-
unless unsupported_attributes.empty?
|
71
|
-
message = "Unsupported attribute(s) requested in batch - #{unsupported_attributes.join(',')}"
|
72
|
-
raise Smartcar::Base::InvalidParameterValue.new, message
|
73
|
-
end
|
74
|
-
attributes
|
75
|
-
end
|
76
|
-
|
77
|
-
def get_path(klass)
|
78
|
-
path = klass::PATH.call(id)
|
79
|
-
path.split("/vehicles/#{id}").last
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
@@ -1,110 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Smartcar
|
4
|
-
module VehicleUtils
|
5
|
-
# Data module has all the data accessor methods here to declutter Vehicle class
|
6
|
-
module Data
|
7
|
-
# Fetch the list of permissions that this application has been granted for
|
8
|
-
# this vehicle
|
9
|
-
# EX : Smartcar::Vehicle.new(token: token, id: id).permissions
|
10
|
-
# @param options [Hash] - Optional filter parameters (check documentation)
|
11
|
-
#
|
12
|
-
# @return [Permissions] object
|
13
|
-
def permissions(options: {})
|
14
|
-
get_attribute(klass: Permissions, options: options)
|
15
|
-
end
|
16
|
-
|
17
|
-
# Returns make model year and id of the vehicle
|
18
|
-
# API - https://smartcar.com/api#get-vehicle-attributes
|
19
|
-
#
|
20
|
-
# @return [VehicleAttributes] object
|
21
|
-
def vehicle_attributes
|
22
|
-
get_attribute(klass: VehicleAttributes)
|
23
|
-
end
|
24
|
-
|
25
|
-
# Returns the state of charge (SOC) and remaining range of an electric or
|
26
|
-
# plug-in hybrid vehicle's battery.
|
27
|
-
# API - https://smartcar.com/docs/api#get-ev-battery
|
28
|
-
#
|
29
|
-
# @return [Battery] object
|
30
|
-
def battery
|
31
|
-
get_attribute(klass: Battery)
|
32
|
-
end
|
33
|
-
|
34
|
-
# Returns the capacity of an electric or
|
35
|
-
# plug-in hybrid vehicle's battery.
|
36
|
-
# API - https://smartcar.com/docs/api#get-ev-battery-capacity
|
37
|
-
#
|
38
|
-
# @return [Battery] object
|
39
|
-
def battery_capacity
|
40
|
-
get_attribute(klass: BatteryCapacity)
|
41
|
-
end
|
42
|
-
|
43
|
-
# Returns the current charge status of the vehicle.
|
44
|
-
# API - https://smartcar.com/docs/api#get-ev-battery
|
45
|
-
#
|
46
|
-
# @return [Charge] object
|
47
|
-
def charge
|
48
|
-
get_attribute(klass: Charge)
|
49
|
-
end
|
50
|
-
|
51
|
-
# Returns the remaining life span of a vehicle's engine oil
|
52
|
-
# API - https://smartcar.com/docs/api#get-engine-oil-life
|
53
|
-
#
|
54
|
-
# @return [EngineOil] object
|
55
|
-
def engine_oil
|
56
|
-
get_attribute(klass: EngineOil)
|
57
|
-
end
|
58
|
-
|
59
|
-
# Returns the status of the fuel remaining in the vehicle's gas tank.
|
60
|
-
# API - https://smartcar.com/docs/api#get-fuel-tank
|
61
|
-
#
|
62
|
-
# @return [Fuel] object
|
63
|
-
def fuel
|
64
|
-
get_attribute(klass: Fuel)
|
65
|
-
end
|
66
|
-
|
67
|
-
# Returns the last known location of the vehicle in geographic coordinates.
|
68
|
-
# API - https://smartcar.com/docs/api#get-location
|
69
|
-
#
|
70
|
-
# @return [Location] object
|
71
|
-
def location
|
72
|
-
get_attribute(klass: Location)
|
73
|
-
end
|
74
|
-
|
75
|
-
# Returns the vehicle's last known odometer reading.
|
76
|
-
# API - https://smartcar.com/docs/api#get-odometer
|
77
|
-
#
|
78
|
-
# @return [Odometer] object
|
79
|
-
def odometer
|
80
|
-
get_attribute(klass: Odometer)
|
81
|
-
end
|
82
|
-
|
83
|
-
# Returns the air pressure of each of the vehicle's tires.
|
84
|
-
# API - https://smartcar.com/docs/api#get-tire-pressure
|
85
|
-
#
|
86
|
-
# @return [TirePressure] object
|
87
|
-
def tire_pressure
|
88
|
-
get_attribute(klass: TirePressure)
|
89
|
-
end
|
90
|
-
|
91
|
-
# Returns the vehicle's manufacturer identifier (VIN).
|
92
|
-
# API - https://smartcar.com/docs/api#get-vin
|
93
|
-
#
|
94
|
-
# @return [String] Vin of the vehicle.
|
95
|
-
def vin
|
96
|
-
@vin ||= get_attribute(klass: Vin).vin
|
97
|
-
end
|
98
|
-
|
99
|
-
private
|
100
|
-
|
101
|
-
def get_attribute(klass:, options: {})
|
102
|
-
body, meta = fetch(
|
103
|
-
path: klass::PATH.call(id),
|
104
|
-
options: options
|
105
|
-
)
|
106
|
-
klass.new(body.merge(meta: meta))
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
data/lib/smartcar/vin.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Smartcar
|
4
|
-
# Hidden class to represent vin
|
5
|
-
#
|
6
|
-
# @attr [String] vin Vin of the vehicle
|
7
|
-
class Vin < Base
|
8
|
-
# Path Proc for hitting vin end point
|
9
|
-
PATH = proc { |id| "/vehicles/#{id}/vin" }
|
10
|
-
attr_reader :vin
|
11
|
-
end
|
12
|
-
end
|