smartcar 1.0.8 → 2.0.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: a3f5f753c2493733b16502aa5d2cbd3957ed073a1caf9dbcd032241febbe75b7
4
- data.tar.gz: 2f639abde820c99a2ea91a80518dd95ffba94dbfc1915dd5ddb53578189fdb5e
3
+ metadata.gz: b57225d3763fbbe7c577b89697ea80af48c4de44dcb39efdbb3a4811bfcfc27d
4
+ data.tar.gz: 5b7915fd5b5686909ddc01a63725287ed1771efa53b48b4c5c7327a6079449e1
5
5
  SHA512:
6
- metadata.gz: 2abac7e475acd840ba72e98367857abed3ea53c1df739179cbaf1a9c6751542bdfc3871cd8f1fc908a428fa603839f68ec0a82c13f0bf9f985f462ee3bf32fa5
7
- data.tar.gz: e7352fd756b8c969eb50450e6fa7024e4196e86511b9630afe1db0ba46cdf8d6ca71d342402b2e891bfcfe0ec6d88ba70a1f17dcf7ac3481ea169d12106ad5cc
6
+ metadata.gz: f792bda809f5a3be1e89f775877f3ce11495b2416c8ec3d0767e8e0ed744333203a0defeb95beca33ba6f0f42d93bcaf7fd40cd6e6dc86ebcdf5fbb066487efa
7
+ data.tar.gz: cd9e6ac196300057dc0e704aab9a75bb65c0475273cf60b62872c92a07d7d7975f316e5e7c7bdab3424c866af1eac710492db538cd94b29e72dfd3678fcad73f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smartcar (1.0.8)
4
+ smartcar (2.0.0)
5
5
  oauth2 (~> 1.4)
6
6
 
7
7
  GEM
@@ -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[:scope] [Array of Strings] - array of scopes that specify what the user can access
28
- # EXAMPLE : ['read_odometer', 'read_vehicle_info', 'required:read_location']
29
- # For further details refer to https://smartcar.com/docs/guides/scope/
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 [Smartcar::Oauth] Returns a Smartcar::Oauth Object that has other methods
33
- def initialize(options)
34
- @redirect_uri = options[:redirect_uri] || get_config('REDIRECT_URI')
35
- @client_id = options[:client_id] || get_config('CLIENT_ID')
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: options[:test_mode] ? TEST : LIVE,
42
- response_type: CODE
57
+ mode: @test_mode ? TEST : LIVE,
58
+ response_type: CODE,
43
59
  }
44
-
45
- %I(scope flags).each do |parameter|
46
- @auth_parameters[parameter] = options[parameter].join(' ') unless options[parameter].nil?
47
- end
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
- @auth_parameters[parameter] = options[parameter] unless options[parameter].nil?
65
+ auth_parameters[parameter] = options[parameter] unless options[parameter].nil?
50
66
  end
51
- end
52
67
 
53
- # Generate the OAuth authorization URL.
54
- #
55
- # @return [String] Authorization URL string
56
- def authorization_url
57
- client.auth_code.authorize_url(@auth_parameters)
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 token_hash [Hash] This is the hash that is returned with the
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(token_hash)
79
- token_object = OAuth2::AccessToken.from_hash(client, token_hash)
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
  #
@@ -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
  #
@@ -1,4 +1,4 @@
1
1
  module Smartcar
2
2
  # Gem current version number
3
- VERSION = "1.0.8"
3
+ VERSION = "2.0.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartcar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ashwin Subramanian