smartcar 3.2.0 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af85a8f04bb5ecfd37182ae539ef60e5ca4e9487edf5db544109417df43d82d0
4
- data.tar.gz: 5f2ccac9657e5e5b122b32943fa48845aa0acd5319dccc8836fd6b3cbf063ced
3
+ metadata.gz: 93454e55a42ab20160c545c9bda459e98c85fe5596519f704ffa001bcef5896f
4
+ data.tar.gz: 0a8148c0f6678079467ee10d58b8564b2dec7e08127a3e7691e734503fc4128b
5
5
  SHA512:
6
- metadata.gz: 47abd5b611ddc7089be88614c2b79a4d2c33db498f1254eea55f2ed1426b795752b47b7e08e9ac8825086790313738f16affdf536ebed450963b36fd0d1305e3
7
- data.tar.gz: 3ffef0a6f8f06da4c9f70a6798a9f701521831cdc30bbc183e613515b2b9520a492bd2d9e686a5c4cca0421d42def98bc0ccb6c4239cabd1d7b250ba5416f31d
6
+ metadata.gz: a5fb72f2ee3aab427c7eefd12628947eb3f5e8644f40f898acea27cdc1b6c47d7d1085b936b7661df699a31baa59e162efa7641cce7b18b823796cd6d392bc3b
7
+ data.tar.gz: dc0ab7c95a537ea9a87c7e8f11ef13de8f8bd752625e0ec0418896de573a05d635837499e2744f53f7881a13c42cf9265e6799727bb68e424c4f57dccdd62370
data/.rubocop.yml CHANGED
@@ -12,7 +12,7 @@ Naming/AccessorMethodName:
12
12
  Enabled: false
13
13
 
14
14
  # Disabling this until we figure out a better way than using openstruct
15
- # Currently we use open struct because this gives us an object representing the JSON
15
+ # Currently we use open struct because this gives us an object representing the JSON
16
16
  # with accessor style methods.
17
17
  Style/OpenStructUse:
18
18
  Enabled: false
@@ -30,3 +30,9 @@ Metrics/MethodLength:
30
30
 
31
31
  Metrics/ClassLength:
32
32
  Max: 200
33
+
34
+ Metrics/CyclomaticComplexity:
35
+ Max: 10
36
+
37
+ Metrics/PerceivedComplexity:
38
+ Max: 10
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smartcar (3.2.0)
4
+ smartcar (3.3.0)
5
5
  oauth2 (~> 1.4)
6
6
  recursive-open-struct (~> 1.1.3)
7
7
 
data/README.md CHANGED
@@ -101,7 +101,7 @@ Example Usage for calling the reports API with oAuth token
101
101
  Example Usage for oAuth -
