jberkel-spotify-api 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG +6 -0
- data/Rakefile +8 -0
- data/VERSION.yml +1 -1
- data/examples/create_playlist.rb +5 -8
- data/lib/jotify.rb +2 -2
- data/lib/jotify/api.rb +9 -2
- data/lib/jotify/media.rb +14 -0
- data/spec/integration_spec.rb +21 -0
- data/spec/{api_spec.rb → jotify/api_spec.rb} +24 -2
- data/spec/jotify/media_spec.rb +106 -0
- data/spec/spec_helper.rb +25 -0
- data/spotify-api.gemspec +8 -7
- metadata +7 -6
- data/spec/media_spec.rb +0 -77
data/.gitignore
CHANGED
data/CHANGELOG
ADDED
data/Rakefile
CHANGED
@@ -26,6 +26,14 @@ Spec::Rake::SpecTask.new do |t|
|
|
26
26
|
t.libs << "./lib"
|
27
27
|
end
|
28
28
|
|
29
|
+
desc "runs specs with rcov"
|
30
|
+
Spec::Rake::SpecTask.new('spec:coverage') do |t|
|
31
|
+
t.rcov = true
|
32
|
+
t.spec_files = FileList["spec/**/*_spec.rb"].delete_if { |f| f =~ /integration/ }
|
33
|
+
t.libs << "./lib"
|
34
|
+
t.rcov_opts = ['--exclude', 'spec/.*rb,\(__.+__\)']
|
35
|
+
end
|
36
|
+
|
29
37
|
Spec::Rake::SpecTask.new(:integration) do |t|
|
30
38
|
t.rcov = false
|
31
39
|
t.spec_files = FileList["spec/**/*_spec.rb"].select { |f| f =~ /integration/ }
|
data/VERSION.yml
CHANGED
data/examples/create_playlist.rb
CHANGED
@@ -6,17 +6,14 @@ require 'json'
|
|
6
6
|
require 'pp'
|
7
7
|
|
8
8
|
puts "creating a new playlist"
|
9
|
-
resp = RestClient.post
|
9
|
+
resp = RestClient.post('http://localhost:3000/playlists', {
|
10
|
+
"name"=>'my shiny playlist',
|
11
|
+
"tracks" => [ {'id'=>'6qHiOf1BFCQIzAjJsRbMfY'}, {'id'=>'1VaucR6Bsks5Q9bYBsXvuF'} ]
|
12
|
+
}.to_json)
|
13
|
+
|
10
14
|
if resp.code == 201
|
11
15
|
location = resp.headers[:location]
|
12
16
|
puts "201 created (#{location})"
|
13
|
-
spotify_id = location[location.rindex('/')+1..-1]
|
14
|
-
|
15
|
-
puts "adding some tracks to it"
|
16
|
-
resp = RestClient.put "http://localhost:3000/playlists/#{spotify_id}", {
|
17
|
-
"tracks" => [ {'id'=>'6qHiOf1BFCQIzAjJsRbMfY'}, {'id'=>'1VaucR6Bsks5Q9bYBsXvuF'} ]
|
18
|
-
}.to_json
|
19
|
-
puts resp
|
20
17
|
else
|
21
18
|
raise resp.to_s
|
22
19
|
end
|
data/lib/jotify.rb
CHANGED
@@ -49,9 +49,9 @@ class Jotify
|
|
49
49
|
@jotify.playlists.map { |p| playlist(p.getId()) }
|
50
50
|
end
|
51
51
|
|
52
|
-
def playlist(id)
|
52
|
+
def playlist(id, resolve_tracks=false)
|
53
53
|
playlist = @jotify.playlist(Jotify.resolve_id(id))
|
54
|
-
|
54
|
+
if resolve_tracks && !playlist.tracks.empty?
|
55
55
|
res = @jotify.browse(playlist.tracks)
|
56
56
|
res.tracks.each_with_index do |t,i|
|
57
57
|
playlist.tracks.set(i, t)
|
data/lib/jotify/api.rb
CHANGED
@@ -82,9 +82,16 @@ end
|
|
82
82
|
|
83
83
|
Sinatra::Application.post('/playlists') do
|
84
84
|
content_type :json
|
85
|
-
|
86
|
-
|
85
|
+
body = request.body.read
|
86
|
+
data = JSON.parse(body)
|
87
|
+
playlist = jotify.create_playlist(data['name'], data['collaborative'])
|
87
88
|
if playlist
|
89
|
+
if data['tracks']
|
90
|
+
ids = data['tracks'].map { |t| t['id'] }
|
91
|
+
unless jotify.add_tracks_to_playlist(playlist, ids)
|
92
|
+
return 500, 'status' => 'ERROR', 'message' => 'playlist created but tracks could not be added'
|
93
|
+
end
|
94
|
+
end
|
88
95
|
redirect playlist.link, 201 # created
|
89
96
|
else
|
90
97
|
return 500, { 'status' => 'ERROR', 'message' => 'playlist could not be created' }.to_json
|
data/lib/jotify/media.rb
CHANGED
@@ -22,6 +22,10 @@ module Java
|
|
22
22
|
def each(&block)
|
23
23
|
tracks.each(&block)
|
24
24
|
end
|
25
|
+
|
26
|
+
def size
|
27
|
+
tracks.size
|
28
|
+
end
|
25
29
|
|
26
30
|
def inspect
|
27
31
|
"[Playlist: #{self.getId()} #{getTracks.to_a}]"
|
@@ -38,6 +42,10 @@ module Java
|
|
38
42
|
:collaborative => collaborative
|
39
43
|
}
|
40
44
|
end
|
45
|
+
|
46
|
+
def <<(track)
|
47
|
+
tracks.add(track) if track.is_a?(Track)
|
48
|
+
end
|
41
49
|
end
|
42
50
|
|
43
51
|
class PlaylistContainer
|
@@ -46,6 +54,12 @@ module Java
|
|
46
54
|
def each(&block)
|
47
55
|
playlists.each(&block)
|
48
56
|
end
|
57
|
+
|
58
|
+
def size() playlists.size end
|
59
|
+
|
60
|
+
def <<(pl)
|
61
|
+
playlists.add(pl) if pl.is_a?(Playlist)
|
62
|
+
end
|
49
63
|
end
|
50
64
|
|
51
65
|
class Result
|
data/spec/integration_spec.rb
CHANGED
@@ -30,4 +30,25 @@ describe "Integration" do
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
+
|
34
|
+
describe "playlists" do
|
35
|
+
|
36
|
+
before do
|
37
|
+
@playlists = @jotify.playlists
|
38
|
+
@playlists.should_not be_empty
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should return name and id, not tracks (GH-3)" do
|
42
|
+
@playlists.each do |pl|
|
43
|
+
puts pl.inspect
|
44
|
+
pl.name.should_not be_nil
|
45
|
+
|
46
|
+
pl.tracks.each do |t|
|
47
|
+
t.getId().should_not be_nil
|
48
|
+
t.getName().should be_nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
33
54
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
#!/usr/bin/env jruby -S spec
|
2
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
3
3
|
|
4
4
|
require 'jotify/api'
|
5
5
|
require 'rack/test'
|
@@ -84,6 +84,7 @@ describe 'Api' do
|
|
84
84
|
before do
|
85
85
|
#String id, String name, String author, boolean collaborative
|
86
86
|
@playlist = Jotify::Media::Playlist.new("4d921ebcdd8c80f32ce1ed5acafbb9c8", "my shiny playlist", "test", false)
|
87
|
+
|
87
88
|
end
|
88
89
|
|
89
90
|
describe "get" do
|
@@ -136,10 +137,31 @@ describe 'Api' do
|
|
136
137
|
describe "create/update" do
|
137
138
|
it "should create a playlist when posting to /playlists" do
|
138
139
|
@jotify.should_receive(:create_playlist).with('my shiny playlist', true).and_return(@playlist)
|
139
|
-
post '/playlists',
|
140
|
+
post '/playlists', { 'name' => 'my shiny playlist', 'collaborative' => true }.to_json
|
141
|
+
last_response.status.should == 201
|
142
|
+
last_response.headers['Location'].should == 'http://open.spotify.com/user/test/playlist/2mnbxTkghYtlHMdX3jdP9C'
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should create a playlist and adding tracks when posting to /playlists" do
|
146
|
+
@jotify.should_receive(:create_playlist).with('my shiny playlist', true).and_return(@playlist)
|
147
|
+
@jotify.should_receive(:add_tracks_to_playlist).with(@playlist, ['1','2']).and_return(true)
|
148
|
+
|
149
|
+
post '/playlists', { 'name' => 'my shiny playlist',
|
150
|
+
'collaborative' => true,
|
151
|
+
'tracks' => [ {'id'=>'1' }, { 'id'=>'2' } ]
|
152
|
+
}.to_json
|
140
153
|
last_response.status.should == 201
|
141
154
|
last_response.headers['Location'].should == 'http://open.spotify.com/user/test/playlist/2mnbxTkghYtlHMdX3jdP9C'
|
142
155
|
end
|
156
|
+
|
157
|
+
|
158
|
+
it "should return 500 if playlist could not be created" do
|
159
|
+
@jotify.should_receive(:create_playlist).with('my shiny playlist', true).and_return(nil)
|
160
|
+
post '/playlists', { :name => 'my shiny playlist', :collaborative =>true }.to_json
|
161
|
+
last_response.status.should == 500
|
162
|
+
json_response.should == {"status"=>"ERROR", "message"=>"playlist could not be created"}
|
163
|
+
end
|
164
|
+
|
143
165
|
|
144
166
|
it "should update playlist when putting to /playlists/id" do
|
145
167
|
@jotify.should_receive(:playlist).with("foo").and_return(@playlist)
|
@@ -0,0 +1,106 @@
|
|
1
|
+
#!/usr/bin/env jruby -S spec
|
2
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
3
|
+
require 'jotify'
|
4
|
+
|
5
|
+
describe Jotify::Media do
|
6
|
+
|
7
|
+
describe "an artist" do
|
8
|
+
before do
|
9
|
+
@artist = Jotify::Media::Artist.new(spotify_hex_id)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should implement to_h" do
|
13
|
+
@artist.to_h.should == {:id=>"4d921ebcdd8c80f32ce1ed5acafbb9c8", :popularity=>0.0, :url=>"http://open.spotify.com/artist/2mnbxTkghYtlHMdX3jdP9C", :name=>nil}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "a track" do
|
18
|
+
before do
|
19
|
+
@track = Jotify::Media::Track.new(spotify_hex_id)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should implement to_h" do
|
23
|
+
@track.to_h.should == {:id=>"4d921ebcdd8c80f32ce1ed5acafbb9c8", :popularity=>0.0, :url=>"http://open.spotify.com/track/2mnbxTkghYtlHMdX3jdP9C", :title=>nil, :artist=>nil, :album=>nil}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "an album" do
|
28
|
+
before do
|
29
|
+
@album = Jotify::Media::Album.new(spotify_hex_id)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should implement to_h" do
|
33
|
+
@album.to_h.should == {:id=>"4d921ebcdd8c80f32ce1ed5acafbb9c8", :popularity=>0.0, :url=>"http://open.spotify.com/album/2mnbxTkghYtlHMdX3jdP9C", :name=>nil, :artist=>nil, :year=>-1, :type=>nil}
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should implement enumerable" do
|
37
|
+
@album.class.included_modules.should include(Enumerable)
|
38
|
+
@album.to_a.should == []
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "a result" do
|
43
|
+
before do
|
44
|
+
@result = Jotify::Media::Result.new
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should implement to_h" do
|
48
|
+
@result.to_h.should == {:artists=>[], :albums=>[], :tracks=>[]}
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should implement enumerable" do
|
52
|
+
@result.class.included_modules.should include(Enumerable)
|
53
|
+
@result.respond_to?(:each).should be_true
|
54
|
+
@result.to_a.should == []
|
55
|
+
@result.inspect.should == "{:artists=>[], :albums=>[], :tracks=>[]}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "playlists" do
|
60
|
+
before(:each) do
|
61
|
+
@p = Jotify::Media::Playlist.new
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should implement enumerable, size, inspect" do
|
65
|
+
@p.class.included_modules.should include(Enumerable)
|
66
|
+
@p.respond_to?(:each).should be_true
|
67
|
+
10.times { @p.tracks.add(Jotify::Media::Track.new) }
|
68
|
+
@p.size.should == 10
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should implement inspect" do
|
72
|
+
@p.inspect.should == "[Playlist: ]"
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should implement <<(track)" do
|
76
|
+
@t = Jotify::Media::Track.new(spotify_hex_id)
|
77
|
+
lambda {
|
78
|
+
@p << @t
|
79
|
+
}.should change(@p, :size).by(1)
|
80
|
+
@p.to_a.first.should == @t
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "playlist container" do
|
85
|
+
before do
|
86
|
+
@container = Jotify::Media::PlaylistContainer.new
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should implement enumerable" do
|
90
|
+
@container.respond_to?(:each).should be_true
|
91
|
+
@container.class.included_modules.should include(Enumerable)
|
92
|
+
@container.to_a.should == []
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should implement size" do
|
96
|
+
@container.size.should == 0
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should implement <<(playlist)" do
|
100
|
+
lambda {
|
101
|
+
2.times { @container << empty_playlist }
|
102
|
+
}.should change(@container, :size).by(2)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -9,3 +9,28 @@ Spec::Runner.configure do |config|
|
|
9
9
|
end
|
10
10
|
|
11
11
|
|
12
|
+
module Helpers
|
13
|
+
def spotify_hex_id
|
14
|
+
'4d921ebcdd8c80f32ce1ed5acafbb9c8'
|
15
|
+
end
|
16
|
+
|
17
|
+
def spotify_uri
|
18
|
+
'2mnbxTkghYtlHMdX3jdP9C'
|
19
|
+
end
|
20
|
+
|
21
|
+
def empty_playlist
|
22
|
+
Jotify::Media::Playlist.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def empty_track
|
26
|
+
Jotify::Media::Track.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def empty_album
|
30
|
+
Jotify::Media::Album.new
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
Spec::Example::ExampleMethods.send(:include, Helpers)
|
35
|
+
|
36
|
+
|
data/spotify-api.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{spotify-api}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.3"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Jan Berkel"]
|
9
|
-
s.date = %q{2009-08-
|
9
|
+
s.date = %q{2009-08-07}
|
10
10
|
s.default_executable = %q{spotify-api-server}
|
11
11
|
s.description = %q{an api for spotify, based on jotify}
|
12
12
|
s.email = %q{jan.berkel@gmail.com}
|
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
|
|
17
17
|
]
|
18
18
|
s.files = [
|
19
19
|
".gitignore",
|
20
|
+
"CHANGELOG",
|
20
21
|
"LICENSE",
|
21
22
|
"README.md",
|
22
23
|
"Rakefile",
|
@@ -29,10 +30,10 @@ Gem::Specification.new do |s|
|
|
29
30
|
"lib/jotify.rb",
|
30
31
|
"lib/jotify/api.rb",
|
31
32
|
"lib/jotify/media.rb",
|
32
|
-
"spec/api_spec.rb",
|
33
33
|
"spec/integration_spec.rb",
|
34
|
+
"spec/jotify/api_spec.rb",
|
35
|
+
"spec/jotify/media_spec.rb",
|
34
36
|
"spec/jotify_spec.rb",
|
35
|
-
"spec/media_spec.rb",
|
36
37
|
"spec/spec_helper.rb",
|
37
38
|
"spotify-api.gemspec"
|
38
39
|
]
|
@@ -42,10 +43,10 @@ Gem::Specification.new do |s|
|
|
42
43
|
s.rubygems_version = %q{1.3.5}
|
43
44
|
s.summary = %q{an api for spotify, based on jotify}
|
44
45
|
s.test_files = [
|
45
|
-
"spec/
|
46
|
-
"spec/
|
46
|
+
"spec/integration_spec.rb",
|
47
|
+
"spec/jotify/api_spec.rb",
|
48
|
+
"spec/jotify/media_spec.rb",
|
47
49
|
"spec/jotify_spec.rb",
|
48
|
-
"spec/media_spec.rb",
|
49
50
|
"spec/spec_helper.rb",
|
50
51
|
"examples/create_playlist.rb",
|
51
52
|
"examples/list_playlists.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jberkel-spotify-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Berkel
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-07 00:00:00 -07:00
|
13
13
|
default_executable: spotify-api-server
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -63,6 +63,7 @@ extra_rdoc_files:
|
|
63
63
|
- README.md
|
64
64
|
files:
|
65
65
|
- .gitignore
|
66
|
+
- CHANGELOG
|
66
67
|
- LICENSE
|
67
68
|
- README.md
|
68
69
|
- Rakefile
|
@@ -75,10 +76,10 @@ files:
|
|
75
76
|
- lib/jotify.rb
|
76
77
|
- lib/jotify/api.rb
|
77
78
|
- lib/jotify/media.rb
|
78
|
-
- spec/api_spec.rb
|
79
79
|
- spec/integration_spec.rb
|
80
|
+
- spec/jotify/api_spec.rb
|
81
|
+
- spec/jotify/media_spec.rb
|
80
82
|
- spec/jotify_spec.rb
|
81
|
-
- spec/media_spec.rb
|
82
83
|
- spec/spec_helper.rb
|
83
84
|
- spotify-api.gemspec
|
84
85
|
has_rdoc: false
|
@@ -109,10 +110,10 @@ signing_key:
|
|
109
110
|
specification_version: 3
|
110
111
|
summary: an api for spotify, based on jotify
|
111
112
|
test_files:
|
112
|
-
- spec/api_spec.rb
|
113
113
|
- spec/integration_spec.rb
|
114
|
+
- spec/jotify/api_spec.rb
|
115
|
+
- spec/jotify/media_spec.rb
|
114
116
|
- spec/jotify_spec.rb
|
115
|
-
- spec/media_spec.rb
|
116
117
|
- spec/spec_helper.rb
|
117
118
|
- examples/create_playlist.rb
|
118
119
|
- examples/list_playlists.rb
|
data/spec/media_spec.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
#!/usr/bin/env jruby -S spec
|
2
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
3
|
-
require 'jotify'
|
4
|
-
|
5
|
-
describe Jotify::Media do
|
6
|
-
|
7
|
-
describe "an artist" do
|
8
|
-
before do
|
9
|
-
@artist = Jotify::Media::Artist.new
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should implement to_h" do
|
13
|
-
@artist.respond_to?(:to_h).should be_true
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
describe "a track" do
|
18
|
-
before do
|
19
|
-
@track = Jotify::Media::Track.new
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should implement to_h" do
|
23
|
-
@track.respond_to?(:to_h).should be_true
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe "an album" do
|
28
|
-
before do
|
29
|
-
@album = Jotify::Media::Album.new
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should implement to_h" do
|
33
|
-
@album.respond_to?(:to_h).should be_true
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should implement enumerable" do
|
37
|
-
@album.class.included_modules.should include(Enumerable)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
describe "a result" do
|
42
|
-
before do
|
43
|
-
@result = Jotify::Media::Result.new
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should implement to_h" do
|
47
|
-
@result.respond_to?(:to_h).should be_true
|
48
|
-
end
|
49
|
-
|
50
|
-
it "should implement enumerable" do
|
51
|
-
@result.class.included_modules.should include(Enumerable)
|
52
|
-
@result.respond_to?(:each).should be_true
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
describe "playlists" do
|
57
|
-
it "should implement enumerable" do
|
58
|
-
p = Jotify::Media::Playlist.new
|
59
|
-
p.class.included_modules.should include(Enumerable)
|
60
|
-
p.respond_to?(:each).should be_true
|
61
|
-
10.times { p.tracks.add(Jotify::Media::Track.new) }
|
62
|
-
p.to_a.size.should == 10
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
describe "playlist container" do
|
67
|
-
before do
|
68
|
-
@container = Jotify::Media::PlaylistContainer.new
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should implement enumerable" do
|
72
|
-
@container.respond_to?(:each).should be_true
|
73
|
-
@container.class.included_modules.should include(Enumerable)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|