rspotify 0.7.1 → 0.8.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: 4976cbb938eca04f4a36e3084052c4ed1fb0d119
4
- data.tar.gz: 0f6d2a0a7a6a5a73eda3d8e5a26ca4e6070c0de1
3
+ metadata.gz: 64b0804e1886e1ffa8bb5d5df377e8d25ddfeb50
4
+ data.tar.gz: 462a1211c106279de90490fa58ef1c7340122019
5
5
  SHA512:
6
- metadata.gz: fa34c1e3e875b2bf8d34a65d0f524473a5d28aaf7b997da6f30b2c7d0de4024bf965523cf23f9ff1368138042eaee4702fcc561012746b8f155687533f9ac3f4
7
- data.tar.gz: 7d3a74a7c1308fcabb2297f6968f9e6bebb87c88d4175305b971682f4116f8db37de6ad900676be3e61f79ad7db4ac4c665ab9a52cd156a7aab5842d67864283
6
+ metadata.gz: b1d02142eacd40743e3681e7b87858f4390738e12a2fc3383f48dcf977ca705f985ac101195de3411f6627a609f308b523e787b54e9010910fadd09e9ad9526c
7
+ data.tar.gz: 4819abd8af5a198e89c4fce6f781d33c2051b25817f8f2122d50976e31b1d44017ef026dd9099bf574d4dbea331a69bd448174e8304569ee30e8459ce8196883
data/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  This is a ruby wrapper for the [new Spotify Web API](https://developer.spotify.com/web-api), released in June 17, 2014.
4
4
 
5
+ ## Features
6
+
7
+ * [Full documentation](http://rdoc.info/github/guilhermesad/rspotify/master/frames)
8
+ * Full API Endpoint coverage
9
+ * OAuth and other authorization flows
10
+
5
11
  ## Installation
6
12
 
7
13
  Add this line to your application's Gemfile:
@@ -97,13 +103,55 @@ RSpotify focuses on objects behaviour so you can forget the API and worry about
97
103
 
98
104
  It is possible to write things like `playlist.tracks.sort_by(&:popularity).last.album` without having to think what API calls must be done. RSpotify fills the gaps for you.
99
105
 
100
- Full documentation can be found [here](http://rdoc.info/github/guilhermesad/rspotify/master/frames).
106
+ Check the [documentation](http://rdoc.info/github/guilhermesad/rspotify/master/frames) for all attributes and methods of albums, artists, etc.
107
+
108
+ ## Rails + OAuth
109
+
110
+ You'll may want your application to access an user's Spotify account.
111
+
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
+
114
+ If so, just add the following to `config/initializers/omniauth.rb` (Remember to [get your credentials](https://developer.spotify.com/my-applications))
115
+
116
+ ```ruby
117
+ Rails.application.config.middleware.use OmniAuth::Builder do
118
+ provider :spotify, "<your_client_id>", "<your_client_secret>", scope: 'user-read-email playlist-modify'
119
+ end
120
+ ```
121
+
122
+ 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
+
124
+ Then just make a link so the user can log in with his Spotify account:
101
125
 
102
- ## Notes
126
+ ```ruby
127
+ <%= link_to 'Sign in with Spotify', '/auth/spotify' %>
128
+ ```
129
+
130
+ And create a route to receive the callback:
103
131
 
104
- This gem uses [client credentials](https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow) to authenticate your access. This means you can get all public data you want, but it's not possible to access private playlists or to create new ones. For that you would want to use [authorization code flow](https://developer.spotify.com/web-api/authorization-guide/#authorization_code_flow).
132
+ ```ruby
133
+ # config/routes.rb
134
+
135
+ get '/auth/spotify/callback', to: 'users#spotify'
136
+ ```
137
+
138
+ Remember you need to tell Spotify this address is white-listed. You can do this by adding it to the Redirect URIs list in your [application page](https://developer.spotify.com/my-applications). An example of Redirect URI would be http://localhost:3000/auth/spotify/callback.
139
+
140
+ Finally, create a new RSpotify User with the token received:
141
+
142
+ ```ruby
143
+ class UsersController < ApplicationController
144
+ def spotify
145
+ spotify_user = RSpotify::User.new(request.env['omniauth.auth'])
146
+ # Now you can create playlists for the user and much more!
147
+
148
+ spotify_user.create_playlist!('my-awesome-playlist')
149
+ # Directly creates playlist in user's Spotify account
150
+ end
151
+ end
152
+ ```
105
153
 
106
- RSpotify focuses on simplicity of use and straight access to data, so the authorization code flow is not supported at the moment. If you really feel the need to use it, please leave a issue for it to be implemented.
154
+ **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.
107
155
 
108
156
  ## Contributing
109
157
 
data/lib/rspotify.rb CHANGED
@@ -1,14 +1,13 @@
1
- require 'rspotify/version'
2
-
3
1
  require 'base64'
4
2
  require 'json'
5
3
  require 'restclient'
6
4
 
7
5
  module RSpotify
8
6
 
9
- API_URI = 'https://api.spotify.com/v1/'
10
- TOKEN_URI = 'https://accounts.spotify.com/api/token'
11
- VERBS = %w(get post put delete)
7
+ API_URI = 'https://api.spotify.com/v1/'
8
+ AUTHORIZE_URI = 'https://accounts.spotify.com/authorize'
9
+ TOKEN_URI = 'https://accounts.spotify.com/api/token'
10
+ VERBS = %w(get post put delete)
12
11
 
13
12
  autoload :Album, 'rspotify/album'
14
13
  autoload :Artist, 'rspotify/artist'
@@ -40,3 +39,6 @@ module RSpotify
40
39
  end
41
40
  end
42
41
  end
42
+
43
+ require 'rspotify/oauth'
44
+ require 'rspotify/version'
@@ -15,7 +15,7 @@ module RSpotify
15
15
 
16
16
  # Returns Album object(s) with id(s) provided
17
17
  #
18
- # @param ids [String, Array]
18
+ # @param ids [String, Array] Maximum: 20 IDs
19
19
  # @return [Album, Array<Album>]
20
20
  #
21
21
  # @example
@@ -33,9 +33,9 @@ module RSpotify
33
33
 
34
34
  # Returns array of Album objects matching the query, ordered by popularity
35
35
  #
36
- # @param query [String]
37
- # @param limit [Integer]
38
- # @param offset [Integer]
36
+ # @param query [String] The search query's keywords.
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.
39
39
  # @return [Array<Album>]
40
40
  #
41
41
  # @example
@@ -8,7 +8,7 @@ module RSpotify
8
8
 
9
9
  # Returns Artist object(s) with id(s) provided
10
10
  #
11
- # @param ids [String, Array]
11
+ # @param ids [String, Array] Maximum: 50 IDs
12
12
  # @return [Artist, Array<Artist>]
13
13
  #
14
14
  # @example
@@ -26,9 +26,9 @@ module RSpotify
26
26
 
27
27
  # Returns array of Artist objects matching the query, ordered by popularity
28
28
  #
29
- # @param query [String]
30
- # @param limit [Integer]
31
- # @param offset [Integer]
29
+ # @param query [String] The search query's keywords.
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.
32
32
  # @return [Array<Artist>]
33
33
  #
34
34
  # @example
data/lib/rspotify/base.rb CHANGED
@@ -23,14 +23,19 @@ module RSpotify
23
23
  # tracks.class #=> Array
24
24
  # tracks.first.class #=> RSpotify::Track
25
25
  def self.find(ids, type)
26
- case ids.class.to_s
27
- when 'Array'
26
+ case ids
27
+ when Array
28
28
  if type == 'user'
29
29
  warn 'Spotify API does not support finding several users simultaneously'
30
30
  return false
31
31
  end
32
+ limit = (type == 'album' ? 20 : 50)
33
+ if ids.size > limit
34
+ warn "Too many ids requested. Maximum: #{limit} for #{type}"
35
+ return false
36
+ end
32
37
  find_many(ids, type)
33
- when 'String'
38
+ when String
34
39
  id = ids
35
40
  find_one(id, type)
36
41
  end
@@ -74,7 +79,7 @@ module RSpotify
74
79
  # albums.size #=> 10
75
80
  def self.search(query, type, limit = 20, offset = 0)
76
81
  if limit < 1 || limit > 50
77
- warn "Limit must be between 1 and 50"
82
+ warn 'Limit must be between 1 and 50'
78
83
  return false
79
84
  end
80
85
 
@@ -0,0 +1,19 @@
1
+ require 'omniauth-oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Spotify < OmniAuth::Strategies::OAuth2
6
+ option :name, 'spotify'
7
+
8
+ option :client_options, {
9
+ site: RSpotify::API_URI,
10
+ authorize_url: RSpotify::AUTHORIZE_URI,
11
+ token_url: RSpotify::TOKEN_URI,
12
+ }
13
+
14
+ info do
15
+ access_token.get('me').parsed
16
+ end
17
+ end
18
+ end
19
+ end
@@ -15,7 +15,7 @@ module RSpotify
15
15
 
16
16
  # Returns Track object(s) with id(s) provided
17
17
  #
18
- # @param ids [String, Array]
18
+ # @param ids [String, Array] Maximum: 50 IDs
19
19
  # @return [Track, Array<Track>]
20
20
  #
21
21
  # @example
@@ -33,9 +33,9 @@ module RSpotify
33
33
 
34
34
  # Returns array of Track objects matching the query, ordered by popularity
35
35
  #
36
- # @param query [String]
37
- # @param limit [Integer]
38
- # @param offset [Integer]
36
+ # @param query [String] The search query's keywords.
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.
39
39
  # @return [Array<Track>]
40
40
  #
41
41
  # @example
data/lib/rspotify/user.rb CHANGED
@@ -4,7 +4,7 @@ module RSpotify
4
4
 
5
5
  # Returns User object with id provided
6
6
  #
7
- # @param ids [String]
7
+ # @param id [String]
8
8
  # @return [User]
9
9
  #
10
10
  # @example
@@ -22,9 +22,28 @@ module RSpotify
22
22
  end
23
23
 
24
24
  def initialize(options = {})
25
+ @credentials ||= options['credentials']
26
+ options = options['info'] if options['info']
27
+
28
+ @country ||= options['country']
29
+ @display_name ||= options['display_name']
30
+ @email ||= options['email']
31
+ @images ||= options['images']
32
+ @product ||= options['product']
33
+
25
34
  super(options)
26
35
  end
27
36
 
37
+ def create_playlist!(name, public: true)
38
+ headers = {
39
+ 'Authorization' => "Bearer #{@credentials['token']}",
40
+ 'Content-Type' => 'application/json'
41
+ }
42
+ request_data = %Q({"name":"#{name}", "public":#{public}})
43
+
44
+ RSpotify.post("users/#{@id}/playlists", request_data, headers)
45
+ end
46
+
28
47
  # Returns all playlists from user
29
48
  #
30
49
  # @return [Array<Playlist>]
@@ -35,9 +54,8 @@ module RSpotify
35
54
  # playlists.first.class #=> RSpotify::Playlist
36
55
  # playlists.first.name #=> "Movie Soundtrack Masterpieces"
37
56
  def playlists
38
- return @playlists unless @playlists.nil?
39
57
  playlists = RSpotify.auth_get("users/#{@id}/playlists")['items']
40
- @playlists = playlists.map { |p| Playlist.new p }
58
+ playlists.map { |p| Playlist.new p }
41
59
  end
42
60
  end
43
61
  end
@@ -1,3 +1,3 @@
1
1
  module RSpotify
2
- VERSION = '0.7.1'
2
+ VERSION = '0.8.0'
3
3
  end
data/rspotify.gemspec CHANGED
@@ -16,6 +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.1'
19
20
  spec.add_dependency 'rest_client', '~> 1.7'
20
21
 
21
22
  spec.add_development_dependency 'bundler'
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspotify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.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-06-30 00:00:00.000000000 Z
11
+ date: 2014-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth-oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rest_client
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -112,6 +126,7 @@ files:
112
126
  - lib/rspotify/album.rb
113
127
  - lib/rspotify/artist.rb
114
128
  - lib/rspotify/base.rb
129
+ - lib/rspotify/oauth.rb
115
130
  - lib/rspotify/playlist.rb
116
131
  - lib/rspotify/track.rb
117
132
  - lib/rspotify/user.rb