rspotify 2.10.0 → 2.10.1
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/README.md +35 -1
- data/lib/rspotify/user.rb +10 -0
- data/lib/rspotify/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d19f6d23158c4ece9b8ede71d1b0ed2ed71598baad04895b62dc209f0147f556
|
4
|
+
data.tar.gz: 0c3b328c9b8f3ff4d87041c0d749fef09689bf922b4f7f4c9f5aa958a2d27587
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 636252d2d8d95caf3f8b07187b97fcefe22f6e6a052400d48ab200c2db47451934e37e7c4b25aa08bfddf1c0c1daf6b61fc7b0f2af19e54f25edcc0836da8731
|
7
|
+
data.tar.gz: fed825f00b36af6c3c9e2df6a6312b09219ea6d68b8136085b13ddcfa0764b747b1489a7afb1d73236a769f2fe1571617c49d709e199f7e58f1990a9bb4272e9
|
data/README.md
CHANGED
@@ -217,7 +217,40 @@ class UsersController < ApplicationController
|
|
217
217
|
end
|
218
218
|
```
|
219
219
|
|
220
|
-
|
220
|
+
## Refreshing the access token
|
221
|
+
|
222
|
+
The user's access token is automatically refreshed by RSpotify when needed. This is especially useful if you persist
|
223
|
+
the user data on a database. This way, the user only need log in to Spotify once during the use of the application.
|
224
|
+
|
225
|
+
Additionally, you can store a proc that is invoked when a new access token is generated. This give you the
|
226
|
+
opportunity to persist the new access token for future use. The proc will be invoked with two arguments: the
|
227
|
+
new access token and the lifetime of the token in seconds. For example, if lifetime value returned from
|
228
|
+
Spotify is 3600, you know that the token will be good for one hour.
|
229
|
+
|
230
|
+
In the sample code below, the credentials have been retrieved from some persistent store such as
|
231
|
+
AWS SecretsManager.
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
|
235
|
+
callback_proc = Proc.new { |new_access_token, token_lifetime |
|
236
|
+
now = Time.now.utc.to_i # seconds since 1/1/1970, midnight UTC
|
237
|
+
deadline = now+token_lifetime
|
238
|
+
#puts("new access token will expire at #{Time.at(deadline).utc.to_s}")
|
239
|
+
self.save_new_access_token(new_access_token)
|
240
|
+
}
|
241
|
+
|
242
|
+
spotify_user = RSpotify::User.new(
|
243
|
+
{
|
244
|
+
'credentials' => {
|
245
|
+
"token" => self.credentials["access_token"],
|
246
|
+
"refresh_token" => self.credentials["refresh_token"],
|
247
|
+
"access_refresh_callback" => callback_proc
|
248
|
+
} ,
|
249
|
+
'id' => self.credentials["user_id"]
|
250
|
+
})
|
251
|
+
|
252
|
+
|
253
|
+
```
|
221
254
|
|
222
255
|
RSpotify provides a way to facilitate persistence:
|
223
256
|
|
@@ -232,6 +265,7 @@ spotify_user = RSpotify::User.new(hash)
|
|
232
265
|
spotify_user.create_playlist!('my_awesome_playlist') # automatically refreshes token
|
233
266
|
```
|
234
267
|
|
268
|
+
|
235
269
|
## Getting raw response
|
236
270
|
|
237
271
|
To get the raw response from Spotify API requests, just toggle the `raw_response` variable:
|
data/lib/rspotify/user.rb
CHANGED
@@ -38,6 +38,16 @@ module RSpotify
|
|
38
38
|
response = RestClient.post(TOKEN_URI, request_body, RSpotify.send(:auth_header))
|
39
39
|
response = JSON.parse(response)
|
40
40
|
@@users_credentials[user_id]['token'] = response['access_token']
|
41
|
+
access_refresh_proc = @@users_credentials[user_id]['access_refresh_callback']
|
42
|
+
# If the access token expires and a new one is granted via the refresh
|
43
|
+
# token, then this proc will be called with two parameters:
|
44
|
+
# new_access_token and token_lifetime (in seconds)
|
45
|
+
# The purpose is to allow the calling environment to invoke some action,
|
46
|
+
# such as persisting the new access token somewhere, when the new token
|
47
|
+
# is generated.
|
48
|
+
if (access_refresh_proc.respond_to? :call)
|
49
|
+
access_refresh_proc.call(response['access_token'], response['expires_in'])
|
50
|
+
end
|
41
51
|
rescue RestClient::BadRequest => e
|
42
52
|
raise e if e.response !~ /Refresh token revoked/
|
43
53
|
end
|
data/lib/rspotify/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspotify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.10.
|
4
|
+
version: 2.10.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Guilherme Sad
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: omniauth-oauth2
|