rspotify 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6af35dbd2125b6c361ceae88fab934a9ab67970b
4
- data.tar.gz: 01a9189c5e26c2389bd2a82fe5a85d3f7ddd6dc3
3
+ metadata.gz: 86ed84fdd85f9e551759817dea9a12460349a4b8
4
+ data.tar.gz: e455df5cc613e94a65d78af348b6e775a21b2335
5
5
  SHA512:
6
- metadata.gz: 3627753fbca7a077f97cd15892ec972fca4c4bb4ab90daac149818651b387c75483f5a646dab2cf3d35e6751575e145904edccf7954fa6239cbf32ec474658e7
7
- data.tar.gz: e950c1e6fa5bb1e9b3b81478ef0d5fba128e1e1069f10c6075f3beed8bfa2a170cc608598198edbbb96704a73760e2ce0beb2cb36d803183824d23fc3e84bffd
6
+ metadata.gz: f1c2cad5eaf9f7d00a62ce9a2ef61360779ec5264873e44634c75f291bb034cea5734b1fb5f43d59b7f870603d233d444fc598418ceffd273f2121c58e8444a0
7
+ data.tar.gz: 4653d34b828be1ae1575771eadbc76c76fd4416f2a08a55e84845cb909d5633ae0751ee3e6fd1bd1bf7819c85a1c80b03bee6ba5b411114390289b93778f9330
data/README.md CHANGED
@@ -18,9 +18,11 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- This gem is pretty new and authentication has not yet been implemented. So far you can access public data such as albums, tracks and artists:
21
+ Directly access public data such as albums, tracks, artists and users:
22
22
 
23
23
  ```ruby
24
+ require 'rspotify'
25
+
24
26
  tracks = RSpotify::Track.search('Paranoid')
25
27
 
26
28
  tracks.first.name #=> "Paranoid"
@@ -40,13 +42,35 @@ artist.genres #=> ["Alternative Rap", "East Coast Rap", "Hardcore Rap", "Hip Hop
40
42
 
41
43
  album = RSpotify::Album.find('0uZ8zQLHru4BiNTL2PQY91')
42
44
  album.album_type #=> "single"
45
+ album.tracks #=> (Track array)
43
46
 
44
47
  track = RSpotify::Track.find('2qmxggATKFIeg68guRSG3r')
45
48
  track.duration_ms #=> 270800
49
+ track.album #=> (Album object)
46
50
 
47
51
  user = RSpotify::User.find('wizzler')
48
52
  user.external_urls["spotify"] #=> "https://open.spotify.com/user/wizzler"
49
53
  ```
54
+
55
+ Some data require authentication to be accessed, such as playlists. You can easily get your credentials [here](https://developer.spotify.com/my-applications)
56
+
57
+ Then just copy and paste them like so:
58
+
59
+ ```ruby
60
+ RSpotify.authenticate("<your_client_id>", "<your_client_secret>")
61
+
62
+ # Now you can access any public playlist and much more!
63
+
64
+ playlist = RSpotify::Playlist.find('wizzler', '00wHcTN0zQiun4xri9pmvX')
65
+ playlist.name #=> "Movie Soundtrack Masterpieces"
66
+ playlist.description #=> "Iconic soundtracks featured..."
67
+ playlist.followers['total'] #=> 13
68
+ playlist.tracks #=> (Track array)
69
+
70
+ my_user = RSpotify::User.find("my_user")
71
+ my_playlists = my_user.playlists #=> (Playlist array)
72
+ ```
73
+
50
74
  More documentation will follow
51
75
 
52
76
  ## Contributing
File without changes
@@ -1,24 +1,44 @@
1
1
  require 'rspotify/version'
2
2
 
3
+ require 'base64'
3
4
  require 'json'
4
5
  require 'restclient'
5
6
 
6
7
  module RSpotify
7
8
 
8
- BASE_URI = 'https://api.spotify.com/v1/'
9
- VERBS = %w(get post put delete)
9
+ API_URI = 'https://api.spotify.com/v1/'
10
+ TOKEN_URI = 'https://accounts.spotify.com/api/token'
11
+ VERBS = %w(get post put delete)
10
12
 
11
- autoload :Base, 'rspotify/base'
12
- autoload :Artist, 'rspotify/artist'
13
- autoload :Album, 'rspotify/album'
14
- autoload :Track, 'rspotify/track'
15
- autoload :User, 'rspotify/user'
13
+ autoload :Album, 'rspotify/album'
14
+ autoload :Artist, 'rspotify/artist'
15
+ autoload :Base, 'rspotify/base'
16
+ autoload :Playlist, 'rspotify/playlist'
17
+ autoload :Track, 'rspotify/track'
18
+ autoload :User, 'rspotify/user'
19
+
20
+ def self.authenticate(client_id, client_secret)
21
+ request_body = { grant_type: 'client_credentials' }
22
+ authorization = Base64.strict_encode64 "#{client_id}:#{client_secret}"
23
+ headers = { 'Authorization' => "Basic #{authorization}" }
24
+ response = RestClient.post(TOKEN_URI, request_body, headers)
25
+ @access_token = JSON.parse(response)['access_token']
26
+ true
27
+ end
16
28
 
17
29
  VERBS.each do |verb|
30
+
18
31
  define_singleton_method verb do |path, *params|
19
- url = BASE_URI + path
32
+ url = API_URI + path
20
33
  response = RestClient.send(verb, url, *params)
21
34
  JSON.parse response
22
35
  end
36
+
37
+ define_singleton_method "auth_#{verb}" do |path, *params|
38
+ auth_header = { 'Authorization' => "Bearer #{@access_token}" }
39
+ params << auth_header
40
+ send(verb, path, *params)
41
+ end
42
+
23
43
  end
24
44
  end
@@ -18,7 +18,8 @@ module RSpotify
18
18
  @name = options['name']
19
19
 
20
20
  if options['tracks']
21
- @tracks = options['tracks']['items'].map { |t| Track.new (t) }
21
+ tracks = options['tracks']['items']
22
+ @tracks = tracks.map { |t| Track.new t }
22
23
  end
23
24
 
24
25
  super(options)
@@ -0,0 +1,36 @@
1
+ module RSpotify
2
+
3
+ class Playlist < Base
4
+
5
+ attr_accessor :collaborative, :description, :followers,
6
+ :images, :name, :owner, :public, :tracks
7
+
8
+ def self.find(user_id, id)
9
+ json = RSpotify.auth_get("users/#{user_id}/playlists/#{id}")
10
+ Playlist.new json
11
+ end
12
+
13
+ def self.search
14
+ #TODO
15
+ end
16
+
17
+ def initialize(options = {})
18
+ @collaborative = options['collaborative']
19
+ @description = options['description']
20
+ @followers = options['followers']
21
+ @images = options['images']
22
+ @name = options['name']
23
+ @owner = options['owner']
24
+ @public = options['public']
25
+ @tracks = options['tracks']
26
+
27
+ super(options)
28
+ end
29
+
30
+ def tracks
31
+ json = RSpotify.auth_get("users/#{@owner['id']}/playlists/#{@id}/tracks")
32
+ json['items'].map{ |t| Track.new t['track'] }
33
+ end
34
+
35
+ end
36
+ end
@@ -14,5 +14,10 @@ module RSpotify
14
14
  super(options)
15
15
  end
16
16
 
17
+ def playlists
18
+ playlists = RSpotify.auth_get("users/#{@id}/playlists")['items']
19
+ playlists.map { |p| Playlist.new p }
20
+ end
21
+
17
22
  end
18
23
  end
@@ -1,3 +1,3 @@
1
1
  module RSpotify
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
File without changes
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.1.1
4
+ version: 0.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-06-19 00:00:00.000000000 Z
11
+ date: 2014-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest_client
@@ -92,14 +92,17 @@ files:
92
92
  - LICENSE.txt
93
93
  - README.md
94
94
  - Rakefile
95
+ - doc/TODO
95
96
  - lib/rspotify.rb
96
97
  - lib/rspotify/album.rb
97
98
  - lib/rspotify/artist.rb
98
99
  - lib/rspotify/base.rb
100
+ - lib/rspotify/playlist.rb
99
101
  - lib/rspotify/track.rb
100
102
  - lib/rspotify/user.rb
101
103
  - lib/rspotify/version.rb
102
104
  - rspotify.gemspec
105
+ - spec/TODO
103
106
  homepage: http://rubygems.org/gems/rspotify
104
107
  licenses:
105
108
  - MIT
@@ -124,4 +127,5 @@ rubygems_version: 2.2.2
124
127
  signing_key:
125
128
  specification_version: 4
126
129
  summary: A ruby wrapper for the Spotify Web API
127
- test_files: []
130
+ test_files:
131
+ - spec/TODO