rspotify 1.1.0 → 1.2.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: a915d527294d8772bf9f21e87660fce4241a9beb
4
- data.tar.gz: 494e4a78f576a65780077bbd98e2a11aeb610c78
3
+ metadata.gz: 3fd61ef8b246df135f26baba22ee01c8c8b95aea
4
+ data.tar.gz: 2e9b132363b9250147ef8e1f94dd6c5a1ed4b79f
5
5
  SHA512:
6
- metadata.gz: 7e604afc1b2830bf9bb3e07b5a9e7e042bb184d61bf2476efee94759c2e3f5fc2163b1cd4c7830343f3bb90d91e48256e92c26573c80ac62b5f34f27f466e7b8
7
- data.tar.gz: e7fa3c1792e6f9dbc456758f5677ba9c6b2bdc4795631cd07c7694c2dadeba0a31675719b8a8812f2f019d532d86c300556d770a4b24fc510d2998e52276b2a1
6
+ metadata.gz: c30d2c943bd75df12a5215cb1f459bbef43298c740513f0757a0b0e49475047ef6e5758c47f5a98a25972e1f6ce6afb10cac8d43d6ab807c6e66bc08e9d2bad6
7
+ data.tar.gz: ec69eab471a3dbd4149f48bc2d319c229a3333671467f74bae73cb13b2db3c4367a2bd0d1fe676e1c7a630e3c45a61e29aad5df24e3133112c6bcc62e22d940f
data/README.md CHANGED
@@ -123,7 +123,7 @@ RSpotify::authenticate("<your_client_id>", "<your_client_secret>")
123
123
  # config/initializers/omniauth.rb
124
124
 
125
125
  Rails.application.config.middleware.use OmniAuth::Builder do
126
- provider :spotify, "<your_client_id>", "<your_client_secret>", scope: 'user-read-email playlist-modify-public'
126
+ provider :spotify, "<your_client_id>", "<your_client_secret>", scope: 'user-read-email playlist-modify-public user-library-read user-library-modify'
127
127
  end
128
128
  ```
129
129
 
@@ -153,7 +153,7 @@ class UsersController < ApplicationController
153
153
  spotify_user = RSpotify::User.new(request.env['omniauth.auth'])
154
154
  # Now you can access user's private data, create playlists and much more
155
155
 
156
- # Access private data (Check doc for all attributes)
156
+ # Access private data
157
157
  spotify_user.country #=> "US"
158
158
  spotify_user.email #=> "example@email.com"
159
159
 
@@ -164,6 +164,13 @@ class UsersController < ApplicationController
164
164
  tracks = RSpotify::Track.search('Know')
165
165
  playlist.add_tracks!(tracks)
166
166
  playlist.tracks.first.name #=> "Somebody That I Used To Know"
167
+
168
+ # Access and modify user's music library
169
+ spotify_user.save_tracks!(tracks)
170
+ spotify_user.saved_tracks.size #=> 20
171
+ spotify_user.remove_tracks!(tracks)
172
+
173
+ # Check doc for more
167
174
  end
168
175
  end
169
176
  ```
@@ -68,7 +68,7 @@ module RSpotify
68
68
  @albums = json['items'].map { |a| Album.new a }
69
69
  end
70
70
 
71
- # Returns array of similar artists. Similarity is based on analysis of the Spotify community’s {http://news.spotify.com/se/2010/02/03/related-artists listening history}.
71
+ # Returns array of similar artists. Similarity is based on analysis of the Spotify community’s {http://news.spotify.com/se/2010/02/03/related-artists listening history}
72
72
  #
73
73
  # @return [Array<Artist>]
74
74
  #
@@ -7,7 +7,7 @@ module RSpotify
7
7
  API_URI = 'https://api.spotify.com/v1/'
8
8
  AUTHORIZE_URI = 'https://accounts.spotify.com/authorize'
9
9
  TOKEN_URI = 'https://accounts.spotify.com/api/token'
10
- VERBS = %w(get post)
10
+ VERBS = %w(get post put delete)
11
11
 
12
12
  def self.auth_header
13
13
  authorization = Base64.strict_encode64 "#{@client_id}:#{@client_secret}"
@@ -15,7 +15,7 @@ module RSpotify
15
15
  end
16
16
  private_class_method :auth_header
17
17
 
18
- # Authenticates access to restricted data. Requires {https://developer.spotify.com/my-applications user credentials}.
18
+ # Authenticates access to restricted data. Requires {https://developer.spotify.com/my-applications user credentials}
19
19
  #
20
20
  # @param client_id [String]
21
21
  # @param client_secret [String]
@@ -34,14 +34,12 @@ module RSpotify
34
34
  end
35
35
 
36
36
  VERBS.each do |verb|
37
- # RSpotify::{get,post}
38
37
  define_singleton_method verb do |path, *params|
39
38
  url = API_URI + path
40
39
  response = RestClient.send(verb, url, *params)
41
40
  JSON.parse response unless response.empty?
42
41
  end
43
42
 
44
- # RSpotify::auth_{get,post}
45
43
  define_singleton_method "auth_#{verb}" do |path, *params|
46
44
  auth_header = { 'Authorization' => "Bearer #{@client_token}" }
47
45
  params << auth_header
@@ -55,7 +55,7 @@ module RSpotify
55
55
  #
56
56
  # @param tracks [Array<Track>] Tracks to be added. Maximum: 100 per request
57
57
  # @param position [Integer, NilClass] The position to insert the tracks, a zero-based index. Default: tracks are appended to the playlist
58
- # @return [NilClass]
58
+ # @return [Array<Track>] The tracks added
59
59
  #
60
60
  # @example
61
61
  # tracks = RSpotify::Track.search('Know', 30)
@@ -79,6 +79,7 @@ module RSpotify
79
79
 
80
80
  User.oauth_post(@owner.id, url, {})
81
81
  @tracks = nil
82
+ tracks
82
83
  end
83
84
 
84
85
  # When an object is obtained undirectly, Spotify usually returns a simplified version of it.
data/lib/rspotify/user.rb CHANGED
@@ -56,7 +56,6 @@ module RSpotify
56
56
  end
57
57
  private_class_method :oauth_header
58
58
 
59
- # User::oauth_{get,post}
60
59
  RSpotify::VERBS.each do |verb|
61
60
  define_singleton_method "oauth_#{verb}" do |user_id, path, *params|
62
61
  params << oauth_header(user_id)
@@ -114,8 +113,75 @@ module RSpotify
114
113
  # playlists.first.class #=> RSpotify::Playlist
115
114
  # playlists.first.name #=> "Movie Soundtrack Masterpieces"
116
115
  def playlists
117
- playlists = RSpotify.auth_get("users/#{@id}/playlists")['items']
118
- playlists.map { |p| Playlist.new p }
116
+ json = RSpotify.auth_get("users/#{@id}/playlists")
117
+ json['items'].map { |p| Playlist.new p }
118
+ end
119
+
120
+ # Remove tracks from the user’s “Your Music” library.
121
+ #
122
+ # @param tracks [Array<Track>] The tracks to remove. Maximum: 50.
123
+ # @return [Array<Track>] The tracks removed.
124
+ #
125
+ # @example
126
+ # tracks = user.saved_tracks
127
+ #
128
+ # user.saved_tracks.size #=> 20
129
+ # user.remove_tracks!(tracks)
130
+ # user.saved_tracks.size #=> 0
131
+ def remove_tracks!(tracks)
132
+ tracks_ids = tracks.map(&:id)
133
+ url = "me/tracks?ids=#{tracks_ids.join ','}"
134
+ User.oauth_delete(@id, url)
135
+ tracks
136
+ end
137
+
138
+ # Save tracks to the user’s “Your Music” library.
139
+ #
140
+ # @param tracks [Array<Track>] The tracks to save. Maximum: 100.
141
+ # @return [Array<Track>] The tracks saved.
142
+ #
143
+ # @example
144
+ # tracks = RSpotify::Track.search('Know')
145
+ #
146
+ # user.saved_tracks.size #=> 0
147
+ # user.save_tracks!(tracks)
148
+ # user.saved_tracks.size #=> 20
149
+ def save_tracks!(tracks)
150
+ tracks_ids = tracks.map(&:id)
151
+ url = "me/tracks"
152
+ request_body = tracks_ids.inspect
153
+ User.oauth_put(@id, url, request_body)
154
+ tracks
155
+ end
156
+
157
+ # Returns the tracks saved in the Spotify user’s “Your Music” library
158
+ #
159
+ # @param limit [Integer] Maximum number of tracks to return. Minimum: 1. Maximum: 50. Default: 20.
160
+ # @param offset [Integer] The index of the first track to return. Use with limit to get the next set of tracks. Default: 0.
161
+ # @return [Array<Track>]
162
+ #
163
+ # @example
164
+ # tracks = user.saved_tracks
165
+ # tracks.size #=> 20
166
+ # tracks.first.name #=> "Do I Wanna Know?"
167
+ def saved_tracks(limit: 20, offset: 0)
168
+ url = "me/tracks?limit=#{limit}&offset=#{offset}"
169
+ json = User.oauth_get(@id, url)
170
+ json['items'].map { |t| Track.new t['track'] }
171
+ end
172
+
173
+ # Check if tracks are already saved in the Spotify user’s “Your Music” library
174
+ #
175
+ # @param tracks [Array<Track>] The tracks to check. Maximum: 50.
176
+ # @return [Array<Boolean>] Array of booleans, in the same order in which the tracks were specified.
177
+ #
178
+ # @example
179
+ # tracks = RSpotify::Track.search('Know')
180
+ # user.saved_tracks?(tracks) #=> [true, false, true...]
181
+ def saved_tracks?(tracks)
182
+ tracks_ids = tracks.map(&:id)
183
+ url = "me/tracks/contains?ids=#{tracks_ids.join ','}"
184
+ User.oauth_get(@id, url)
119
185
  end
120
186
 
121
187
  # Returns a hash containing all user attributes
@@ -1,3 +1,3 @@
1
1
  module RSpotify
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
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: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guilherme Sad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-26 00:00:00.000000000 Z
11
+ date: 2014-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: omniauth-oauth2