smartcar 3.3.1 → 3.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/Gemfile.lock +5 -5
- data/README.md +2 -0
- data/lib/smartcar/base.rb +2 -2
- data/lib/smartcar/vehicle.rb +20 -0
- data/lib/smartcar/version.rb +1 -1
- data/lib/smartcar.rb +69 -3
- data/lib/smartcar_error.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b73275a395285b7814ea6a00e15ba722b34c04e93f04f911558243cd906fb85
|
4
|
+
data.tar.gz: 46a585c74ab00a8c61fb3493a1f4cff476cbbbcae525be27124c5fdffb5be64b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 448a25c995db755a1b5d198d4843bb46da8c58512c8c42d83e9efcd81c68b8e57e8586fc0eba401936da85b64392aa95f8f05f3d9b1d80843764cae42ee6cd04
|
7
|
+
data.tar.gz: 4a3aa2472759eec45741c9d842cb336fccb5e1b4fc98859400aba6f526ac6a89c8ae0f1aedb9f87486e18e15e34c80a15855361aa1f909d9d83bebb21bc7ada6
|
data/.rubocop.yml
CHANGED
@@ -7,6 +7,10 @@ AllCops:
|
|
7
7
|
Metrics/AbcSize:
|
8
8
|
Enabled: false
|
9
9
|
|
10
|
+
# Lengthen module line length
|
11
|
+
Metrics/ModuleLength:
|
12
|
+
Max: 200
|
13
|
+
|
10
14
|
# Disabling this becuase we are using `set` and `get` prefixed methods to keep some commonality across SDKs
|
11
15
|
Naming/AccessorMethodName:
|
12
16
|
Enabled: false
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
smartcar (3.
|
4
|
+
smartcar (3.6.0)
|
5
5
|
oauth2 (~> 1.4)
|
6
6
|
recursive-open-struct (~> 1.1.3)
|
7
7
|
|
@@ -20,15 +20,15 @@ GEM
|
|
20
20
|
rexml
|
21
21
|
diff-lcs (1.5.0)
|
22
22
|
docile (1.4.0)
|
23
|
-
faraday (2.
|
23
|
+
faraday (2.4.0)
|
24
24
|
faraday-net_http (~> 2.0)
|
25
25
|
ruby2_keywords (>= 0.0.4)
|
26
|
-
faraday-net_http (2.0
|
26
|
+
faraday-net_http (2.1.0)
|
27
27
|
hashdiff (1.0.1)
|
28
|
-
jwt (2.
|
28
|
+
jwt (2.4.1)
|
29
29
|
multi_json (1.15.0)
|
30
30
|
multi_xml (0.6.0)
|
31
|
-
oauth2 (1.4.
|
31
|
+
oauth2 (1.4.10)
|
32
32
|
faraday (>= 0.17.3, < 3.0)
|
33
33
|
jwt (>= 1.0, < 3.0)
|
34
34
|
multi_json (~> 1.3)
|
data/README.md
CHANGED
@@ -144,6 +144,8 @@ Example of providing a custom Faraday connection to various methods:
|
|
144
144
|
|
145
145
|
# Passing the custom service into a Smartcar::Vehicle object
|
146
146
|
vehicle = Smartcar::Vehicle.new(token: token, id: id, options: { service: service })
|
147
|
+
|
148
|
+
connections = Smartcar.get_connections(amt: 'amt', filter: {userId: 'user-id'}, options: {service: service})
|
147
149
|
```
|
148
150
|
|
149
151
|
## Development
|
data/lib/smartcar/base.rb
CHANGED
@@ -14,7 +14,7 @@ module Smartcar
|
|
14
14
|
# Constant for Basic auth type
|
15
15
|
BASIC = 'Basic'
|
16
16
|
|
17
|
-
attr_accessor :token, :error, :unit_system, :version, :auth_type
|
17
|
+
attr_accessor :token, :error, :unit_system, :version, :auth_type, :url
|
18
18
|
|
19
19
|
%i[get post patch put delete].each do |verb|
|
20
20
|
# meta programming and define all Restful methods.
|
@@ -54,7 +54,7 @@ module Smartcar
|
|
54
54
|
# @return [OAuth2::AccessToken] An initialized AccessToken instance that acts as service client
|
55
55
|
def service
|
56
56
|
@service ||= Faraday.new(
|
57
|
-
url: ENV['SMARTCAR_API_ORIGIN'] || API_ORIGIN,
|
57
|
+
url: url || ENV['SMARTCAR_API_ORIGIN'] || API_ORIGIN,
|
58
58
|
request: { timeout: DEFAULT_REQUEST_TIMEOUT }
|
59
59
|
)
|
60
60
|
end
|
data/lib/smartcar/vehicle.rb
CHANGED
@@ -52,11 +52,18 @@ module Smartcar
|
|
52
52
|
}
|
53
53
|
},
|
54
54
|
vin: { path: proc { |id| "/vehicles/#{id}/vin" } },
|
55
|
+
get_charge_limit: { path: proc { |id| "/vehicles/#{id}/charge/limit" } },
|
55
56
|
disconnect!: { type: :delete, path: proc { |id| "/vehicles/#{id}/application" } },
|
56
57
|
lock!: { type: :post, path: proc { |id| "/vehicles/#{id}/security" }, body: { action: 'LOCK' } },
|
57
58
|
unlock!: { type: :post, path: proc { |id| "/vehicles/#{id}/security" }, body: { action: 'UNLOCK' } },
|
58
59
|
start_charge!: { type: :post, path: proc { |id| "/vehicles/#{id}/charge" }, body: { action: 'START' } },
|
59
60
|
stop_charge!: { type: :post, path: proc { |id| "/vehicles/#{id}/charge" }, body: { action: 'STOP' } },
|
61
|
+
set_charge_limit!: {
|
62
|
+
type: :post,
|
63
|
+
path: proc { |id| "/vehicles/#{id}/charge/limit" },
|
64
|
+
body: proc { |limit| { limit: limit } },
|
65
|
+
skip: true
|
66
|
+
},
|
60
67
|
subscribe!: {
|
61
68
|
type: :post,
|
62
69
|
path: proc { |id, webhook_id| "/vehicles/#{id}/webhooks/#{webhook_id}" },
|
@@ -228,6 +235,19 @@ module Smartcar
|
|
228
235
|
build_response(response, headers)
|
229
236
|
end
|
230
237
|
|
238
|
+
# Set the charge limit for a given vehicle
|
239
|
+
#
|
240
|
+
# @param limit [float] A value between 0 and 1 denoting the charge limit to be set.
|
241
|
+
#
|
242
|
+
# @return [OpenStruct] Meta attribute with the relevant items from response headers.
|
243
|
+
def set_charge_limit!(limit)
|
244
|
+
path = METHODS.dig(:set_charge_limit!, :path).call(id)
|
245
|
+
body = METHODS.dig(:set_charge_limit!, :body).call(limit)
|
246
|
+
|
247
|
+
response, headers = post(path, {}, body)
|
248
|
+
build_response(response, headers)
|
249
|
+
end
|
250
|
+
|
231
251
|
# Method to get batch requests.
|
232
252
|
# API - https://smartcar.com/docs/api#post-batch-request
|
233
253
|
# @param paths [Array] Array of paths as strings. Ex ['/battery', '/odometer']
|
data/lib/smartcar/version.rb
CHANGED
data/lib/smartcar.rb
CHANGED
@@ -15,10 +15,12 @@ module Smartcar
|
|
15
15
|
|
16
16
|
# Host to connect to smartcar
|
17
17
|
API_ORIGIN = 'https://api.smartcar.com/'
|
18
|
+
MANAGEMENT_API_ORIGIN = 'https://management.smartcar.com'
|
18
19
|
PATHS = {
|
19
20
|
compatibility: '/compatibility',
|
20
21
|
user: '/user',
|
21
|
-
vehicles: '/vehicles'
|
22
|
+
vehicles: '/vehicles',
|
23
|
+
connections: '/management/connections'
|
22
24
|
}.freeze
|
23
25
|
|
24
26
|
# Path for smartcar oauth
|
@@ -80,8 +82,8 @@ module Smartcar
|
|
80
82
|
# @return [OpenStruct] And object representing the JSON response mentioned in https://smartcar.com/docs/api#compatibility-api
|
81
83
|
# and a meta attribute with the relevant items from response headers.
|
82
84
|
def get_compatibility(vin:, scope:, country: 'US', options: {})
|
83
|
-
raise InvalidParameterValue.new, 'vin is a required field' if vin.nil?
|
84
|
-
raise InvalidParameterValue.new, 'scope is a required field' if scope.nil?
|
85
|
+
raise Base::InvalidParameterValue.new, 'vin is a required field' if vin.nil?
|
86
|
+
raise Base::InvalidParameterValue.new, 'scope is a required field' if scope.nil? || scope.empty?
|
85
87
|
|
86
88
|
base_object = Base.new(
|
87
89
|
{
|
@@ -166,6 +168,70 @@ module Smartcar
|
|
166
168
|
hash_challenge(amt, body.to_json) == signature
|
167
169
|
end
|
168
170
|
|
171
|
+
# Module method Returns a paged list of all vehicle connections connected to the application.
|
172
|
+
#
|
173
|
+
# API Documentation - https://smartcar.com/docs/api#get-connections
|
174
|
+
# @param amt [String] - Application Management token
|
175
|
+
# @param filters [Hash] - Optional filter parameters (check documentation)
|
176
|
+
# @param paging [Hash] - Pass a cursor for paginated results
|
177
|
+
# @param options [Hash] Other optional parameters including overrides
|
178
|
+
# @option options [Faraday::Connection] :service Optional connection object to be used for requests
|
179
|
+
# @option options [String] :version Optional API version to use, defaults to what is globally set
|
180
|
+
#
|
181
|
+
# @return [OpenStruct] And object representing the JSON response mentioned in https://smartcar.com/docs/api#get-connections
|
182
|
+
# and a meta attribute with the relevant items from response headers.
|
183
|
+
def get_connections(amt:, filter: {}, paging: {}, options: {})
|
184
|
+
paging[:limit] ||= 10
|
185
|
+
base_object = Base.new(
|
186
|
+
token: generate_basic_management_auth(amt, options),
|
187
|
+
version: options[:version] || Smartcar.get_api_version,
|
188
|
+
service: options[:service],
|
189
|
+
auth_type: Base::BASIC,
|
190
|
+
url: ENV['SMARTCAR_MANAGEMENT_API_ORIGIN'] || MANAGEMENT_API_ORIGIN
|
191
|
+
)
|
192
|
+
query_params = filter.merge(paging).compact
|
193
|
+
|
194
|
+
base_object.build_response(*base_object.get(
|
195
|
+
PATHS[:connections],
|
196
|
+
query_params
|
197
|
+
))
|
198
|
+
end
|
199
|
+
|
200
|
+
def delete_connections(amt:, filter: {}, options: {})
|
201
|
+
user_id = filter[:user_id]
|
202
|
+
vehicle_id = filter[:vehicle_id]
|
203
|
+
error_message = nil
|
204
|
+
error_message = 'Filter can contain EITHER user_id OR vehicle_id, not both.' if user_id && vehicle_id
|
205
|
+
error_message = 'Filter needs one of user_id OR vehicle_id.' unless user_id || vehicle_id
|
206
|
+
|
207
|
+
raise Base::InvalidParameterValue.new, error_message if error_message
|
208
|
+
|
209
|
+
query_params = {}
|
210
|
+
query_params['user_id'] = user_id if user_id
|
211
|
+
query_params['vehicle_id'] = vehicle_id if vehicle_id
|
212
|
+
|
213
|
+
base_object = Base.new(
|
214
|
+
url: ENV['SMARTCAR_MANAGEMENT_API_ORIGIN'] || MANAGEMENT_API_ORIGIN,
|
215
|
+
auth_type: Base::BASIC,
|
216
|
+
token: generate_basic_management_auth(amt, options),
|
217
|
+
version: options[:version] || Smartcar.get_api_version,
|
218
|
+
service: options[:service]
|
219
|
+
)
|
220
|
+
|
221
|
+
base_object.build_response(*base_object.delete(
|
222
|
+
PATHS[:connections],
|
223
|
+
query_params
|
224
|
+
))
|
225
|
+
end
|
226
|
+
|
227
|
+
# returns auth token for Basic vehicle management auth
|
228
|
+
#
|
229
|
+
# @return [String] Base64 encoding of default:amt
|
230
|
+
def generate_basic_management_auth(amt, options = {})
|
231
|
+
username = options[:username] || 'default'
|
232
|
+
Base64.strict_encode64("#{username}:#{amt}")
|
233
|
+
end
|
234
|
+
|
169
235
|
private
|
170
236
|
|
171
237
|
def build_compatibility_params(vin, scope, country, options)
|
data/lib/smartcar_error.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# Custom SmartcarError class to represent errors from Smartcar APIs.
|
4
4
|
class SmartcarError < StandardError
|
5
|
-
attr_reader :code, :status_code, :request_id, :type, :description, :doc_url, :resolution, :detail
|
5
|
+
attr_reader :code, :status_code, :request_id, :type, :description, :doc_url, :resolution, :detail, :retry_after
|
6
6
|
|
7
7
|
def initialize(status, body, headers)
|
8
8
|
@status_code = status
|
@@ -11,6 +11,7 @@ class SmartcarError < StandardError
|
|
11
11
|
@request_id = headers['sc-request-id']
|
12
12
|
return
|
13
13
|
end
|
14
|
+
@retry_after = headers['retry-after']
|
14
15
|
body = coerce_attributes(body)
|
15
16
|
|
16
17
|
super("#{body[:type]}:#{body[:code]} - #{body[:description]}")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smartcar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ashwin Subramanian
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|