rspotify 0.10.0 → 0.11.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: 099ef6df09a9e5e69ec2765409c9d4262f4e5e30
4
- data.tar.gz: 9f00aa8931f67d946b158391c7afa99e05fad61b
3
+ metadata.gz: 1d036fdc4740624c2fbb316884229f8e36910b12
4
+ data.tar.gz: 10ac95732e3035d833761359d7315bc6ee262fbf
5
5
  SHA512:
6
- metadata.gz: 451f177d91bcf3cb1025cac3d448b0a69a0216faf51c0f6285a0499ce36435fc6aa04b5d1de6890617339f3f50164e0c71ffaa9697430eb2429e0f8e05d98e9e
7
- data.tar.gz: 759310516f92db6b07e71e8afd8ca4b4da649376fff85580d19ea394d0f0d6b34245e68dd1e5bafb27abd55950eee5c3ee32f3e3258802af5f8a025bef91ea1c
6
+ metadata.gz: 81c76cb23d53a4fa2f5fac2a08533301656e3c54c07df43c9339bef21b25384e2bec3770a7672db67eb076a53a0c3319f46ea14be6cd79fe000702cda798c61a
7
+ data.tar.gz: bf69b0fa697e50e686c57beb3c4375e53684219ddbd9ad87086ff8c5b8bf36bfca961602bf2e03ce3e1acd240866f01e40e7325e134ee3815af5d3b188cb2e30
data/lib/rspotify.rb CHANGED
@@ -1,44 +1,12 @@
1
- require 'base64'
2
- require 'json'
3
- require 'restclient'
1
+ require 'rspotify/connection'
2
+ require 'rspotify/oauth'
3
+ require 'rspotify/version'
4
4
 
5
5
  module RSpotify
6
-
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)
11
-
12
6
  autoload :Album, 'rspotify/album'
13
7
  autoload :Artist, 'rspotify/artist'
14
8
  autoload :Base, 'rspotify/base'
15
9
  autoload :Playlist, 'rspotify/playlist'
16
10
  autoload :Track, 'rspotify/track'
17
11
  autoload :User, 'rspotify/user'
18
-
19
- def self.authenticate(client_id, client_secret)
20
- request_body = { grant_type: 'client_credentials' }
21
- authorization = Base64.strict_encode64 "#{client_id}:#{client_secret}"
22
- headers = { 'Authorization' => "Basic #{authorization}" }
23
- response = RestClient.post(TOKEN_URI, request_body, headers)
24
- @client_token = JSON.parse(response)['access_token']
25
- true
26
- end
27
-
28
- VERBS.each do |verb|
29
- define_singleton_method verb do |path, *params|
30
- url = API_URI + path
31
- response = RestClient.send(verb, url, *params)
32
- JSON.parse response unless response.empty?
33
- end
34
-
35
- define_singleton_method "auth_#{verb}" do |path, *params|
36
- auth_header = { 'Authorization' => "Bearer #{@client_token}" }
37
- params << auth_header
38
- send(verb, path, *params)
39
- end
40
- end
41
12
  end
42
-
43
- require 'rspotify/oauth'
44
- require 'rspotify/version'
@@ -1,9 +1,9 @@
1
1
  module RSpotify
2
2
 
3
- # @attr [Array<String>] genres A list of the genres used to classify the album. If not yet classified, the array is empty.
4
- # @attr [Array<Hash>] images The cover art for the album in various sizes, widest first
5
- # @attr [String] name The name of the album
6
- # @attr [Integer] popularity The popularity of the album. The value will be between 0 and 100, with 100 being the most popular
3
+ # @attr [Array<String>] genres A list of the genres the artist is associated with. If not yet classified, the array is empty
4
+ # @attr [Array<Hash>] images Images of the artist in various sizes, widest first
5
+ # @attr [String] name The name of the artist
6
+ # @attr [Integer] popularity The popularity of the artist. The value will be between 0 and 100, with 100 being the most popular
7
7
  class Artist < Base
8
8
 
9
9
  # Returns Artist object(s) with id(s) provided
