smartcar 3.3.0 → 3.5.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: 93454e55a42ab20160c545c9bda459e98c85fe5596519f704ffa001bcef5896f
4
- data.tar.gz: 0a8148c0f6678079467ee10d58b8564b2dec7e08127a3e7691e734503fc4128b
3
+ metadata.gz: c3ad3ad7082e2d27def7f0ee201f1b8f8b20eb25d3f17fffe71aecbb666dab87
4
+ data.tar.gz: ce9c4420338126a9cd6a22b2ae2da01f2ef9b469aaeb7df5a160377e54d939ce
5
5
  SHA512:
6
- metadata.gz: a5fb72f2ee3aab427c7eefd12628947eb3f5e8644f40f898acea27cdc1b6c47d7d1085b936b7661df699a31baa59e162efa7641cce7b18b823796cd6d392bc3b
7
- data.tar.gz: dc0ab7c95a537ea9a87c7e8f11ef13de8f8bd752625e0ec0418896de573a05d635837499e2744f53f7881a13c42cf9265e6799727bb68e424c4f57dccdd62370
6
+ metadata.gz: 61339cf13180062815aca1ed833d5761a0c3613595818526fc9f247e44a23d4e76f547f859c769a5bdd90917ef3e77f74048ad624806aefe8a060331b13eb2a7
7
+ data.tar.gz: 9974454e496f5e67cb7bf121e696fea11c0b1ab22583a521293fcb42e750cfc70073efb33e63f1c8c2b11f77ddb1afc23f56904b7a6de2e8f03348c62fe6f0df
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smartcar (3.3.0)
4
+ smartcar (3.5.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.2.0)
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.2)
26
+ faraday-net_http (2.1.0)
27
27
  hashdiff (1.0.1)
28
- jwt (2.3.0)
28
+ jwt (2.4.1)
29
29
  multi_json (1.15.0)
30
30
  multi_xml (0.6.0)
31
- oauth2 (1.4.9)
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
@@ -31,6 +31,7 @@ not have access to the dashboard, please
31
31
 
32
32
  - Create a new `AuthClient` object with your `client_id`, `client_secret`,
33
33
  `redirect_uri`.
34
+ -
34
35
  - Redirect the user to Smartcar Connect using `get_auth_url` with required `scope` or with one
35
36
  of our frontend SDKs.
36
37
  - The user will login, and then accept or deny your `scope`'s permissions.
@@ -76,6 +77,9 @@ Setup the environment variables for SMARTCAR_CLIENT_ID, SMARTCAR_CLIENT_SECRET a
76
77
  export SMARTCAR_CLIENT_ID=<client id>
77
78
  export SMARTCAR_CLIENT_SECRET=<client secret>
78
79
  export SMARTCAR_REDIRECT_URI=<redirect URI>
80
+ # Optional ENV variables
81
+ export SMARTCAR_CONNECT_ORIGIN=(default_value: connect.smartcar.com): Used as the host for the URL that starts the Connect/OAuth2 flow
82
+ export SMARTCAR_AUTH_ORIGIN=(default_value: auth.smartcar.com): Used as the host for the token exchange requests
79
83
  ```
80
84
 
81
85
  Example Usage for calling the reports API with oAuth token
@@ -6,7 +6,7 @@ module Smartcar
6
6
  class AuthClient
7
7
  include Smartcar::Utils
8
8
 
9
- attr_reader :redirect_uri, :client_id, :client_secret, :scope, :mode, :flags, :origin
9
+ attr_reader :redirect_uri, :client_id, :client_secret, :scope, :mode, :flags, :auth_origin, :connect_origin
10
10
 
11
11
  # Constructor for a client object
12
12
  #
@@ -23,7 +23,8 @@ module Smartcar
23
23
  options[:redirect_uri] ||= get_config('SMARTCAR_REDIRECT_URI')
24
24
  options[:client_id] ||= get_config('SMARTCAR_CLIENT_ID')
25
25
  options[:client_secret] ||= get_config('SMARTCAR_CLIENT_SECRET')
26
- options[:origin] = ENV['SMARTCAR_AUTH_ORIGIN'] || AUTH_ORIGIN
26
+ options[:auth_origin] = ENV['SMARTCAR_AUTH_ORIGIN'] || AUTH_ORIGIN
27
+ options[:connect_origin] = ENV['SMARTCAR_CONNECT_ORIGIN'] || CONNECT_ORIGIN
27
28
  options[:mode] = determine_mode(options[:test_mode], options[:mode]) || 'live'
28
29
  super
29
30
  end
@@ -57,7 +58,7 @@ module Smartcar
57
58
  def get_auth_url(scope, options = {})
58
59
  initialize_auth_parameters(scope, options)
59
60
  add_single_select_options(options[:single_select])
60
- client.auth_code.authorize_url(@auth_parameters)
61
+ connect_client.auth_code.authorize_url(@auth_parameters)
61
62
  end
62
63
 
63
64
  # Generates the tokens hash using the code returned in oauth process.
@@ -70,9 +71,9 @@ module Smartcar
70
71
  def exchange_code(code, options = {})
71
72
  set_token_url(options[:flags])
72
73
 
73
- token_hash = client.auth_code
74
- .get_token(code, redirect_uri: redirect_uri)
75
- .to_hash
74
+ token_hash = auth_client.auth_code
75
+ .get_token(code, redirect_uri: redirect_uri)
76
+ .to_hash
76
77
 
77
78
  json_to_ostruct(token_hash)
78
79
  rescue OAuth2::Error => e
@@ -88,7 +89,7 @@ module Smartcar
88
89
  def exchange_refresh_token(token, options = {})
89
90
  set_token_url(options[:flags])
90
91
 
91
- token_object = OAuth2::AccessToken.from_hash(client, { refresh_token: token })
92
+ token_object = OAuth2::AccessToken.from_hash(auth_client, { refresh_token: token })
92
93
  token_object = token_object.refresh!
93
94
 
94
95
  json_to_ostruct(token_object.to_hash)
@@ -101,7 +102,7 @@ module Smartcar
101
102
  #
102
103
  # @return [Boolean]
103
104
  def expired?(expires_at)
104
- OAuth2::AccessToken.from_hash(client, { expires_at: expires_at }).expired?
105
+ OAuth2::AccessToken.from_hash(auth_client, { expires_at: expires_at }).expired?
105
106
  end
106
107
 
107
108
  private
@@ -117,7 +118,7 @@ module Smartcar
117
118
  params[:flags] = build_flags(flags) if flags
118
119
  # Note - The inbuild interface to get the token does not allow any way to pass additional
119
120
  # URL params. Hence building the token URL with the flags and setting it in client.
120
- client.options[:token_url] = client.connection.build_url('/oauth/token', params).request_uri
121
+ auth_client.options[:token_url] = auth_client.connection.build_url('/oauth/token', params).request_uri
121
122
  end
122
123
 
123
124
  def initialize_auth_parameters(scope, options)
@@ -144,13 +145,22 @@ module Smartcar
144
145
  end
145
146
  end
146
147
 
147
- # gets the Oauth Client object
148
+ # gets the Oauth Client object configured with auth.connect.smartcar.com
148
149
  #
149
150
  # @return [OAuth2::Client] A Oauth Client object.
150
- def client
151
- @client ||= OAuth2::Client.new(client_id,
152
- client_secret,
153
- site: origin)
151
+ def auth_client
152
+ @auth_client ||= OAuth2::Client.new(client_id,
153
+ client_secret,
154
+ site: auth_origin)
155
+ end
156
+
157
+ # gets the Oauth Client object configured with connect.smartcar.com
158
+ #
159
+ # @return [OAuth2::Client] A Oauth Client object.
160
+ def connect_client
161
+ @connect_client ||= OAuth2::Client.new(client_id,
162
+ client_secret,
163
+ site: connect_origin)
154
164
  end
155
165
  end
156
166
  end
@@ -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']
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Smartcar
4
4
  # Gem current version number
5
- VERSION = '3.3.0'
5
+ VERSION = '3.5.0'
6
6
  end
data/lib/smartcar.rb CHANGED
@@ -22,7 +22,8 @@ module Smartcar
22
22
  }.freeze
23
23
 
24
24
  # Path for smartcar oauth
25
- AUTH_ORIGIN = 'https://connect.smartcar.com'
25
+ CONNECT_ORIGIN = 'https://connect.smartcar.com'
26
+ AUTH_ORIGIN = 'https://auth.smartcar.com'
26
27
  %w[success code test live force auto metric imperial].each do |constant|
27
28
  # Constant to represent the value
28
29
  const_set(constant.upcase, constant.freeze)
@@ -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.3.0
4
+ version: 3.5.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-08-03 00:00:00.000000000 Z
11
+ date: 2023-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -235,7 +235,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
235
235
  - !ruby/object:Gem::Version
236
236
  version: '0'
237
237
  requirements: []
238
- rubygems_version: 3.0.8
238
+ rubygems_version: 3.1.6
239
239
  signing_key:
240
240
  specification_version: 4
241
241
  summary: Ruby Gem to access smartcar APIs (https://smartcar.com/docs/)