102
102
  ```ruby
103
103
  # To get the redirect URL :
104
- 2.5.5 :002 > options = {test_mode: true}
104
+ 2.5.5 :002 > options = {mode: 'test'}
105
105
  2.5.5 :003 > require 'smartcar'
106
106
  2.5.5 :004 > client = Smartcar::AuthClient.new(options)
107
107
  2.5.5 :005 > url = client.get_auth_url(["read_battery","read_charge","read_fuel","read_location","control_security","read_odometer","read_tires","read_vin","read_vehicle_info"], {flags: ["country:DE"]})
@@ -14,15 +14,17 @@ module Smartcar
14
14
  # @option options[:client_id] [String] - Client ID, if not passed fallsback to ENV['SMARTCAR_CLIENT_ID']
15
15
  # @option options[:client_secret] [String] - Client Secret, if not passed fallsback to ENV['SMARTCAR_CLIENT_SECRET']
16
16
  # @option options[:redirect_uri] [String] - Redirect URI, if not passed fallsback to ENV['SMARTCAR_REDIRECT_URI']
17
- # @option options[:test_mode] [Boolean] - Setting this to 'true' runs it in test mode.
18
- #
17
+ # @option options[:test_mode] [Boolean] - [DEPRECATED], please use `mode` instead.
18
+ # Launch Smartcar Connect in [test mode](https://smartcar.com/docs/guides/testing/).
19
+ # @option options[:mode] [String] - Determine what mode Smartcar Connect should be launched in.
20
+ # Should be one of test, live or simulated.
19
21
  # @return [Smartcar::AuthClient] Returns a Smartcar::AuthClient Object that has other methods
20
22
  def initialize(options)
21
23
  options[:redirect_uri] ||= get_config('SMARTCAR_REDIRECT_URI')
22
24
  options[:client_id] ||= get_config('SMARTCAR_CLIENT_ID')
23
25
  options[:client_secret] ||= get_config('SMARTCAR_CLIENT_SECRET')
24
- options[:mode] = options[:test_mode].is_a?(TrueClass) ? TEST : LIVE
25
26
  options[:origin] = ENV['SMARTCAR_AUTH_ORIGIN'] || AUTH_ORIGIN
27
+ options[:mode] = determine_mode(options[:test_mode], options[:mode]) || 'live'
26
28
  super
27
29
  end
28
30
 
data/lib/smartcar/base.rb CHANGED
@@ -22,7 +22,7 @@ module Smartcar
22
22
  # @param data [Hash] request body if needed.
23
23
  #
24
24
  # @return [Hash] The response Json parsed as a hash.
25
- define_method verb do |path, data = nil, headers = {}|
25
+ define_method verb do |path, query_params = {}, data = nil, headers = {}|
26
26
  response = service.send(verb) do |request|
27
27
  request_headers = {}
28
28
  request_headers['Authorization'] = auth_type == BASIC ? "Basic #{token}" : "Bearer #{token}"
@@ -32,6 +32,7 @@ module Smartcar
32
32
  "Smartcar/#{VERSION} (#{RbConfig::CONFIG['host_os']}; #{RbConfig::CONFIG['arch']}) Ruby v#{RUBY_VERSION}"
33
33
  request.headers = request_headers.merge(headers)
34
34
  complete_path = "/v#{version}#{path}"
35
+ complete_path += "?#{URI.encode_www_form(query_params.compact)}" unless query_params.empty?
35
36
  if verb == :get
36
37
  request.url complete_path, data
37
38
  else
@@ -46,17 +47,6 @@ module Smartcar
46
47
  end
47
48
  end
48
49
 
49
- # This requires a proc 'PATH' to be defined in the class
50
- # @param path [String] resource path
51
- # @param query_params [Hash] query params
52
- # @param auth [String] type of auth
53
- #
54
- # @return [Object]
55
- def fetch(path:, query_params: {})
56
- path += "?#{URI.encode_www_form(query_params)}" unless query_params.empty?
57
- get(path)
58
- end
59
-
60
50
  private
61
51
 
62
52
  # gets a smartcar API service/client
@@ -127,5 +127,25 @@ module Smartcar
127
127
 
128
128
  path.split('/').reject(&:empty?).join('_').to_sym
129
129
  end
130
+
131
+ # takes query parameters and returns them as a string
132
+ # EX - {'country': 'DE', 'flags': true} -> "county:DE flags:true"
133
+ def stringify_params(query_params)
134
+ query_params&.map { |key, value| "#{key}:#{value}" }&.join(' ')
135
+ end
136
+
137
+ def determine_mode(test_mode, mode)
138
+ unless mode.nil?
139
+ unless %w[test live simulated].include? mode
140
+ raise 'The "mode" parameter MUST be one of the following: \'test\', \'live\', \'simulated\''
141
+ end
142
+
143
+ return mode
144
+ end
145
+ return if test_mode.nil?
146
+
147
+ warn '[DEPRECATION] The "test_mode" parameter is deprecated, please use the "mode" parameter instead.'
148
+ test_mode.is_a?(TrueClass) ? 'test' : 'live'
149
+ end
130
150
  end
131
151
  end
@@ -11,6 +11,7 @@ module Smartcar
11
11
  # @attr [Hash] options
12
12
  # @attr unit_system [String] Unit system to represent the data in, defaults to Imperial
13
13
  # @attr version [String] API version to be used.
14
+ # @attr flags [Hash] Object of flags where key is the name of the flag and value is string or boolean value.
14
15
  # @attr service [Faraday::Connection] An optional connection object to be used for requests.
15
16
  class Vehicle < Base
16
17
  attr_reader :id
@@ -77,6 +78,7 @@ module Smartcar
77
78
  @unit_system = options[:unit_system] || METRIC
78
79
  @version = options[:version] || Smartcar.get_api_version
79
80
  @service = options[:service]
81
+ @query_params = { flags: stringify_params(options[:flags]) }
80
82
 
81
83
  raise InvalidParameterValue.new, "Invalid Units provided : #{@unit_system}" unless UNITS.include?(@unit_system)
82
84
  raise InvalidParameterValue.new, 'Vehicle ID (id) is a required field' if id.nil?
@@ -177,11 +179,11 @@ module Smartcar
177
179
  define_method method do
178
180
  body, headers = case item[:type]
179
181
  when :post
180
- post(item[:path].call(id), item[:body])
182
+ post(item[:path].call(id), @query_params, item[:body])
181
183
  when :delete
182
- delete(item[:path].call(id))
184
+ delete(item[:path].call(id), @query_params)
183
185
  else
184
- fetch(path: item[:path].call(id))
186
+ get(item[:path].call(id), @query_params)
185
187
  end
186
188
  build_aliases(build_response(body, headers), item[:aliases])
187
189
  end
@@ -195,7 +197,7 @@ module Smartcar
195
197
  # @return [OpenStruct] And object representing the JSON response mentioned in https://smartcar.com/docs/api#get-application-permissions
196
198
  # and a meta attribute with the relevant items from response headers.
197
199
  def permissions(paging = {})
198
- response, headers = fetch(path: METHODS.dig(:permissions, :path).call(id), query_params: paging)
200
+ response, headers = get(METHODS.dig(:permissions, :path).call(id), @query_params.merge(paging))
199
201
  build_response(response, headers)
200
202
  end
201
203
 
@@ -206,7 +208,7 @@ module Smartcar
206
208
  # @return [OpenStruct] An object representing the JSON response and a meta attribute
207
209
  # with the relevant items from response headers.
208
210
  def subscribe!(webhook_id)
209
- response, headers = post(METHODS.dig(:subscribe!, :path).call(id, webhook_id), {})
211
+ response, headers = post(METHODS.dig(:subscribe!, :path).call(id, webhook_id), @query_params)
210
212
  build_aliases(build_response(response, headers), METHODS.dig(:subscribe!, :aliases))
211
213
  end
212
214
 
@@ -220,7 +222,8 @@ module Smartcar
220
222
  # swapping off the token with amt for unsubscribe.
221
223
  access_token = token
222
224
  self.token = amt
223
- response, headers = delete(METHODS.dig(:unsubscribe!, :path).call(id, webhook_id))
225
+ response, headers = delete(METHODS.dig(:unsubscribe!, :path).call(id, webhook_id),
226
+ @query_params)
224
227
  self.token = access_token
225
228
  build_response(response, headers)
226
229
  end
@@ -233,7 +236,7 @@ module Smartcar
233
236
  # an OpenStruct object of the requested attribute or taises if it is an error.
234
237
  def batch(paths)
235
238
  request_body = { requests: paths.map { |path| { path: path } } }
236
- response, headers = post("/vehicles/#{id}/batch", request_body)
239
+ response, headers = post("/vehicles/#{id}/batch", @query_params, request_body)
237
240
  process_batch_response(response, headers)
238
241
  end
239
242
 
@@ -249,7 +252,7 @@ module Smartcar
249
252
  # response body and a "meta" attribute with the relevant items from response headers.
250
253
  def request(method, path, body = {}, headers = {})
251
254
  path = "/vehicles/#{id}/#{path}"
252
- raw_response, headers = send(method.downcase, path, body, headers)
255
+ raw_response, headers = send(method.downcase, path, @query_params, body, headers)
253
256
  meta = build_meta(headers)
254
257
  json_to_ostruct({ body: raw_response, meta: meta })
255
258
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Smartcar
4
4
  # Gem current version number
5
- VERSION = '3.2.0'
5
+ VERSION = '3.3.0'
6
6
  end
data/lib/smartcar.rb CHANGED
@@ -38,6 +38,7 @@ module Smartcar
38
38
  @api_version = '2.0'
39
39
 
40
40
  class << self
41
+ include Smartcar::Utils
41
42
  # Module method Used to set api version to be used.
42
43
  # This method can be used at the top to set the version and any
43
44
  # following request will use the version set here unless overridden
@@ -67,7 +68,10 @@ module Smartcar
67
68
  # @option options [String] :client_secret Client Secret that overrides ENV
68
69
  # @option options [String] :version API version to use, defaults to what is globally set
69
70
  # @option options [Hash] :flags A hash of flag name string as key and a string or boolean value.
70
- # @option options [Boolean] :test_mode Whether to use test mode or not.
71
+ # @option options[Boolean] :test_mode [DEPRECATED], please use `mode` instead.
72
+ # Launch Smartcar Connect in test mode(https://smartcar.com/docs/guides/testing/).
73
+ # @option options [String] :mode Determine what mode Smartcar Connect should be launched in.
74
+ # Should be one of test, live or simulated.
71
75
  # @option options [String] :test_mode_compatibility_level this is required argument while using
72
76
  # test mode with a real vin. For more information refer to docs.
73
77
  # @option options [Faraday::Connection] :service Optional connection object to be used for requests
@@ -88,9 +92,9 @@ module Smartcar
88
92
 
89
93
  base_object.token = generate_basic_auth(options, base_object)
90
94
 
91
- base_object.build_response(*base_object.fetch(
92
- path: PATHS[:compatibility],
93
- query_params: build_compatibility_params(vin, scope, country, options)
95
+ base_object.build_response(*base_object.get(
96
+ PATHS[:compatibility],
97
+ build_compatibility_params(vin, scope, country, options)
94
98
  ))
95
99
  end
96
100
 
@@ -112,7 +116,7 @@ module Smartcar
112
116
  service: options[:service]
113
117
  }
114
118
  )
115
- base_object.build_response(*base_object.fetch(path: PATHS[:user]))
119
+ base_object.build_response(*base_object.get(PATHS[:user]))
116
120
  end
117
121
 
118
122
  # Module method Returns a paged list of all vehicles connected to the application for the current authorized user.
@@ -134,9 +138,9 @@ module Smartcar
134
138
  service: options[:service]
135
139
  }
136
140
  )
137
- base_object.build_response(*base_object.fetch(
138
- path: PATHS[:vehicles],
139
- query_params: paging
141
+ base_object.build_response(*base_object.get(
142
+ PATHS[:vehicles],
143
+ paging
140
144
  ))
141
145
  end
142
146
 
@@ -169,15 +173,15 @@ module Smartcar
169
173
  scope: scope.join(' '),
170
174
  country: country
171
175
  }
172
- query_params[:flags] = options[:flags].map { |key, value| "#{key}:#{value}" }.join(' ') if options[:flags]
173
- query_params[:mode] = options[:test_mode].is_a?(TrueClass) ? 'test' : 'live' unless options[:test_mode].nil?
176
+ query_params[:flags] = stringify_params(options[:flags])
174
177
 
175
- if options[:test_mode_compatibility_level]
176
- query_params[:test_mode_compatibility_level] =
177
- options[:test_mode_compatibility_level]
178
- query_params[:mode] = 'test'
179
- end
178
+ mode = determine_mode(options[:test_mode], options[:mode])
180
179
 
180
+ unless options[:test_mode_compatibility_level].nil?
181
+ query_params[:test_mode_compatibility_level] = options[:test_mode_compatibility_level]
182
+ mode = 'test'
183
+ end
184
+ query_params[:mode] = mode unless mode.nil?
181
185
  query_params
182
186
  end
183
187
 
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.2.0
4
+ version: 3.3.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: 2022-05-03 00:00:00.000000000 Z
11
+ date: 2022-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler