rspotify 0.8.0 → 0.9.0
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 +13 -2
- data/lib/rspotify.rb +4 -4
- data/lib/rspotify/playlist.rb +24 -1
- data/lib/rspotify/user.rb +18 -8
- data/lib/rspotify/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b8f239c93e7f8445a6ab7963b82551dae8d2fa5
|
4
|
+
data.tar.gz: 0201d9d38995ffcd8b7d218402e5ef047898b890
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 318e529c2896ad7563d85c64b74833df356d2987a78fabb1f207f9ba7eb8ecd8f2a94d78797f1eb316c46964a48c110d7a35723c4db8a633e90c0fd29133f3b7
|
7
|
+
data.tar.gz: 0433cdf3d380e6d945ba3b3ee7357111a127b2d2d1f2d043f9f498e9ca14646f065116994e6823491b1c5cdcd7e3738418615c8668f240714155af45c165ed8c
|
data/README.md
CHANGED
@@ -145,14 +145,25 @@ class UsersController < ApplicationController
|
|
145
145
|
spotify_user = RSpotify::User.new(request.env['omniauth.auth'])
|
146
146
|
# Now you can create playlists for the user and much more!
|
147
147
|
|
148
|
-
spotify_user.create_playlist!('my-awesome-playlist')
|
149
|
-
# Directly creates playlist in user's Spotify account
|
148
|
+
playlist = spotify_user.create_playlist!('my-awesome-playlist')
|
149
|
+
# Directly creates playlist in user's Spotify account and returns it
|
150
|
+
|
151
|
+
tracks = RSpotify::Track.search('Know')
|
152
|
+
|
153
|
+
playlist.add_tracks!(tracks)
|
154
|
+
# user's Spotify account now has a playlist with songs containing "Know" in the name
|
155
|
+
|
156
|
+
playlist.tracks.first.name #=> "Somebody That I Used To Know"
|
150
157
|
end
|
151
158
|
end
|
152
159
|
```
|
153
160
|
|
154
161
|
**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.
|
155
162
|
|
163
|
+
## Notes
|
164
|
+
|
165
|
+
If you'd like to use OAuth outside rails, have a look [here](https://developer.spotify.com/web-api/authorization-guide/#authorization_code_flow) for the requests that need to be made. You should be able to pass the response to RSpotify::User.new just as well, and from there easily create playlists and more for your user.
|
166
|
+
|
156
167
|
## Contributing
|
157
168
|
|
158
169
|
1. Fork it ( https://github.com/guilhermesad/rspotify/fork )
|
data/lib/rspotify.rb
CHANGED
@@ -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)
|
11
11
|
|
12
12
|
autoload :Album, 'rspotify/album'
|
13
13
|
autoload :Artist, 'rspotify/artist'
|
@@ -21,7 +21,7 @@ module RSpotify
|
|
21
21
|
authorization = Base64.strict_encode64 "#{client_id}:#{client_secret}"
|
22
22
|
headers = { 'Authorization' => "Basic #{authorization}" }
|
23
23
|
response = RestClient.post(TOKEN_URI, request_body, headers)
|
24
|
-
@
|
24
|
+
@client_token = JSON.parse(response)['access_token']
|
25
25
|
true
|
26
26
|
end
|
27
27
|
|
@@ -29,11 +29,11 @@ module RSpotify
|
|
29
29
|
define_singleton_method verb do |path, *params|
|
30
30
|
url = API_URI + path
|
31
31
|
response = RestClient.send(verb, url, *params)
|
32
|
-
JSON.parse response
|
32
|
+
JSON.parse response unless response.empty?
|
33
33
|
end
|
34
34
|
|
35
35
|
define_singleton_method "auth_#{verb}" do |path, *params|
|
36
|
-
auth_header = { 'Authorization' => "Bearer #{@
|
36
|
+
auth_header = { 'Authorization' => "Bearer #{@client_token}" }
|
37
37
|
params << auth_header
|
38
38
|
send(verb, path, *params)
|
39
39
|
end
|
data/lib/rspotify/playlist.rb
CHANGED
@@ -50,6 +50,21 @@ module RSpotify
|
|
50
50
|
super(options)
|
51
51
|
end
|
52
52
|
|
53
|
+
#TODO doc
|
54
|
+
def add_tracks!(tracks, position: nil)
|
55
|
+
if tracks.size > 100
|
56
|
+
warn 'Too many tracks requested. Maximum: 100'
|
57
|
+
return false
|
58
|
+
end
|
59
|
+
|
60
|
+
track_uris = tracks.map(&:uri).join(',')
|
61
|
+
url = "users/#{@owner.id}/playlists/#{@id}/tracks?uris=#{track_uris}"
|
62
|
+
url << "&position=#{position}" if position
|
63
|
+
|
64
|
+
RSpotify.post(url, {}, User.send(:oauth_headers, @owner.id))
|
65
|
+
@tracks = nil
|
66
|
+
end
|
67
|
+
|
53
68
|
# When an object is obtained undirectly, Spotify usually returns a simplified version of it.
|
54
69
|
# This method updates it into a full object, with all attributes filled.
|
55
70
|
#
|
@@ -61,7 +76,15 @@ module RSpotify
|
|
61
76
|
# playlist.complete!
|
62
77
|
# playlist.instance_variable_get("@description") #=> "Iconic soundtracks..."
|
63
78
|
def complete!
|
64
|
-
|
79
|
+
url = "users/#{@owner.id}/playlists/#{@id}"
|
80
|
+
credentials_defined = User.class_variable_defined?('@@users_credentials')
|
81
|
+
credentials = (credentials_defined ? User.class_variable_get('@@users_credentials') : nil)
|
82
|
+
|
83
|
+
if credentials && credentials[@owner.id]
|
84
|
+
initialize RSpotify.get(url, User.send(:oauth_headers, @owner.id))
|
85
|
+
else
|
86
|
+
initialize RSpotify.auth_get(url)
|
87
|
+
end
|
65
88
|
end
|
66
89
|
end
|
67
90
|
end
|
data/lib/rspotify/user.rb
CHANGED
@@ -21,9 +21,17 @@ module RSpotify
|
|
21
21
|
false
|
22
22
|
end
|
23
23
|
|
24
|
+
def self.oauth_headers(user_id)
|
25
|
+
{
|
26
|
+
'Authorization' => "Bearer #{@@users_credentials[user_id]['token']}",
|
27
|
+
'Content-Type' => 'application/json'
|
28
|
+
}
|
29
|
+
end
|
30
|
+
private_class_method :oauth_headers
|
31
|
+
|
24
32
|
def initialize(options = {})
|
25
|
-
|
26
|
-
options
|
33
|
+
credentials = options['credentials']
|
34
|
+
options = options['info'] if options['info']
|
27
35
|
|
28
36
|
@country ||= options['country']
|
29
37
|
@display_name ||= options['display_name']
|
@@ -32,16 +40,18 @@ module RSpotify
|
|
32
40
|
@product ||= options['product']
|
33
41
|
|
34
42
|
super(options)
|
43
|
+
|
44
|
+
if credentials
|
45
|
+
@@users_credentials ||= {}
|
46
|
+
@@users_credentials[@id] = credentials
|
47
|
+
end
|
35
48
|
end
|
36
49
|
|
50
|
+
#TODO doc
|
37
51
|
def create_playlist!(name, public: true)
|
38
|
-
|
39
|
-
'Authorization' => "Bearer #{@credentials['token']}",
|
40
|
-
'Content-Type' => 'application/json'
|
41
|
-
}
|
52
|
+
url = "users/#{@id}/playlists"
|
42
53
|
request_data = %Q({"name":"#{name}", "public":#{public}})
|
43
|
-
|
44
|
-
RSpotify.post("users/#{@id}/playlists", request_data, headers)
|
54
|
+
Playlist.new RSpotify.post(url, request_data, User.send(:oauth_headers, @id))
|
45
55
|
end
|
46
56
|
|
47
57
|
# Returns all playlists from user
|
data/lib/rspotify/version.rb
CHANGED