rspotify 2.9.2 → 2.10.3

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: c165c5081e4cca893c47dc2cd8f4f14575ab64aee6afd554124c3b0b36f3d9b6
4
- data.tar.gz: 851456edf9def81e75378588216daf83145afbc4aa5ea53d6f3efdb75bff3d73
3
+ metadata.gz: 95783cbdc5ca066a1454219ff9afd451ee730efa2f65547c3694b7e06ae71893
4
+ data.tar.gz: dfb2ba9eee5180070d7fb000ab1c832a5ff6f8adea379f6598fca967f96da53a
5
5
  SHA512:
6
- metadata.gz: f060beaba6b8c8995b23952d503703943dd89d765f4c23224dc7a4a05d34849f30a4ab551aea64c0f9bbd193014d524dd593f99cb5a9099a4c71084ecce8e3b0
7
- data.tar.gz: 3b877fc32c3ec4caf9aa31c72e7b4541e4b0bd432469e74ec6b33765eea1c90fa005bfc478dd70152b0da9260b98699c756541397fa1686d9bdc9d6f6de77607
6
+ metadata.gz: ec01dac8afbd2c6411b31c9df4fd8eac38d6ce5fa468f9e7bb49a8297d64bf9b852220de763258c5d3af0f3e0341166279a116d7667d274cf5b45914f2fe03bb
7
+ data.tar.gz: '09b20a9b1d6becae29c27050213aa55f49408e44575f7669875a0a3a28ca9659e09488459ec9b39466255479d4143959f92ae3071cccf5c54e5c98d643b77b6f'
data/README.md CHANGED
@@ -217,7 +217,40 @@ class UsersController < ApplicationController
217
217
  end
218
218
  ```
219
219
 
220
- The user's access token is automatically refreshed by RSpotify when needed. This is specially useful if you persist the user data on a database: this way he only needs to log in to Spotify once in his entire use of your application.
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:
@@ -18,6 +18,11 @@ module OmniAuth
18
18
  def raw_info
19
19
  @raw_info ||= access_token.get('me').parsed
20
20
  end
21
+
22
+ # Fix for: https://github.com/guilhermesad/rspotify/issues/189
23
+ def callback_url
24
+ full_host + script_name + callback_path
25
+ end
21
26
  end
22
27
  end
23
28
  end
@@ -73,6 +73,21 @@ module RSpotify
73
73
  User.oauth_put(@user.id, url, params.to_json)
74
74
  end
75
75
 
76
+ # Add an item to the end of the user’s current playback queue
77
+ # If `device_id` is not passed, the currently active spotify app will be triggered
78
+ #
79
+ # @param [String] device_id the ID of the device to set the repeat state on.
80
+ # @param [String] uri the spotify uri of the track to be queued
81
+ #
82
+ # @example
83
+ # player = user.player
84
+ # player.queue("spotify:track:4iV5W9uYEdYUVa79Axb7Rh")
85
+ def queue(device_id = nil, uri)
86
+ url = "me/player/queue?uri=#{uri}"
87
+ url = device_id.nil? ? url : "#{url}&device_id=#{device_id}"
88
+ User.oauth_post(@user.id, url, {})
89
+ end
90
+
76
91
  # Toggle the current user's player repeat status.
77
92
  # If `device_id` is not passed, the currently active spotify app will be triggered.
78
93
  # If `state` is not passed, the currently active context will be set to repeat.
@@ -315,7 +315,7 @@ module RSpotify
315
315
 
316
316
  params = {
317
317
  method: :delete,
318
- url: URI::encode(RSpotify::API_URI + @path + '/tracks'),
318
+ url: Addressable::URI.encode(RSpotify::API_URI + @path + '/tracks'),
319
319
  headers: User.send(:oauth_header, @owner.id),
320
320
  payload: positions ? { positions: positions } : { tracks: tracks }
321
321
  }
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
@@ -1,3 +1,3 @@
1
1
  module RSpotify
2
- VERSION = '2.9.2'.freeze
2
+ VERSION = '2.10.3'.freeze
3
3
  end
data/rspotify.gemspec CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.test_files = spec.files.grep(/^spec\//)
17
17
  spec.require_paths = ['lib']
18
18
 
19
- spec.add_dependency 'omniauth-oauth2', '~> 1.5.0'
19
+ spec.add_dependency 'omniauth-oauth2', '>= 1.6'
20
20
  spec.add_dependency 'rest-client', '~> 2.0.2'
21
21
  spec.add_dependency 'addressable', '~> 2.5.2'
22
22
 
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspotify
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.9.2
4
+ version: 2.10.3
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-09-23 00:00:00.000000000 Z
11
+ date: 2021-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: omniauth-oauth2
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.5.0
19
+ version: '1.6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 1.5.0
26
+ version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rest-client
29
29
  requirement: !ruby/object:Gem::Requirement