smartcar 2.4.0 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +28 -0
- data/Gemfile +4 -2
- data/Gemfile.lock +22 -1
- data/Rakefile +11 -3
- data/bin/console +4 -3
- data/lib/smartcar.rb +36 -26
- data/lib/smartcar/base.rb +14 -12
- data/lib/smartcar/battery.rb +6 -4
- data/lib/smartcar/battery_capacity.rb +4 -2
- data/lib/smartcar/charge.rb +6 -4
- data/lib/smartcar/engine_oil.rb +5 -3
- data/lib/smartcar/fuel.rb +8 -6
- data/lib/smartcar/location.rb +5 -3
- data/lib/smartcar/oauth.rb +45 -37
- data/lib/smartcar/odometer.rb +4 -2
- data/lib/smartcar/permissions.rb +4 -2
- data/lib/smartcar/tire_pressure.rb +11 -10
- data/lib/smartcar/user.rb +7 -4
- data/lib/smartcar/utils.rb +14 -5
- data/lib/smartcar/vehicle.rb +19 -242
- data/lib/smartcar/vehicle_attributes.rb +7 -5
- data/lib/smartcar/vehicle_utils/actions.rb +61 -0
- data/lib/smartcar/vehicle_utils/batch.rb +83 -0
- data/lib/smartcar/vehicle_utils/data.rb +110 -0
- data/lib/smartcar/version.rb +3 -1
- data/lib/smartcar/vin.rb +4 -2
- data/ruby-sdk.gemspec +24 -21
- metadata +29 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3848d46ccf722b908e7b42acc1afebccc24fbf1362c36bd0b0a867cd8bdd5b85
|
4
|
+
data.tar.gz: 3cfbf110c26177478a4ffad5373aeffcb73411b85f014c7af98293ea42739601
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f46fc77b3eafb462cfbd210d7509293368d9d2306ce8d16dbfbd4aad7a76339e53b689f06c2e50b3cc6eb56be5a390c805f8ae88384f6cfcdfa7e1ce27b34e91
|
7
|
+
data.tar.gz: ee615279dc5c61e75ba1afd3ffa640b2df9ffafd4ea459cf22558d145e434b117814bd4279fcfd418643146eec4386911735f8f1066babd3ce494df44d15cfba
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# Prevent messages for libraries of rubocop
|
2
|
+
AllCops:
|
3
|
+
NewCops: enable
|
4
|
+
SuggestExtensions: false
|
5
|
+
|
6
|
+
# Disabling ABC size for now as refactoring few places seems pointless for now
|
7
|
+
Metrics/AbcSize:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Metrics/BlockLength:
|
11
|
+
Exclude:
|
12
|
+
- '**/*.gemspec'
|
13
|
+
- 'spec/**/*'
|
14
|
+
|
15
|
+
# Just ignoring test helper for headless auth.
|
16
|
+
Metrics/MethodLength:
|
17
|
+
Max: 20
|
18
|
+
Exclude:
|
19
|
+
- 'spec/smartcar/helpers/auth_helper.rb'
|
20
|
+
|
21
|
+
# Parameters in data from json API comes in as camelCase, ignoring those files to avoid snake_case enforcement
|
22
|
+
Naming/MethodName:
|
23
|
+
Exclude:
|
24
|
+
- 'lib/smartcar/battery.rb'
|
25
|
+
- 'lib/smartcar/charge.rb'
|
26
|
+
- 'lib/smartcar/engine_oil.rb'
|
27
|
+
- 'lib/smartcar/fuel.rb'
|
28
|
+
- 'lib/smartcar/tire_pressure.rb'
|
data/Gemfile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
6
|
|
5
7
|
# Specify your gem's dependencies in smartcar.gemspec
|
6
8
|
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
smartcar (2.
|
4
|
+
smartcar (2.5.0)
|
5
5
|
oauth2 (~> 1.4)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
+
ast (2.4.2)
|
10
11
|
byebug (11.1.3)
|
11
12
|
childprocess (3.0.0)
|
12
13
|
diff-lcs (1.3)
|
@@ -25,9 +26,15 @@ GEM
|
|
25
26
|
multi_json (~> 1.3)
|
26
27
|
multi_xml (~> 0.5)
|
27
28
|
rack (>= 1.2, < 3)
|
29
|
+
parallel (1.20.1)
|
30
|
+
parser (3.0.0.0)
|
31
|
+
ast (~> 2.4.1)
|
28
32
|
rack (2.2.3)
|
33
|
+
rainbow (3.0.0)
|
29
34
|
rake (12.3.3)
|
30
35
|
redcarpet (3.5.1)
|
36
|
+
regexp_parser (2.1.1)
|
37
|
+
rexml (3.2.5)
|
31
38
|
rspec (3.9.0)
|
32
39
|
rspec-core (~> 3.9.0)
|
33
40
|
rspec-expectations (~> 3.9.0)
|
@@ -41,11 +48,24 @@ GEM
|
|
41
48
|
diff-lcs (>= 1.2.0, < 2.0)
|
42
49
|
rspec-support (~> 3.9.0)
|
43
50
|
rspec-support (3.9.3)
|
51
|
+
rubocop (1.12.0)
|
52
|
+
parallel (~> 1.10)
|
53
|
+
parser (>= 3.0.0.0)
|
54
|
+
rainbow (>= 2.2.2, < 4.0)
|
55
|
+
regexp_parser (>= 1.8, < 3.0)
|
56
|
+
rexml
|
57
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
58
|
+
ruby-progressbar (~> 1.7)
|
59
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
60
|
+
rubocop-ast (1.4.1)
|
61
|
+
parser (>= 2.7.1.5)
|
62
|
+
ruby-progressbar (1.11.0)
|
44
63
|
ruby2_keywords (0.0.4)
|
45
64
|
rubyzip (2.3.0)
|
46
65
|
selenium-webdriver (3.142.7)
|
47
66
|
childprocess (>= 0.5, < 4.0)
|
48
67
|
rubyzip (>= 1.2.2)
|
68
|
+
unicode-display_width (2.0.0)
|
49
69
|
|
50
70
|
PLATFORMS
|
51
71
|
ruby
|
@@ -56,6 +76,7 @@ DEPENDENCIES
|
|
56
76
|
rake (~> 12.3, >= 12.3.3)
|
57
77
|
redcarpet (~> 3.5.0)
|
58
78
|
rspec (~> 3.0)
|
79
|
+
rubocop (~> 1.12)
|
59
80
|
selenium-webdriver (~> 3.142)
|
60
81
|
smartcar!
|
61
82
|
|
data/Rakefile
CHANGED
@@ -1,6 +1,14 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
3
5
|
|
4
6
|
RSpec::Core::RakeTask.new(:spec)
|
5
7
|
|
6
|
-
|
8
|
+
require 'rubocop/rake_task'
|
9
|
+
|
10
|
+
RuboCop::RakeTask.new(:rubocop) do |t|
|
11
|
+
t.options = ['--display-cop-names']
|
12
|
+
end
|
13
|
+
|
14
|
+
task default: %i[rubocop spec]
|
data/bin/console
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require
|
4
|
-
require
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'smartcar'
|
5
6
|
|
6
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
8
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +11,5 @@ require "smartcar"
|
|
10
11
|
# require "pry"
|
11
12
|
# Pry.start
|
12
13
|
|
13
|
-
require
|
14
|
+
require 'irb'
|
14
15
|
IRB.start(__FILE__)
|
data/lib/smartcar.rb
CHANGED
@@ -1,59 +1,68 @@
|
|
1
|
-
|
2
|
-
require "smartcar/version"
|
3
|
-
require "smartcar/base"
|
4
|
-
require "smartcar/oauth"
|
5
|
-
require "smartcar/permissions"
|
6
|
-
require "smartcar/battery"
|
7
|
-
require "smartcar/battery_capacity"
|
8
|
-
require "smartcar/charge"
|
9
|
-
require "smartcar/engine_oil"
|
10
|
-
require "smartcar/fuel"
|
11
|
-
require "smartcar/location"
|
12
|
-
require "smartcar/odometer"
|
13
|
-
require "smartcar/tire_pressure"
|
14
|
-
require "smartcar/vin"
|
15
|
-
require "smartcar/vehicle_attributes"
|
16
|
-
require "smartcar/vehicle"
|
17
|
-
require "smartcar/user"
|
1
|
+
# frozen_string_literal: true
|
18
2
|
|
3
|
+
require 'smartcar/utils'
|
4
|
+
require 'smartcar/version'
|
5
|
+
require 'smartcar/base'
|
6
|
+
require 'smartcar/oauth'
|
7
|
+
require 'smartcar/permissions'
|
8
|
+
require 'smartcar/battery'
|
9
|
+
require 'smartcar/battery_capacity'
|
10
|
+
require 'smartcar/charge'
|
11
|
+
require 'smartcar/engine_oil'
|
12
|
+
require 'smartcar/fuel'
|
13
|
+
require 'smartcar/location'
|
14
|
+
require 'smartcar/odometer'
|
15
|
+
require 'smartcar/tire_pressure'
|
16
|
+
require 'smartcar/vin'
|
17
|
+
require 'smartcar/vehicle_attributes'
|
18
|
+
require 'smartcar/vehicle_utils/batch'
|
19
|
+
require 'smartcar/vehicle_utils/data'
|
20
|
+
require 'smartcar/vehicle_utils/actions'
|
21
|
+
require 'smartcar/vehicle'
|
22
|
+
require 'smartcar/user'
|
19
23
|
|
20
24
|
# Main Smartcar umbrella module
|
21
25
|
module Smartcar
|
22
26
|
# Error raised when a config is not found
|
23
27
|
class ConfigNotFound < StandardError; end
|
28
|
+
|
24
29
|
# Error raised when Smartcar returns non 400, 404, 401, 200 or 204 response
|
25
30
|
class ExternalServiceError < StandardError; end
|
31
|
+
|
26
32
|
# Error raised when Smartcar returns 404
|
27
33
|
class ServiceUnavailableError < ExternalServiceError; end
|
34
|
+
|
28
35
|
# Error raised when Smartcar returns Authentication Error with status 401
|
29
36
|
class AuthenticationError < ExternalServiceError; end
|
37
|
+
|
30
38
|
# Error raised when Smartcar returns 400 response
|
31
39
|
class BadRequestError < ExternalServiceError; end
|
32
40
|
|
33
41
|
# Host to connect to smartcar
|
34
|
-
SITE =
|
42
|
+
SITE = 'https://api.smartcar.com/'
|
35
43
|
|
36
44
|
# Path for smartcar oauth
|
37
|
-
OAUTH_PATH =
|
38
|
-
%w
|
45
|
+
OAUTH_PATH = 'https://connect.smartcar.com/oauth/authorize'
|
46
|
+
%w[success code test live force auto metric imperial].each do |constant|
|
39
47
|
# Constant to represent the value
|
40
48
|
const_set(constant.upcase, constant.freeze)
|
41
49
|
end
|
42
50
|
|
43
51
|
# Lock value sent in request body
|
44
|
-
LOCK =
|
52
|
+
LOCK = 'LOCK'
|
45
53
|
# Unlock value sent in request body
|
46
|
-
UNLOCK =
|
54
|
+
UNLOCK = 'UNLOCK'
|
47
55
|
# Start charge value sent in request body
|
48
|
-
START_CHARGE =
|
56
|
+
START_CHARGE = 'START'
|
49
57
|
# Stop charge value sent in request body
|
50
|
-
STOP_CHARGE =
|
58
|
+
STOP_CHARGE = 'STOP'
|
51
59
|
# Constant for units
|
52
|
-
UNITS = [IMPERIAL,METRIC]
|
60
|
+
UNITS = [IMPERIAL, METRIC].freeze
|
53
61
|
|
54
62
|
# Smartcar API version variable - defaulted to 1.0
|
55
|
-
@api_version =
|
63
|
+
@api_version = '1.0'
|
56
64
|
|
65
|
+
# rubocop:disable Naming/AccessorMethodName
|
57
66
|
# Module method Used to set api version to be used.
|
58
67
|
# This method can be used at the top to set the version and any
|
59
68
|
# following request will use the version set here unless overridden
|
@@ -70,4 +79,5 @@ module Smartcar
|
|
70
79
|
def self.get_api_version
|
71
80
|
instance_variable_get('@api_version')
|
72
81
|
end
|
82
|
+
# rubocop:enable Naming/AccessorMethodName
|
73
83
|
end
|
data/lib/smartcar/base.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'oauth2'
|
2
4
|
require 'base64'
|
3
5
|
module Smartcar
|
@@ -9,28 +11,28 @@ module Smartcar
|
|
9
11
|
# Error raised when an invalid parameter is passed.
|
10
12
|
class InvalidParameterValue < StandardError; end
|
11
13
|
# Constant for Bearer auth type
|
12
|
-
BEARER = 'BEARER'
|
14
|
+
BEARER = 'BEARER'
|
13
15
|
# Constant for Basic auth type
|
14
|
-
BASIC = 'BASIC'
|
16
|
+
BASIC = 'BASIC'
|
15
17
|
# Number of seconds to wait for response
|
16
18
|
REQUEST_TIMEOUT = 310
|
17
19
|
|
18
20
|
attr_accessor :token, :error, :meta, :unit_system, :version
|
19
21
|
|
20
|
-
%i
|
22
|
+
%i[get post patch put delete].each do |verb|
|
21
23
|
# meta programming and define all Restful methods.
|
22
24
|
# @param path [String] the path to hit for the request.
|
23
25
|
# @param data [Hash] request body if needed.
|
24
26
|
#
|
25
27
|
# @return [Hash] The response Json parsed as a hash.
|
26
|
-
define_method verb do |path, data=nil|
|
28
|
+
define_method verb do |path, data = nil|
|
27
29
|
response = service.send(verb) do |request|
|
28
30
|
request.headers['Authorization'] = "BEARER #{token}"
|
29
|
-
request.headers['Authorization'] = "BASIC #{
|
31
|
+
request.headers['Authorization'] = "BASIC #{generate_basic_auth}" if data && data[:auth] == BASIC
|
30
32
|
request.headers['sc-unit-system'] = unit_system if unit_system
|
31
|
-
request.headers['Content-Type'] =
|
33
|
+
request.headers['Content-Type'] = 'application/json'
|
32
34
|
complete_path = "/v#{version}#{path}"
|
33
|
-
if verb
|
35
|
+
if verb == :get
|
34
36
|
request.url complete_path, data
|
35
37
|
else
|
36
38
|
request.url complete_path
|
@@ -39,6 +41,7 @@ module Smartcar
|
|
39
41
|
end
|
40
42
|
error = get_error(response)
|
41
43
|
raise error if error
|
44
|
+
|
42
45
|
[JSON.parse(response.body), response.headers]
|
43
46
|
end
|
44
47
|
end
|
@@ -49,10 +52,9 @@ module Smartcar
|
|
49
52
|
# @param auth [String] type of auth
|
50
53
|
#
|
51
54
|
# @return [Object]
|
52
|
-
def fetch(path
|
53
|
-
|
54
|
-
|
55
|
-
get(_path, {auth: auth})
|
55
|
+
def fetch(path:, options: {}, auth: 'BEARER')
|
56
|
+
path += "?#{URI.encode_www_form(options)}" unless options.empty?
|
57
|
+
get(path, { auth: auth })
|
56
58
|
end
|
57
59
|
|
58
60
|
private
|
@@ -60,7 +62,7 @@ module Smartcar
|
|
60
62
|
# returns auth token for BASIC auth
|
61
63
|
#
|
62
64
|
# @return [String] Base64 encoding of CLIENT:SECRET
|
63
|
-
def
|
65
|
+
def generate_basic_auth
|
64
66
|
Base64.strict_encode64("#{get_config('CLIENT_ID')}:#{get_config('CLIENT_SECRET')}")
|
65
67
|
end
|
66
68
|
|
data/lib/smartcar/battery.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Smartcar
|
2
4
|
# class to represent Battery info
|
3
|
-
|
4
|
-
|
5
|
+
# @attr [Number] percentRemaining Decimal value representing the remaining charge percent.
|
6
|
+
# @attr [Number] range Remaining range of the vehicle.
|
5
7
|
class Battery < Base
|
6
8
|
# Path Proc for hitting battery end point
|
7
|
-
PATH =
|
9
|
+
PATH = proc { |id| "/vehicles/#{id}/battery" }
|
8
10
|
attr_reader :percentRemaining, :range
|
9
11
|
|
10
12
|
# just to have Ruby-esque method names
|
11
|
-
|
13
|
+
alias percentage_remaining percentRemaining
|
12
14
|
end
|
13
15
|
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Smartcar
|
2
4
|
# class to represent Battery Capacity info
|
3
|
-
|
5
|
+
# @attr [Number] capacity Decimal value representing the battery's total capacity in kWh.
|
4
6
|
class BatteryCapacity < Base
|
5
7
|
# Path Proc for hitting battery capacity end point
|
6
|
-
PATH =
|
8
|
+
PATH = proc { |id| "/vehicles/#{id}/battery/capacity" }
|
7
9
|
attr_reader :capacity
|
8
10
|
end
|
9
11
|
end
|
data/lib/smartcar/charge.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Smartcar
|
2
4
|
# class to represent Charge info
|
3
|
-
|
4
|
-
|
5
|
+
# @attr [Boolean] isPluggedIn Specifies if the vehicle is plugged in.
|
6
|
+
# @attr [String] state Charging state of the vehicle.
|
5
7
|
class Charge < Base
|
6
8
|
# Path Proc for hitting charge end point
|
7
|
-
PATH =
|
9
|
+
PATH = proc { |id| "/vehicles/#{id}/charge" }
|
8
10
|
attr_reader :isPluggedIn, :state
|
9
11
|
|
10
12
|
# just to have Ruby-esque method names
|
11
|
-
|
13
|
+
alias is_plugged_in? isPluggedIn
|
12
14
|
end
|
13
15
|
end
|
data/lib/smartcar/engine_oil.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Smartcar
|
2
4
|
# class to represent Engine oil info
|
3
|
-
|
5
|
+
# @attr [Number] lifeRemaining Remaining life of the engine oil
|
4
6
|
class EngineOil < Base
|
5
7
|
# Path Proc for hitting engine oil end point
|
6
|
-
PATH =
|
8
|
+
PATH = proc { |id| "/vehicles/#{id}/engine/oil" }
|
7
9
|
attr_reader :lifeRemaining
|
8
10
|
|
9
11
|
# just to have Ruby-esque method names
|
10
|
-
|
12
|
+
alias life_remaining lifeRemaining
|
11
13
|
end
|
12
14
|
end
|
data/lib/smartcar/fuel.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Smartcar
|
2
4
|
# class to represent Fuel info
|
3
|
-
|
4
|
-
|
5
|
-
|
5
|
+
# @attr [Number] amountRemaining Amount of fuel remaining.
|
6
|
+
# @attr [Number] percentageRemaining Decimal value representing the remaining fuel percent.
|
7
|
+
# @attr [Number] range Remaining range of the vehicle.
|
6
8
|
class Fuel < Base
|
7
9
|
# Path Proc for hitting fuel end point
|
8
|
-
PATH =
|
10
|
+
PATH = proc { |id| "/vehicles/#{id}/fuel" }
|
9
11
|
attr_reader :amountRemaining, :percentRemaining, :range
|
10
12
|
|
11
13
|
# just to have Ruby-esque method names
|
12
|
-
|
13
|
-
|
14
|
+
alias amount_remaining amountRemaining
|
15
|
+
alias percent_remaining percentRemaining
|
14
16
|
end
|
15
17
|
end
|
data/lib/smartcar/location.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Smartcar
|
2
4
|
# class to represent Location info
|
3
|
-
|
4
|
-
|
5
|
+
# @attr [Number] latitude Latitude of last recorded location.
|
6
|
+
# @attr [Number] longitude Longitude of last recorded location.
|
5
7
|
class Location < Base
|
6
8
|
# Path Proc for hitting location end point
|
7
|
-
PATH =
|
9
|
+
PATH = proc { |id| "/vehicles/#{id}/location" }
|
8
10
|
attr_reader :latitude, :longitude
|
9
11
|
end
|
10
12
|
end
|
data/lib/smartcar/oauth.rb
CHANGED
@@ -1,8 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Smartcar
|
2
4
|
# Oauth class to take care of the Oauth 2.0 with Smartcar APIs
|
3
5
|
#
|
4
6
|
class Oauth < Base
|
5
7
|
extend Smartcar::Utils
|
8
|
+
|
9
|
+
attr_reader :redirect_uri, :client_id, :client_secret, :scope, :mode
|
10
|
+
|
6
11
|
# By default users are not shown the permission dialog if they have already
|
7
12
|
# approved the set of scopes for this application. The application can elect
|
8
13
|
# to always display the permissions dialog to the user by setting
|
@@ -19,11 +24,11 @@ module Smartcar
|
|
19
24
|
#
|
20
25
|
# @return [Smartcar::Oauth] Returns a Smartcar::Oauth Object that has other methods
|
21
26
|
def initialize(options)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
+
options[:redirect_uri] ||= get_config('REDIRECT_URI')
|
28
|
+
options[:client_id] ||= get_config('CLIENT_ID')
|
29
|
+
options[:client_secret] ||= get_config('CLIENT_SECRET')
|
30
|
+
options[:mode] = options[:test_mode].nil? || !options[:test_mode] ? LIVE : TEST
|
31
|
+
super
|
27
32
|
end
|
28
33
|
|
29
34
|
# Generate the OAuth authorization URL.
|
@@ -50,28 +55,9 @@ module Smartcar
|
|
50
55
|
#
|
51
56
|
# @return [String] Authorization URL string
|
52
57
|
def authorization_url(options = {})
|
53
|
-
options
|
54
|
-
|
55
|
-
|
56
|
-
approval_prompt: options[:force_prompt] ? FORCE : AUTO,
|
57
|
-
mode: @test_mode ? TEST : LIVE,
|
58
|
-
response_type: CODE,
|
59
|
-
}
|
60
|
-
|
61
|
-
auth_parameters[:flags] = options[:flags].join(' ') unless options[:flags].nil?
|
62
|
-
auth_parameters[:scope] = @scope.join(' ') unless @scope.nil?
|
63
|
-
|
64
|
-
%I(state make).each do |parameter|
|
65
|
-
auth_parameters[parameter] = options[parameter] unless options[parameter].nil?
|
66
|
-
end
|
67
|
-
|
68
|
-
if(options[:single_select].is_a?(Hash))
|
69
|
-
auth_parameters[:single_select_vin] = options[:single_select][:vin]
|
70
|
-
auth_parameters[:single_select] = true
|
71
|
-
else
|
72
|
-
auth_parameters[:single_select] = !!options[:single_select]
|
73
|
-
end
|
74
|
-
client.auth_code.authorize_url(auth_parameters)
|
58
|
+
initialize_auth_parameters(options)
|
59
|
+
add_single_select_options(options[:single_select])
|
60
|
+
client.auth_code.authorize_url(@auth_parameters)
|
75
61
|
end
|
76
62
|
|
77
63
|
# Generates the tokens hash using the code returned in oauth process.
|
@@ -81,10 +67,10 @@ module Smartcar
|
|
81
67
|
# @return [Hash] Hash of token, refresh token, expiry info and token type
|
82
68
|
def get_token(auth_code)
|
83
69
|
client.auth_code
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
70
|
+
.get_token(
|
71
|
+
auth_code,
|
72
|
+
redirect_uri: redirect_uri
|
73
|
+
).to_hash
|
88
74
|
end
|
89
75
|
|
90
76
|
# Refreshing the access token
|
@@ -92,7 +78,7 @@ module Smartcar
|
|
92
78
|
#
|
93
79
|
# @return [Hash] Hash of token, refresh token, expiry info and token type
|
94
80
|
def exchange_refresh_token(refresh_token)
|
95
|
-
token_object = OAuth2::AccessToken.from_hash(client, {refresh_token: refresh_token})
|
81
|
+
token_object = OAuth2::AccessToken.from_hash(client, { refresh_token: refresh_token })
|
96
82
|
token_object = token_object.refresh!
|
97
83
|
token_object.to_hash
|
98
84
|
end
|
@@ -102,18 +88,40 @@ module Smartcar
|
|
102
88
|
#
|
103
89
|
# @return [Boolean]
|
104
90
|
def expired?(expires_at)
|
105
|
-
OAuth2::AccessToken.from_hash(client, {expires_at: expires_at}).expired?
|
91
|
+
OAuth2::AccessToken.from_hash(client, { expires_at: expires_at }).expired?
|
106
92
|
end
|
107
93
|
|
108
94
|
private
|
95
|
+
|
96
|
+
def initialize_auth_parameters(options)
|
97
|
+
@auth_parameters = {
|
98
|
+
response_type: CODE,
|
99
|
+
redirect_uri: redirect_uri,
|
100
|
+
mode: mode,
|
101
|
+
state: options[:state],
|
102
|
+
make: options[:make],
|
103
|
+
approval_prompt: options[:force_prompt] ? FORCE : AUTO,
|
104
|
+
flags: options[:flags]&.join(' '),
|
105
|
+
scope: scope&.join(' ')
|
106
|
+
}
|
107
|
+
end
|
108
|
+
|
109
|
+
def add_single_select_options(single_select)
|
110
|
+
if single_select.is_a?(Hash)
|
111
|
+
@auth_parameters[:single_select_vin] = single_select[:vin]
|
112
|
+
@auth_parameters[:single_select] = true
|
113
|
+
else
|
114
|
+
@auth_parameters[:single_select] = !single_select.nil?
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
109
118
|
# gets the Oauth Client object
|
110
119
|
#
|
111
120
|
# @return [OAuth2::Client] A Oauth Client object.
|
112
121
|
def client
|
113
|
-
@client ||= OAuth2::Client.new(
|
114
|
-
|
115
|
-
|
116
|
-
)
|
122
|
+
@client ||= OAuth2::Client.new(client_id,
|
123
|
+
client_secret,
|
124
|
+
site: OAUTH_PATH)
|
117
125
|
end
|
118
126
|
end
|
119
127
|
end
|