@@ -0,0 +1,41 @@
1
+ require 'base64'
2
+ require 'json'
3
+ require 'restclient'
4
+
5
+ module RSpotify
6
+
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)
11
+
12
+ def self.auth_header
13
+ authorization = Base64.strict_encode64 "#{@client_id}:#{@client_secret}"
14
+ { 'Authorization' => "Basic #{authorization}" }
15
+ end
16
+ private_class_method :auth_header
17
+
18
+ def self.authenticate(client_id, client_secret)
19
+ @client_id, @client_secret = client_id, client_secret
20
+ request_body = { grant_type: 'client_credentials' }
21
+ response = RestClient.post(TOKEN_URI, request_body, auth_header)
22
+ @client_token = JSON.parse(response)['access_token']
23
+ true
24
+ end
25
+
26
+ VERBS.each do |verb|
27
+ # RSpotify::{get,post}
28
+ define_singleton_method verb do |path, *params|
29
+ url = API_URI + path
30
+ response = RestClient.send(verb, url, *params)
31
+ JSON.parse response unless response.empty?
32
+ end
33
+
34
+ # RSpotify::auth_{get,post}
35
+ define_singleton_method "auth_#{verb}" do |path, *params|
36
+ auth_header = { 'Authorization' => "Bearer #{@client_token}" }
37
+ params << auth_header
38
+ send(verb, path, *params)
39
+ end
40
+ end
41
+ end
@@ -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_headers, @owner.id))
80
+ RSpotify.post(url, {}, User.send(:oauth_header, @owner.id))
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_headers, @owner.id))
100
+ initialize RSpotify.get(url, User.send(:oauth_header, @owner.id))
101
101
  else
102
102
  initialize RSpotify.auth_get(url)
103
103
  end
data/lib/rspotify/user.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module RSpotify
2
2
 
3
3
  # @attr [String] country The country of the user, as set in the user's account profile. An {http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 ISO 3166-1 alpha-2 country code}. This field is only available when the current user has granted access to the *user-read-private* scope.
4
+ # @attr [Hash] credentials The credentials generated for the user with OAuth. Includes access token, token type, token expiration time and refresh token. This field is only available when the current user has granted access to any scope.
4
5
  # @attr [String] display_name The name displayed on the user's profile. This field is only available when the current user has granted access to the *user-read-private* scope.
5
6
  # @attr [String] email The user's email address. This field is only available when the current user has granted access to the *user-read-email* scope.
6
7
  # @attr [Array] images The user's profile image. This field is only available when the current user has granted access to the *user-read-private* scope.
@@ -26,13 +27,13 @@ module RSpotify
26
27
  false
27
28
  end
28
29
 
29
- def self.oauth_headers(user_id)
30
+ def self.oauth_header(user_id)
30
31
  {
31
32
  'Authorization' => "Bearer #{@@users_credentials[user_id]['token']}",
32
33
  'Content-Type' => 'application/json'
33
34
  }
34
35
  end
35
- private_class_method :oauth_headers
36
+ private_class_method :oauth_header
36
37
 
37
38
  def initialize(options = {})
38
39
  credentials = options['credentials']
@@ -49,6 +50,7 @@ module RSpotify
49
50
  if credentials
50
51
  @@users_credentials ||= {}
51
52
  @@users_credentials[@id] = credentials
53
+ @credentials = @@users_credentials[@id]
52
54
  end
53
55
  end
54
56
 
@@ -70,7 +72,7 @@ module RSpotify
70
72
  def create_playlist!(name, public: true)
71
73
  url = "users/#{@id}/playlists"
72
74
  request_data = %Q({"name":"#{name}", "public":#{public}})
73
- Playlist.new RSpotify.post(url, request_data, User.send(:oauth_headers, @id))
75
+ Playlist.new RSpotify.post(url, request_data, User.send(:oauth_header, @id))
74
76
  end
75
77
 
76
78
  # Returns all playlists from user
@@ -86,5 +88,14 @@ module RSpotify
86
88
  playlists = RSpotify.auth_get("users/#{@id}/playlists")['items']
87
89
  playlists.map { |p| Playlist.new p }
88
90
  end
91
+
92
+ # Returns a hash containing all user attributes
93
+ def to_hash
94
+ hash = {}
95
+ instance_variables.each do |var|
96
+ hash[var.to_s.delete('@')] = instance_variable_get(var)
97
+ end
98
+ hash
99
+ end
89
100
  end
90
101
  end
@@ -1,3 +1,3 @@
1
1
  module RSpotify
2
- VERSION = '0.10.0'
2
+ VERSION = '0.11.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: 0.10.0
4
+ version: 0.11.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-13 00:00:00.000000000 Z
11
+ date: 2014-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: omniauth-oauth2
@@ -126,6 +126,7 @@ files:
126
126
  - lib/rspotify/album.rb
127
127
  - lib/rspotify/artist.rb
128
128
  - lib/rspotify/base.rb
129
+ - lib/rspotify/connection.rb
129
130
  - lib/rspotify/oauth.rb
130
131
  - lib/rspotify/playlist.rb
131
132
  - lib/rspotify/track.rb
@@ -171,4 +172,3 @@ test_files:
171
172
  - spec/lib/rspotify/user_spec.rb
172
173
  - spec/lib/rspotify_spec.rb
173
174
  - spec/spec_helper.rb
174
- has_rdoc: