smartcar 1.0.8 → 2.0.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/Gemfile.lock +1 -1
- data/lib/smartcar/oauth.rb +52 -28
- data/lib/smartcar/utils.rb +9 -0
- data/lib/smartcar/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b57225d3763fbbe7c577b89697ea80af48c4de44dcb39efdbb3a4811bfcfc27d
|
4
|
+
data.tar.gz: 5b7915fd5b5686909ddc01a63725287ed1771efa53b48b4c5c7327a6079449e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f792bda809f5a3be1e89f775877f3ce11495b2416c8ec3d0767e8e0ed744333203a0defeb95beca33ba6f0f42d93bcaf7fd40cd6e6dc86ebcdf5fbb066487efa
|
7
|
+
data.tar.gz: cd9e6ac196300057dc0e704aab9a75bb65c0475273cf60b62872c92a07d7d7975f316e5e7c7bdab3424c866af1eac710492db538cd94b29e72dfd3678fcad73f
|
data/Gemfile.lock
CHANGED
data/lib/smartcar/oauth.rb
CHANGED
@@ -12,10 +12,25 @@ module Smartcar
|
|
12
12
|
# @option options[:client_id] [String] - Client ID, if not passed fallsback to ENV['CLIENT_ID']
|
13
13
|
# @option options[:client_secret] [String] - Client Secret, if not passed fallsback to ENV['CLIENT_SECRET']
|
14
14
|
# @option options[:redirect_uri] [String] - Redirect URI, if not passed fallsback to ENV['REDIRECT_URI']
|
15
|
+
# @option options[:scope] [Array of Strings] - array of scopes that specify what the user can access
|
16
|
+
# EXAMPLE : ['read_odometer', 'read_vehicle_info', 'required:read_location']
|
17
|
+
# For further details refer to https://smartcar.com/docs/guides/scope/
|
18
|
+
# @option options[:test_mode] [Boolean] - Setting this to 'true' runs it in test mode.
|
19
|
+
#
|
20
|
+
# @return [Smartcar::Oauth] Returns a Smartcar::Oauth Object that has other methods
|
21
|
+
def initialize(options)
|
22
|
+
@redirect_uri = options[:redirect_uri] || get_config('REDIRECT_URI')
|
23
|
+
@client_id = options[:client_id] || get_config('CLIENT_ID')
|
24
|
+
@client_secret = options[:client_secret] || get_config('CLIENT_SECRET')
|
25
|
+
@scope = options[:scope]
|
26
|
+
@test_mode = !!options[:test_mode]
|
27
|
+
end
|
28
|
+
|
29
|
+
# Generate the OAuth authorization URL.
|
30
|
+
# @param options [Hash]
|
15
31
|
# @option options[:state] [String] - OAuth state parameter passed to the
|
16
32
|
# redirect uri. This parameter may be used for identifying the user who
|
17
33
|
# initiated the request.
|
18
|
-
# @option options[:test_mode] [Boolean] - Setting this to 'true' runs it in test mode.
|
19
34
|
# @option options[:force_prompt] [Boolean] - Setting `force_prompt` to
|
20
35
|
# `true` will show the permissions approval screen on every authentication
|
21
36
|
# attempt, even if the user has previously consented to the exact scope of
|
@@ -24,37 +39,39 @@ module Smartcar
|
|
24
39
|
# users to bypass the car brand selection screen.
|
25
40
|
# For a complete list of supported makes, please see our
|
26
41
|
# [API Reference](https://smartcar.com/docs/api#authorization) documentation.
|
27
|
-
# @option options[:
|
28
|
-
#
|
29
|
-
#
|
42
|
+
# @option options[:single_select] [Boolean, Hash] - An optional value that sets the
|
43
|
+
# behavior of the grant dialog displayed to the user. If set to `true`,
|
44
|
+
# `single_select` limits the user to selecting only one vehicle. If `single_select`
|
45
|
+
# is an hash with the property `vin`, Smartcar will only authorize the vehicle
|
46
|
+
# with the specified VIN. See the
|
47
|
+
# [Single Select guide](https://smartcar.com/docs/guides/single-select/)
|
48
|
+
# for more information.
|
30
49
|
# @option options[:flags] [Array of Strings] - an optional array of early access features to enable.
|
31
50
|
#
|
32
|
-
# @return [
|
33
|
-
def
|
34
|
-
|
35
|
-
|
36
|
-
@client_secret = options[:client_secret] || get_config('CLIENT_SECRET')
|
37
|
-
|
38
|
-
@auth_parameters = {
|
51
|
+
# @return [String] Authorization URL string
|
52
|
+
def authorization_url(options = {})
|
53
|
+
options[:scope] = @scope
|
54
|
+
auth_parameters = {
|
39
55
|
redirect_uri: @redirect_uri,
|
40
56
|
approval_prompt: options[:force_prompt] ? FORCE : AUTO,
|
41
|
-
mode:
|
42
|
-
response_type: CODE
|
57
|
+
mode: @test_mode ? TEST : LIVE,
|
58
|
+
response_type: CODE,
|
43
59
|
}
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
60
|
+
|
61
|
+
auth_parameters[:flags] = options[:flags].join(' ') unless options[:flags].nil?
|
62
|
+
auth_parameters[:scope] = @scope.join(' ') unless @scope.nil?
|
63
|
+
|
48
64
|
%I(state make).each do |parameter|
|
49
|
-
|
65
|
+
auth_parameters[parameter] = options[parameter] unless options[parameter].nil?
|
50
66
|
end
|
51
|
-
end
|
52
67
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
75
|
end
|
59
76
|
|
60
77
|
# Generates the tokens hash using the code returned in oauth process.
|
@@ -71,16 +88,23 @@ module Smartcar
|
|
71
88
|
end
|
72
89
|
|
73
90
|
# Refreshing the access token
|
74
|
-
# @param
|
75
|
-
# get_token method
|
91
|
+
# @param refresh_token [String] refresh_token received during token exchange
|
76
92
|
#
|
77
93
|
# @return [Hash] Hash of token, refresh token, expiry info and token type
|
78
|
-
def refresh_token
|
79
|
-
token_object = OAuth2::AccessToken.from_hash(client,
|
94
|
+
def exchange_refresh_token(refresh_token)
|
95
|
+
token_object = OAuth2::AccessToken.from_hash(client, {refresh_token: refresh_token})
|
80
96
|
token_object = token_object.refresh!
|
81
97
|
token_object.to_hash
|
82
98
|
end
|
83
99
|
|
100
|
+
# Checks if token is expired using Oauth2 classes
|
101
|
+
# @param expires_at [Number] expires_at as time since epoch
|
102
|
+
#
|
103
|
+
# @return [Boolean]
|
104
|
+
def expired?(expires_at)
|
105
|
+
OAuth2::AccessToken.from_hash(client, {expires_at: expires_at}).expired?
|
106
|
+
end
|
107
|
+
|
84
108
|
private
|
85
109
|
# gets the Oauth Client object
|
86
110
|
#
|
data/lib/smartcar/utils.rb
CHANGED
@@ -10,6 +10,15 @@
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
# Utility method to return a hash of the isntance variables
|
14
|
+
#
|
15
|
+
# @return [Hash] hash of all instance variables
|
16
|
+
def to_hash
|
17
|
+
instance_variables.each_with_object({}) do |attribute, hash|
|
18
|
+
hash[attribute.to_s.delete("@").to_sym] = instance_variable_get(attribute)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
13
22
|
# gets a given env variable, checks for existence and throws exception if not present
|
14
23
|
# @param config_name [String] key of the env variable
|
15
24
|
#
|
data/lib/smartcar/version.rb
CHANGED