rspotify 0.11.0 → 1.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
  SHA1:
3
- metadata.gz: 1d036fdc4740624c2fbb316884229f8e36910b12
4
- data.tar.gz: 10ac95732e3035d833761359d7315bc6ee262fbf
3
+ metadata.gz: 2d7f28937ceecdffd0ec686f189c65158444bb57
4
+ data.tar.gz: 77e683c22e6504e964dcb72b52ff96b0fd9afe0f
5
5
  SHA512:
6
- metadata.gz: 81c76cb23d53a4fa2f5fac2a08533301656e3c54c07df43c9339bef21b25384e2bec3770a7672db67eb076a53a0c3319f46ea14be6cd79fe000702cda798c61a
7
- data.tar.gz: bf69b0fa697e50e686c57beb3c4375e53684219ddbd9ad87086ff8c5b8bf36bfca961602bf2e03ce3e1acd240866f01e40e7325e134ee3815af5d3b188cb2e30
6
+ metadata.gz: d07731312cd48df3a44650a99043cc47be1280830d118f63150930a53fe1f6ee7b9f0c4ab9912ceabb7f3eb25df673d863f56e45e1a729f0065f695dbd6dd364
7
+ data.tar.gz: e952d461b1b57602732edc183c3d8ae09f1382f0fac8e007fa6a10fdb31250e46cae6f32c09aded69d932bdcb363ca51485f5b41c1da3b49797d794c2583910b
data/README.md CHANGED
@@ -111,9 +111,17 @@ You'll may want your application to access an user's Spotify account.
111
111
 
112
112
  For instance, suppose you want your app to create playlists for the user based on his taste, or to add a feature that syncs user's playlists with some external app.
113
113
 
114
- If so, just add the following to `config/initializers/omniauth.rb` (Remember to [get your credentials](https://developer.spotify.com/my-applications))
114
+ If so, add the following lines to your application (Remember to [get your credentials](https://developer.spotify.com/my-applications))
115
115
 
116
116
  ```ruby
117
+ # config/application.rb
118
+
119
+ RSpotify::authenticate("<your_client_id>", "<your_client_secret>")
120
+ ```
121
+
122
+ ```ruby
123
+ # config/initializers/omniauth.rb
124
+
117
125
  Rails.application.config.middleware.use OmniAuth::Builder do
118
126
  provider :spotify, "<your_client_id>", "<your_client_secret>", scope: 'user-read-email playlist-modify'
119
127
  end
@@ -121,7 +129,7 @@ end
121
129
 
122
130
  You should replace the scope values for the ones your own app will require from the user. You can see the list of available scopes in [here](https://developer.spotify.com/web-api/using-scopes).
123
131
 
124
- Then just make a link so the user can log in with his Spotify account:
132
+ Next, make a link so the user can log in with his Spotify account:
125
133
 
126
134
  ```ruby
127
135
  <%= link_to 'Sign in with Spotify', '/auth/spotify' %>
@@ -160,7 +168,20 @@ class UsersController < ApplicationController
160
168
  end
161
169
  ```
162
170
 
163
- **Note**: You might also like to add `RSpotify::authenticate("<your_client_id>", "<your_client_secret>")` to your `config/application.rb`. This will allow extra calls to be made.
171
+ 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.
172
+
173
+ RSpotify provides a way to easy persistance:
174
+
175
+ ```ruby
176
+ hash = spotify_user.to_hash
177
+ # hash containing all user attributes, including access tokens
178
+
179
+ # Use the hash to persist the data the way you prefer...
180
+
181
+ # Then recover the Spotify user whenever you like
182
+ spotify_user = RSpotify::User.new(hash)
183
+ spotify_user.create_playlist!('my_awesome_playlist') # automatically refreshes token
184
+ ```
164
185
 
165
186
  ## Notes
166
187
 
@@ -34,8 +34,8 @@ module RSpotify
34
34
  # Returns array of Album objects matching the query, ordered by popularity
35
35
  #
36
36
  # @param query [String] The search query's keywords. See the q description in {https://developer.spotify.com/web-api/search-item here} for details.
37
- # @param limit [Integer] Maximum number of albums to return. Minimum: 1. Maximum: 50.
38
- # @param offset [Integer] The index of the first album to return. Use with limit to get the next set of albums.
37
+ # @param limit [Integer] Maximum number of albums to return. Minimum: 1. Maximum: 50. Default: 20.
38
+ # @param offset [Integer] The index of the first album to return. Use with limit to get the next set of albums. Default: 0.
39
39
  # @return [Array<Album>]
40
40
  #
41
41
  # @example
@@ -44,10 +44,10 @@ module RSpotify
44
44
  # albums.first.class #=> RSpotify::Album
45
45
  # albums.first.name #=> "AM"
46
46
  #
47
- # albums = RSpotify::Base.search('AM', 10)
47
+ # albums = RSpotify::Base.search('AM', limit: 10)
48
48
  # albums.size #=> 10
49
- def self.search(query, limit = 20, offset = 0)
50
- super(query, 'album', limit, offset)
49
+ def self.search(query, limit: 20, offset: 0)
50
+ super(query, 'album', limit: limit, offset: offset)
51
51
  end
52
52
 
53
53
  def initialize(options = {})
@@ -27,8 +27,8 @@ module RSpotify
27
27
  # Returns array of Artist objects matching the query, ordered by popularity
28
28
  #
29
29
  # @param query [String] The search query's keywords. See the q description in {https://developer.spotify.com/web-api/search-item here} for details.
30
- # @param limit [Integer] Maximum number of artists to return. Minimum: 1. Maximum: 50.
31
- # @param offset [Integer] The index of the first artist to return. Use with limit to get the next set of artists.
30
+ # @param limit [Integer] Maximum number of artists to return. Minimum: 1. Maximum: 50. Default: 20.
31
+ # @param offset [Integer] The index of the first artist to return. Use with limit to get the next set of artists. Default: 0.
32
32
  # @return [Array<Artist>]
33
33
  #
34
34
  # @example
@@ -37,10 +37,10 @@ module RSpotify
37
37
  # artists.first.class #=> RSpotify::Artist
38
38
  # artists.first.name #=> "Arctic Monkeys"
39
39
  #
40
- # artists = RSpotify::Artist.search('Arctic', 10)
40
+ # artists = RSpotify::Artist.search('Arctic', limit: 10)
41
41
  # artists.size #=> 10
42
- def self.search(query, limit = 20, offset = 0)
43
- super(query, 'artist', limit, offset)
42
+ def self.search(query, limit: 20, offset: 0)
43
+ super(query, 'artist', limit: limit, offset: offset)
44
44
  end
45
45
 
46
46
  def initialize(options = {})
data/lib/rspotify/base.rb CHANGED
@@ -63,8 +63,8 @@ module RSpotify
63
63
  #
64
64
  # @param query [String] The search query's keywords. See the q description in {https://developer.spotify.com/web-api/search-item here} for details.
65
65
  # @param type [String] Valid types are: album, artist and track. Separate multiple types with commas.
66
- # @param limit [Integer] Maximum number of objects to return. Minimum: 1. Maximum: 50.
67
- # @param offset [Integer] The index of the first object to return. Use with limit to get the next set of objects.
66
+ # @param limit [Integer] Maximum number of objects to return. Minimum: 1. Maximum: 50. Default: 20.
67
+ # @param offset [Integer] The index of the first object to return. Use with limit to get the next set of objects. Default: 0.
68
68
  # @return [Array<Base>]
69
69
  #
70
70
  # @example
@@ -73,9 +73,9 @@ module RSpotify
73
73
  # artists.first.class #=> RSpotify::Artist
74
74
  # artists.first.name #=> "Arctic Monkeys"
75
75
  #
76
- # albums = RSpotify::Base.search('AM', 'album', 10)
76
+ # albums = RSpotify::Base.search('AM', 'album', limit: 10)
77
77
  # albums.size #=> 10
78
- def self.search(query, types, limit = 20, offset = 0)
78
+ def self.search(query, types, limit: 20, offset: 0)
79
79
  if limit < 1 || limit > 50
80
80
  warn 'Limit must be between 1 and 50'
81
81
  return false
@@ -77,7 +77,7 @@ module RSpotify
77
77
  url = "users/#{@owner.id}/playlists/#{@id}/tracks?uris=#{track_uris}"
78
78
  url << "&position=#{position}" if position
79
79
 
80
- RSpotify.post(url, {}, User.send(:oauth_header, @owner.id))
80
+ User.oauth_post(@owner.id, url, {})
81
81
  @tracks = nil
82
82
  end
83
83
 
@@ -97,7 +97,7 @@ module RSpotify
97
97
  credentials = (credentials_defined ? User.class_variable_get('@@users_credentials') : nil)
98
98
 
99
99
  if credentials && credentials[@owner.id]
100
- initialize RSpotify.get(url, User.send(:oauth_header, @owner.id))
100
+ initialize User.oauth_get(@owner.id, url)
101
101
  else
102
102
  initialize RSpotify.auth_get(url)
103
103
  end
@@ -34,8 +34,8 @@ module RSpotify
34
34
  # Returns array of Track objects matching the query, ordered by popularity
35
35
  #
36
36
  # @param query [String] The search query's keywords. See the q description in {https://developer.spotify.com/web-api/search-item here} for details.
37
- # @param limit [Integer] Maximum number of tracks to return. Minimum: 1. Maximum: 50.
38
- # @param offset [Integer] The index of the first track to return. Use with limit to get the next set of tracks.
37
+ # @param limit [Integer] Maximum number of tracks to return. Minimum: 1. Maximum: 50. Default: 20.
38
+ # @param offset [Integer] The index of the first track to return. Use with limit to get the next set of tracks. Default: 0.
39
39
  # @return [Array<Track>]
40
40
  #
41
41
  # @example
@@ -44,10 +44,10 @@ module RSpotify
44
44
  # tracks.first.class #=> RSpotify::Track
45
45
  # tracks.first.name #=> "Thriller"
46
46
  #
47
- # tracks = RSpotify::Track.search('Thriller', 10)
47
+ # tracks = RSpotify::Track.search('Thriller', limit: 10)
48
48
  # tracks.size #=> 10
49
- def self.search(query, limit = 20, offset = 0)
50
- super(query, 'track', limit, offset)
49
+ def self.search(query, limit: 20, offset: 0)
50
+ super(query, 'track', limit: limit, offset: offset)
51
51
  end
52
52
 
53
53
  def initialize(options = {})
data/lib/rspotify/user.rb CHANGED
@@ -27,14 +27,43 @@ module RSpotify
27
27
  false
28
28
  end
29
29
 
30
+ def self.refresh_token(user_id)
31
+ request_body = {
32
+ grant_type: 'refresh_token',
33
+ refresh_token: @@users_credentials[user_id]['refresh_token']
34
+ }
35
+ response = RestClient.post(TOKEN_URI, request_body, RSpotify.send(:auth_header))
36
+ json = JSON.parse(response)
37
+ @@users_credentials[user_id]['token'] = json['access_token']
38
+ end
39
+ private_class_method :refresh_token
40
+
30
41
  def self.oauth_header(user_id)
31
- {
42
+ {
32
43
  'Authorization' => "Bearer #{@@users_credentials[user_id]['token']}",
33
44
  'Content-Type' => 'application/json'
34
45
  }
35
46
  end
36
47
  private_class_method :oauth_header
37
48
 
49
+ def self.oauth_send(user_id, verb, path, *params)
50
+ RSpotify.send(verb, path, *params)
51
+ rescue RestClient::Unauthorized => e
52
+ raise e if e.response !~ /access token expired/
53
+ refresh_token(user_id)
54
+ params[-1] = oauth_header(user_id)
55
+ RSpotify.send(verb, path, *params)
56
+ end
57
+ private_class_method :oauth_header
58
+
59
+ # User::oauth_{get,post}
60
+ RSpotify::VERBS.each do |verb|
61
+ define_singleton_method "oauth_#{verb}" do |user_id, path, *params|
62
+ params << oauth_header(user_id)
63
+ oauth_send(user_id, verb, path, *params)
64
+ end
65
+ end
66
+
38
67
  def initialize(options = {})
39
68
  credentials = options['credentials']
40
69
  options = options['info'] if options['info']
@@ -72,7 +101,7 @@ module RSpotify
72
101
  def create_playlist!(name, public: true)
73
102
  url = "users/#{@id}/playlists"
74
103
  request_data = %Q({"name":"#{name}", "public":#{public}})
75
- Playlist.new RSpotify.post(url, request_data, User.send(:oauth_header, @id))
104
+ Playlist.new User.oauth_post(@id, url, request_data)
76
105
  end
77
106
 
78
107
  # Returns all playlists from user
@@ -1,3 +1,3 @@
1
1
  module RSpotify
2
- VERSION = '0.11.0'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -66,5 +66,19 @@ describe RSpotify::Album do
66
66
  expect(albums.first) .to be_an RSpotify::Album
67
67
  expect(albums.map(&:name)) .to include('AM', 'Am I Wrong', 'A.M.', 'Melody AM')
68
68
  end
69
+
70
+ it 'should accept additional options' do
71
+ albums = RSpotify::Album.search('AM', limit: 10)
72
+ expect(albums.size) .to eq 10
73
+ expect(albums.map(&:name)) .to include('AM', 'Am I Wrong')
74
+
75
+ albums = RSpotify::Album.search('AM', offset: 10)
76
+ expect(albums.size) .to eq 20
77
+ expect(albums.map(&:name)) .to include('Melody AM', 'I Am...')
78
+
79
+ albums = RSpotify::Album.search('AM', limit: 10, offset: 10)
80
+ expect(albums.size) .to eq 10
81
+ expect(albums.map(&:name)) .to include('Melody AM')
82
+ end
69
83
  end
70
84
  end
@@ -59,7 +59,21 @@ describe RSpotify::Artist do
59
59
  expect(artists) .to be_an Array
60
60
  expect(artists.size) .to eq 20
61
61
  expect(artists.first) .to be_an RSpotify::Artist
62
- expect(artists.map(&:name)) .to include('Arctic Monkeys', 'Arctic Moon', 'Arctic', 'Arctic Quest')
62
+ expect(artists.map(&:name)) .to include('Arctic Monkeys', 'Arctic', 'Arctic Warbler', 'Arctic Express')
63
+ end
64
+
65
+ it 'should accept additional options' do
66
+ artists = RSpotify::Artist.search('Arctic', limit: 10)
67
+ expect(artists.size) .to eq 10
68
+ expect(artists.map(&:name)) .to include('Arctic Monkeys', 'Arctic')
69
+
70
+ artists = RSpotify::Artist.search('Arctic', offset: 10)
71
+ expect(artists.size) .to eq 20
72
+ expect(artists.map(&:name)) .to include('Arctic Flame', 'James Arctic')
73
+
74
+ artists = RSpotify::Artist.search('Arctic', limit: 10, offset: 10)
75
+ expect(artists.size) .to eq 10
76
+ expect(artists.map(&:name)) .to include('Arctic Flame')
63
77
  end
64
78
  end
65
79
  end
@@ -63,7 +63,21 @@ describe RSpotify::Track do
63
63
  expect(tracks) .to be_an Array
64
64
  expect(tracks.size) .to eq 20
65
65
  expect(tracks.first) .to be_an RSpotify::Track
66
- expect(tracks.map(&:name)) .to include('Do I Wanna Know?', 'Wanna Know', 'Never Wanna Know')
66
+ expect(tracks.map(&:name)) .to include('Do I Wanna Know?', 'I Wanna Know', 'Never Wanna Know')
67
+ end
68
+
69
+ it 'should accept additional options' do
70
+ tracks = RSpotify::Track.search('Wanna Know', limit: 10)
71
+ expect(tracks.size) .to eq 10
72
+ expect(tracks.map(&:name)) .to include('Do I Wanna Know?', 'I Wanna Know')
73
+
74
+ tracks = RSpotify::Track.search('Wanna Know', offset: 10)
75
+ expect(tracks.size) .to eq 20
76
+ expect(tracks.map(&:name)) .to include('They Wanna Know', 'Say I Wanna Know')
77
+
78
+ tracks = RSpotify::Track.search('Wanna Know', limit: 10, offset: 10)
79
+ expect(tracks.size) .to eq 10
80
+ expect(tracks.map(&:name)) .to include('They Wanna Know')
67
81
  end
68
82
  end
69
83
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspotify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guilherme Sad