soundcloud-ruby-api-wrapper 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +49 -0
- data/VERSION.yml +4 -0
- data/lib/soundcloud/models/base.rb +87 -0
- data/lib/soundcloud/models/comment.rb +16 -0
- data/lib/soundcloud/models/event.rb +22 -0
- data/lib/soundcloud/models/playlist.rb +20 -0
- data/lib/soundcloud/models/track.rb +99 -0
- data/lib/soundcloud/models/user.rb +45 -0
- data/lib/soundcloud.rb +54 -0
- data/spec/fixtures/test_track.mp3 +0 -0
- data/spec/soundcloud_comment_spec.rb +31 -0
- data/spec/soundcloud_event_spec.rb +14 -0
- data/spec/soundcloud_playlist_spec.rb +47 -0
- data/spec/soundcloud_spec.rb +42 -0
- data/spec/soundcloud_track_spec.rb +87 -0
- data/spec/soundcloud_user_spec.rb +54 -0
- data/spec/spec_helper.rb +26 -0
- metadata +97 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 jwagener
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "ruby-api-wrapper"
|
8
|
+
gem.summary = %Q{TODO}
|
9
|
+
gem.email = "johannes@wagener.cc"
|
10
|
+
gem.homepage = "http://github.com/soundcloud/ruby-api-wrapper"
|
11
|
+
gem.authors = ["Johannes Wagener"]
|
12
|
+
gem.add_dependency "jwagener-oauth-active-resource"
|
13
|
+
gem.add_dependency "oauth"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'spec/rake/spectask'
|
21
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
22
|
+
spec.libs << 'lib' << 'spec'
|
23
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
24
|
+
end
|
25
|
+
|
26
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
29
|
+
spec.rcov = true
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
task :default => :spec
|
34
|
+
|
35
|
+
require 'rake/rdoctask'
|
36
|
+
Rake::RDocTask.new do |rdoc|
|
37
|
+
if File.exist?('VERSION.yml')
|
38
|
+
config = YAML.load(File.read('VERSION.yml'))
|
39
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
40
|
+
else
|
41
|
+
version = ""
|
42
|
+
end
|
43
|
+
|
44
|
+
rdoc.rdoc_dir = 'rdoc'
|
45
|
+
rdoc.title = "ruby-api-wrapper #{version}"
|
46
|
+
rdoc.rdoc_files.include('README*')
|
47
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
48
|
+
end
|
49
|
+
|
data/VERSION.yml
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
module Soundcloud
|
2
|
+
module Models
|
3
|
+
class Base < OAuthActiveResource::Resource #:nodoc:
|
4
|
+
# self.site = 'http://api.soundcloud.dev'
|
5
|
+
|
6
|
+
|
7
|
+
# has_many_single_changeable and can_be_a_single_changeable is mostly used in combination with has_many.
|
8
|
+
#
|
9
|
+
# can_be_a_single_changeable expects to have a resource /me which is the logged-in user
|
10
|
+
#
|
11
|
+
# like in self.has_many you have a resource user with a sub resource friends,
|
12
|
+
# but its not allowed to send a PUT /me/friends to update the list of friends
|
13
|
+
# instead to add or remove a friend you have to send a GET/PUT/DELETE to /me/friends/{user_id}
|
14
|
+
#
|
15
|
+
# Example:
|
16
|
+
#
|
17
|
+
# class User < Resource
|
18
|
+
# has_many :friends
|
19
|
+
# can_be_a_single_changeable :friend
|
20
|
+
# has_many_single_changeable :friends
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# me = User.find(:one, :from => '/me')
|
24
|
+
# friend = me.friends.first
|
25
|
+
# stranger = User.find(235)
|
26
|
+
#
|
27
|
+
# friend.is_friend?
|
28
|
+
# => true
|
29
|
+
# stranger.is_friend?
|
30
|
+
# => false
|
31
|
+
#
|
32
|
+
# strange.add_friend!
|
33
|
+
# stranger.is_friend?
|
34
|
+
# => true
|
35
|
+
#
|
36
|
+
# stranger.remove_friend!
|
37
|
+
# stranger.is_friend?
|
38
|
+
# => false
|
39
|
+
#
|
40
|
+
# friend.has_friend?(stranger.id)
|
41
|
+
# => checks if stranger and friend are friend, returns true or false
|
42
|
+
|
43
|
+
def self.can_be_a_single_changeable(*args)
|
44
|
+
args.each do |k|
|
45
|
+
singular = k.to_s
|
46
|
+
define_method("is_#{singular}?") do
|
47
|
+
begin
|
48
|
+
self.connection.get_without_decoding "/me/#{singular.pluralize}/#{self.id}"
|
49
|
+
return true
|
50
|
+
rescue ActiveResource::ResourceNotFound
|
51
|
+
return false
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
define_method("add_#{singular}!") do
|
56
|
+
self.connection.put "/me/#{singular.pluralize}/#{self.id}"
|
57
|
+
end
|
58
|
+
|
59
|
+
define_method("remove_#{singular}!") do
|
60
|
+
self.connection.delete "/me/#{singular.pluralize}/#{self.id}"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# see can_be_a_single_changeable
|
66
|
+
def self.has_many_single_changeable(*args)
|
67
|
+
args.each do |k|
|
68
|
+
singular = k.to_s.singularize
|
69
|
+
define_method("has_#{singular}?") do |object_or_id|
|
70
|
+
if object_or_id.is_a? String or object_or_id.is_a? Integer
|
71
|
+
look_for_id = object_or_id
|
72
|
+
else
|
73
|
+
look_for_id = object_or_id.id
|
74
|
+
end
|
75
|
+
|
76
|
+
begin
|
77
|
+
self.connection.get_without_decoding "/#{self.element_name.pluralize}/#{self.id}/#{singular.pluralize}/#{look_for_id}"
|
78
|
+
return true
|
79
|
+
rescue ActiveResource::ResourceNotFound
|
80
|
+
return false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Soundcloud
|
2
|
+
module Models
|
3
|
+
|
4
|
+
# Soundcloud Comment resource
|
5
|
+
#
|
6
|
+
# http://wiki.github.com/soundcloud/api/documentation#comment
|
7
|
+
#
|
8
|
+
# See Soundcloud::Models::Track
|
9
|
+
class Comment < Base
|
10
|
+
cattr_accessor :element_name
|
11
|
+
belongs_to :user, :track
|
12
|
+
self.site = "#{self.site}/tracks/:track_id/"
|
13
|
+
self.element_name = 'comment'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Soundcloud
|
2
|
+
module Models
|
3
|
+
|
4
|
+
# Soundcloud Event resource
|
5
|
+
#
|
6
|
+
# http://wiki.github.com/soundcloud/api/documentation#event
|
7
|
+
#
|
8
|
+
# Examples:
|
9
|
+
#
|
10
|
+
# client.Event.find(:all,:params => {:filter => 'drop'})
|
11
|
+
# => get all dropbox events
|
12
|
+
#
|
13
|
+
# client.Event.find(:all)
|
14
|
+
# => get all events
|
15
|
+
#
|
16
|
+
class Event < Base
|
17
|
+
belongs_to :user, :track
|
18
|
+
cattr_accessor :element_name
|
19
|
+
self.element_name = 'event'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Soundcloud
|
2
|
+
module Models
|
3
|
+
# Soundcloud Playlist resource
|
4
|
+
#
|
5
|
+
# http://wiki.github.com/soundcloud/api/documentation#playlist
|
6
|
+
#
|
7
|
+
# Examples:
|
8
|
+
class Playlist < Base
|
9
|
+
belongs_to :user
|
10
|
+
has_many :permissions
|
11
|
+
cattr_accessor :element_name
|
12
|
+
self.element_name = 'playlist'
|
13
|
+
def initialize(*args)
|
14
|
+
super(*args)
|
15
|
+
#create empty tracks array if not existing
|
16
|
+
attributes['tracks'] = Array.new if not self.tracks?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module Soundcloud
|
2
|
+
module Models
|
3
|
+
|
4
|
+
# Soundcloud Track resource
|
5
|
+
#
|
6
|
+
# http://wiki.github.com/soundcloud/api/documentation#track
|
7
|
+
#
|
8
|
+
# Examples:
|
9
|
+
#
|
10
|
+
# some_user_tracks = client.Track.find(:all, :from => '/users/some_user/tracks')
|
11
|
+
# => gets the tracks from some_user
|
12
|
+
#
|
13
|
+
# first_song = some_user_tracks.first
|
14
|
+
# => gets the first song from some_user_tracks
|
15
|
+
#
|
16
|
+
# first_song.comments.each do |comment|
|
17
|
+
# if comment.timestamp.nil?
|
18
|
+
# timestamp = ""
|
19
|
+
# else
|
20
|
+
# timestamp = "@#{comment.timestamp/1000}"
|
21
|
+
# end
|
22
|
+
# p "#{comment.user.username} #{timestamp}: #{comment.body}"
|
23
|
+
# end
|
24
|
+
# => Prints all comments of first_song with username, timestamp and body
|
25
|
+
#
|
26
|
+
#
|
27
|
+
# slow_tracks = client.Track.find(:all, :params => { "bpm[to]" => "100"} )
|
28
|
+
# => Gets tracks with a BPM <= 100
|
29
|
+
#
|
30
|
+
# new_track = client.Track.new
|
31
|
+
# new_track.title = 'New Track'
|
32
|
+
# new_track.sharing = 'private'
|
33
|
+
# new_track.set_asset_data(File.new('some_sound_file.wav')
|
34
|
+
# new_track.save
|
35
|
+
# => Create a new Track on Soundcloud with some_sound_file.mp3 as asset data
|
36
|
+
#
|
37
|
+
# File.open('another_sound_file.wav', 'w') {|f| f.write( new_track.download ) }
|
38
|
+
# => Downloads the original soundfile. some_sound_file.wav and another_sound_file.wav should be equal
|
39
|
+
#
|
40
|
+
# some_user = client.User.find('some_user')
|
41
|
+
# new_track.permissions << some_user
|
42
|
+
# new_track.permissions.save
|
43
|
+
# => Gives some_user permission to access the new_track
|
44
|
+
#
|
45
|
+
#
|
46
|
+
#
|
47
|
+
#
|
48
|
+
|
49
|
+
class Track < Base
|
50
|
+
belongs_to :user
|
51
|
+
has_many :permissions, :comments
|
52
|
+
can_be_a_single_changeable :favorite
|
53
|
+
|
54
|
+
cattr_accessor :element_name
|
55
|
+
self.element_name = 'track'
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
def download
|
60
|
+
raise Exception.new('Track is not downloadable') if not downloadable
|
61
|
+
begin
|
62
|
+
response = connection.handle_response( self.class.oauth_connection.get(download_url) )
|
63
|
+
rescue ActiveResource::Redirection => redirection
|
64
|
+
response = Net::HTTP.get(URI.parse(redirection.response['Location']))
|
65
|
+
end
|
66
|
+
return response
|
67
|
+
end
|
68
|
+
|
69
|
+
# multipart stuff, to upload a soundfile
|
70
|
+
|
71
|
+
@asset_data = nil
|
72
|
+
def set_asset_data(file)
|
73
|
+
@asset_data = file
|
74
|
+
end
|
75
|
+
|
76
|
+
def update
|
77
|
+
if not @asset_data.nil?
|
78
|
+
raise 'Multipart update is NotImplemented'
|
79
|
+
self.class.send_multipart_request(:put,'/tracks/#{self.id}','replacement[asset_data]',@asset_data)
|
80
|
+
end
|
81
|
+
super
|
82
|
+
end
|
83
|
+
|
84
|
+
def create
|
85
|
+
if @asset_data.nil?
|
86
|
+
super
|
87
|
+
else
|
88
|
+
#post asset_data
|
89
|
+
params = {'track[title]' => self.title,'track[sharing]' => self.sharing}
|
90
|
+
response = connection.handle_response(self.class.send_multipart_request(:post,'/tracks','track[asset_data]',@asset_data,params))
|
91
|
+
self.id = id_from_response(response)
|
92
|
+
@asset_data = nil
|
93
|
+
# second, 'normal' update request
|
94
|
+
update
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Soundcloud
|
2
|
+
module Models
|
3
|
+
|
4
|
+
# Soundcloud User resource
|
5
|
+
#
|
6
|
+
# http://wiki.github.com/soundcloud/api/documentation#user
|
7
|
+
#
|
8
|
+
# Examples:
|
9
|
+
# user = client.User.find('userABC')
|
10
|
+
# => gets the user with username userABC
|
11
|
+
#
|
12
|
+
# erics = client.User.find(:all, :params=> {:q => "eric"})
|
13
|
+
# erics.each do |user|
|
14
|
+
# p user.username
|
15
|
+
# end
|
16
|
+
# => finds all users named eric and print their usernames
|
17
|
+
#
|
18
|
+
# me = client.User.find(:one, :from => '/me')
|
19
|
+
# => gets the logged-in user
|
20
|
+
#
|
21
|
+
# eric1 = erics.first
|
22
|
+
# eric2 = erics[1]
|
23
|
+
# eric1.has_contact?(eric2)
|
24
|
+
# => checks if the first user named eric is following the second user named eric
|
25
|
+
#
|
26
|
+
# eric2.add_contact!
|
27
|
+
# => makes the loggedin user following eric2
|
28
|
+
|
29
|
+
class User < Base
|
30
|
+
has_many :tracks, :contacts, :comments, :favorites, :playlists
|
31
|
+
has_many_single_changeable :contacts, :favorites
|
32
|
+
can_be_a_single_changeable :contact
|
33
|
+
|
34
|
+
cattr_accessor :element_name
|
35
|
+
self.element_name = 'user'
|
36
|
+
end
|
37
|
+
|
38
|
+
class Permission < User #:nodoc:
|
39
|
+
end
|
40
|
+
|
41
|
+
class Contact < User #:nodoc:
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
data/lib/soundcloud.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# This is a ruby wrapper for the soundcloud API
|
2
|
+
#
|
3
|
+
# Author:: Johannes Wagener (johannes@wagener.cc)
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
|
7
|
+
gem 'oauth'
|
8
|
+
require 'oauth'
|
9
|
+
|
10
|
+
gem 'jwagener-oauth-active-resource'
|
11
|
+
require 'oauth_active_resource'
|
12
|
+
|
13
|
+
module Soundcloud
|
14
|
+
# Will create an OAuth Consumer for you.
|
15
|
+
# You have to register your application on soundcloud.com to get a consumer token and secret.
|
16
|
+
# Optionally you can specify another provider site (i.e. http://api.sandbox-soundcloud.com)
|
17
|
+
# Default provider site is http://api.soundcloud.com
|
18
|
+
def self.consumer(consumer_token,consumer_secret, site = 'http://api.soundcloud.com')
|
19
|
+
return OAuth::Consumer.new(consumer_token, consumer_secret, {
|
20
|
+
:site => site,
|
21
|
+
:request_token_path => "/oauth/request_token",
|
22
|
+
:access_token_path => "/oauth/access_token",
|
23
|
+
:authorize_path => "/oauth/authorize",
|
24
|
+
:scheme => :query_string
|
25
|
+
})
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
# Will create a soundcloud module containing all the soundcloud models.
|
30
|
+
# This module is bound to the given OAuth access token.
|
31
|
+
#
|
32
|
+
# Options:
|
33
|
+
# :access_token = your_oauth_access token
|
34
|
+
# :site = soundcloud_api_site (i.e. "http://api.sandbox-soundcloud.com", defaults to "http://api.soundcloud.com")
|
35
|
+
# Examples:
|
36
|
+
#
|
37
|
+
# cl = Soundcloud.register()
|
38
|
+
# => unauthenticated to "http://api.soundcloud.com"
|
39
|
+
# cl = Soundcloud.register({:access_token => your_access_token, :site => "http://api.sandbox-soundcloud.com"})
|
40
|
+
# => authenticated connection to soundcloud sandbox
|
41
|
+
#
|
42
|
+
def self.register(options = {})
|
43
|
+
options[:site] = options[:site] || 'http://api.soundcloud.com'
|
44
|
+
OAuthActiveResource.register(self.ancestors.first, self.ancestors.first.const_get('Models'), options)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
require 'soundcloud/models/base'
|
49
|
+
require 'soundcloud/models/user'
|
50
|
+
require 'soundcloud/models/comment'
|
51
|
+
require 'soundcloud/models/event'
|
52
|
+
require 'soundcloud/models/playlist'
|
53
|
+
require 'soundcloud/models/track'
|
54
|
+
|
Binary file
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Soundcloud::Models::Comment' do
|
4
|
+
before(:all) do
|
5
|
+
@sc = Soundcloud.register({:access_token=> valid_oauth_access_token, :site => soundcloud_site})
|
6
|
+
# @api_test_1 = @sc.User.find('api-test-1')
|
7
|
+
# @api_test_2 = @sc.User.find('api-test-2')
|
8
|
+
# @api_test_3 = @sc.User.find('api-test-3')
|
9
|
+
@track = @sc.Track.find('static-test-track')
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should be able to create and deleta a new comment for a track' do
|
13
|
+
|
14
|
+
old_count = @track.comments.count
|
15
|
+
comment = @sc.Comment.create({:track_id => @track.id, :body => "new API Test comment"})
|
16
|
+
@track.comments.reload.count.should be old_count + 1
|
17
|
+
|
18
|
+
comment.destroy
|
19
|
+
|
20
|
+
@track.comments.reload.count.should be old_count
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should belong to a track and a user' do
|
24
|
+
comment = @track.comments.first
|
25
|
+
|
26
|
+
comment.user.id.should_not be nil
|
27
|
+
comment.track.id.should_not be nil
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Soundcloud::Models::Event' do
|
4
|
+
before(:all) do
|
5
|
+
@sc = Soundcloud.register({:access_token=> valid_oauth_access_token, :site => soundcloud_site})
|
6
|
+
# @api_test_1 = @sc.User.find('api-test-1')
|
7
|
+
# @api_test_2 = @sc.User.find('api-test-2')
|
8
|
+
# @api_test_3 = @sc.User.find('api-test-3')
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should get the last events' do
|
12
|
+
events = @sc.Event.find(:all)
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe 'Soundcloud::Models::Playlist' do
|
5
|
+
before(:all) do
|
6
|
+
@sc = Soundcloud.register({:access_token=> valid_oauth_access_token, :site => soundcloud_site})
|
7
|
+
@api_test_1 = @sc.User.find('api-test-1')
|
8
|
+
@api_test_2 = @sc.User.find('api-test-2')
|
9
|
+
@api_test_3 = @sc.User.find('api-test-3')
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should be able to create and deleta a new playlist' do
|
14
|
+
# TODO is not implemented in the soundcloud api
|
15
|
+
# pl = @sc.Playlist.new
|
16
|
+
# pl.title = 'Static Test Playlist'
|
17
|
+
# pl.tracks << @sc.Track.find('static-test-track')
|
18
|
+
|
19
|
+
# p pl.to_xml
|
20
|
+
# pl.save
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should be able to find an existing playlist' do
|
24
|
+
pl = @sc.Playlist.find('static-test-playlist')
|
25
|
+
pl.tracks.count.should be >= 3
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should be able to delete tracks of a playlist and put them back' do
|
29
|
+
pl = @sc.Playlist.find('my-static-playlist')
|
30
|
+
old_count = pl.tracks.count
|
31
|
+
deleted_track = pl.tracks.first
|
32
|
+
pl.tracks.delete_at 0
|
33
|
+
pl.save
|
34
|
+
pl.tracks.count.should be == old_count -1
|
35
|
+
|
36
|
+
pl.tracks << deleted_track
|
37
|
+
pl.save
|
38
|
+
pl.tracks.count.should be == old_count
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should belong to a user' do
|
42
|
+
pl = @sc.Playlist.find('static-test-playlist')
|
43
|
+
# check against online attribute, to make sure the complete user is loaded, not the nested user
|
44
|
+
pl.user.online.should_not be nil
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# this spec tests against api.sandbox-soundcloud.com
|
4
|
+
# it makes the follwing assumptions
|
5
|
+
#
|
6
|
+
# the users api-test-1, api-test-2, api-test-3 exists
|
7
|
+
#
|
8
|
+
# the logged in user is api-test-1
|
9
|
+
#
|
10
|
+
# api-test-1 follows api-test-2 but NOT api-test-3 (??)
|
11
|
+
# api-test-2 follows api-test-1 and api-test-3
|
12
|
+
# api-test-3 follows api-test-2 but NOT api-test-1
|
13
|
+
#
|
14
|
+
# api-test-2 has 3 Tracks [Track1 (downloadable),Track2,Track3]
|
15
|
+
# api-test-2 has favorites: Track1 but NOT Track2
|
16
|
+
# api-test-2 has 'static-test-playlist' with Track1, 2 and 3
|
17
|
+
#
|
18
|
+
# api-test-1 has Track "static-test-track" and user api-test-3 has not permissions for it
|
19
|
+
# api-test-1 has a playlist "my-static-playlist" with at least 2 tracks
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
describe "Soundcloud" do
|
24
|
+
before(:all) do
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should create an oauth consumer' do
|
28
|
+
Soundcloud.consumer('consumer_token','consumer_secret').should be_an_instance_of OAuth::Consumer
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should register a client without an oauth token' do
|
32
|
+
sc = Soundcloud.register({:site => soundcloud_site})
|
33
|
+
sc.to_s.should match(/Soundcloud::.+/)
|
34
|
+
lambda{ sc.User.find(:one, :from => "/me")}.should raise_error ActiveResource::UnauthorizedAccess
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should register a client with an oauth token' do
|
38
|
+
sc = Soundcloud.register({:access_token=> valid_oauth_access_token, :site => soundcloud_site})
|
39
|
+
sc.to_s.should match(/Soundcloud::.+/)
|
40
|
+
lambda{ sc.User.find(:one, :from => "/me")}.should_not raise_error ActiveResource::UnauthorizedAccess
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Soundcloud::Models::Track" do
|
4
|
+
before(:all) do
|
5
|
+
@sc = Soundcloud.register({:access_token=> valid_oauth_access_token, :site => soundcloud_site})
|
6
|
+
|
7
|
+
@api_test_1 = @sc.User.find('api-test-1')
|
8
|
+
@api_test_2 = @sc.User.find('api-test-2')
|
9
|
+
@api_test_3 = @sc.User.find('api-test-3')
|
10
|
+
|
11
|
+
test_tracks = @api_test_2.tracks
|
12
|
+
@test_track_1 = test_tracks.first
|
13
|
+
|
14
|
+
begin
|
15
|
+
track = @sc.Track.find('static-test-track')
|
16
|
+
track.permissions.delete( @api_test_3 )
|
17
|
+
track.permissions << @api_test_2
|
18
|
+
track.permissions.save
|
19
|
+
rescue
|
20
|
+
end
|
21
|
+
begin
|
22
|
+
@test_track_1.remove_favorite!
|
23
|
+
rescue
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should be able to create a new track and remove it' do
|
29
|
+
test_track_file = File.new('spec/fixtures/test_track.mp3')
|
30
|
+
|
31
|
+
track = @sc.Track.new
|
32
|
+
track.title = 'API Test 1'
|
33
|
+
track.sharing = 'private'
|
34
|
+
track.set_asset_data(test_track_file)
|
35
|
+
track.save
|
36
|
+
|
37
|
+
track.destroy
|
38
|
+
|
39
|
+
lambda { track.reload }.should raise_error ActiveResource::ResourceNotFound
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should be able to add a user to permissions of a track and delete it again' do
|
43
|
+
track = @sc.Track.find(:one, :from => '/users/api-test-1/tracks/static-test-track')
|
44
|
+
|
45
|
+
old_count = track.permissions.count
|
46
|
+
|
47
|
+
track.permissions << @api_test_3
|
48
|
+
track.permissions.save
|
49
|
+
|
50
|
+
track.permissions.count.should be(old_count+1)
|
51
|
+
|
52
|
+
track.permissions.delete( @api_test_3 )
|
53
|
+
track.permissions.save
|
54
|
+
|
55
|
+
track.permissions.count.should be(old_count)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should add, check and remove a favorite to "me"' do
|
59
|
+
@test_track_1.is_favorite?.should be false
|
60
|
+
@test_track_1.add_favorite!
|
61
|
+
@test_track_1.is_favorite?.should be true
|
62
|
+
@test_track_1.remove_favorite!
|
63
|
+
@test_track_1.is_favorite?.should be false
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should be able to download a track' do
|
67
|
+
track = @sc.Track.find(:one, :from => '/users/api-test-2/tracks/track1-2')
|
68
|
+
track.download
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should be able to download a track (unauthenticated)' do
|
72
|
+
usc = Soundcloud.register({:site => soundcloud_site})
|
73
|
+
track = usc.Track.find(:one, :from => '/users/api-test-2/tracks/track1-2')
|
74
|
+
# File.open('/tmp/sc-track', 'w') {|f| f.write( track.download ) }
|
75
|
+
track.download
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should find tracks with a bpm <= 90' do
|
79
|
+
slow_tracks = @sc.Track.find(:all, :params => { "bpm[to]" => "90"} )
|
80
|
+
slow_tracks.each { |track| track.bpm.should be <= 90.0 }
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should belong to a user' do
|
84
|
+
# check against online attribute, to make sure the complete user is loaded, not the nested user
|
85
|
+
@test_track_1.user.online.should_not be nil
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Soundcloud::Models::User" do
|
4
|
+
before(:all) do
|
5
|
+
@sc = Soundcloud.register({:access_token=> valid_oauth_access_token, :site => soundcloud_site})
|
6
|
+
|
7
|
+
@api_test_1 = @sc.User.find('api-test-1')
|
8
|
+
@api_test_2 = @sc.User.find('api-test-2')
|
9
|
+
@api_test_3 = @sc.User.find('api-test-3')
|
10
|
+
# TODO do the before(:something)
|
11
|
+
begin
|
12
|
+
#remove, just in case it's a contact already
|
13
|
+
@api_test_3.remove_contact!
|
14
|
+
rescue
|
15
|
+
#ignore
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should find a user with a specific name' do
|
20
|
+
test_for = 'api-test-2'
|
21
|
+
user = @sc.User.find(test_for)
|
22
|
+
user.username.should == test_for
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should find all api-test users' do
|
26
|
+
test_for = 'api-test'
|
27
|
+
users = @sc.User.find(:all , :params => {:q => test_for})
|
28
|
+
users.count.should be >= 3
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should check if a user has a contact' do
|
32
|
+
@api_test_2.has_contact?(@api_test_3).should be true
|
33
|
+
@api_test_2.has_contact?(@api_test_3.id).should be true
|
34
|
+
@api_test_3.has_contact?(@api_test_1).should be false
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should add, check and remove a contact' do
|
38
|
+
@api_test_3.is_contact?.should be false
|
39
|
+
@api_test_3.add_contact!
|
40
|
+
@api_test_3.is_contact?.should be true
|
41
|
+
@api_test_3.remove_contact!
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should check if a user has a favorite' do
|
45
|
+
track1 = @sc.Track.find('track1-2')
|
46
|
+
track2 = @sc.Track.find('track2-2')
|
47
|
+
@api_test_2.has_favorite?(track1).should be true
|
48
|
+
@api_test_2.has_favorite?(track1.id).should be true
|
49
|
+
@api_test_2.has_favorite?(track2).should be false
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec'
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
4
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
+
|
6
|
+
|
7
|
+
require 'soundcloud'
|
8
|
+
|
9
|
+
Spec::Runner.configure do |config|
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
def soundcloud_site
|
14
|
+
'http://api.sandbox-soundcloud.com'
|
15
|
+
end
|
16
|
+
|
17
|
+
def valid_oauth_access_token
|
18
|
+
access_token = '2OrtU756yB87XrV09LQK5g'
|
19
|
+
access_secret = 'Y0mcUHPlkKmSgVGSdFAqiqnx6noGtgQv5rHYgq6jVk'
|
20
|
+
consumer_token = 'z8DueHG2qA6wrccaei9Lw'
|
21
|
+
consumer_secret = '3AmT3KFGGa17LRCV65HQZ9F1qFEQ8dIGr4DuuK0aY'
|
22
|
+
|
23
|
+
sc_consumer = Soundcloud.consumer(consumer_token,consumer_secret,soundcloud_site)
|
24
|
+
return OAuth::AccessToken.new(sc_consumer, access_token, access_secret)
|
25
|
+
end
|
26
|
+
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: soundcloud-ruby-api-wrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Johannes Wagener
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-22 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: jwagener-oauth-active-resource
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: oauth
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email: johannes@wagener.cc
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- LICENSE
|
46
|
+
- README.rdoc
|
47
|
+
- Rakefile
|
48
|
+
- VERSION.yml
|
49
|
+
- lib/soundcloud.rb
|
50
|
+
- lib/soundcloud/models/base.rb
|
51
|
+
- lib/soundcloud/models/comment.rb
|
52
|
+
- lib/soundcloud/models/event.rb
|
53
|
+
- lib/soundcloud/models/playlist.rb
|
54
|
+
- lib/soundcloud/models/track.rb
|
55
|
+
- lib/soundcloud/models/user.rb
|
56
|
+
- spec/fixtures/test_track.mp3
|
57
|
+
- spec/soundcloud_comment_spec.rb
|
58
|
+
- spec/soundcloud_event_spec.rb
|
59
|
+
- spec/soundcloud_playlist_spec.rb
|
60
|
+
- spec/soundcloud_spec.rb
|
61
|
+
- spec/soundcloud_track_spec.rb
|
62
|
+
- spec/soundcloud_user_spec.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
has_rdoc: true
|
65
|
+
homepage: http://github.com/soundcloud/ruby-api-wrapper
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options:
|
68
|
+
- --charset=UTF-8
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
version:
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: "0"
|
82
|
+
version:
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.2.0
|
87
|
+
signing_key:
|
88
|
+
specification_version: 2
|
89
|
+
summary: TODO
|
90
|
+
test_files:
|
91
|
+
- spec/soundcloud_comment_spec.rb
|
92
|
+
- spec/soundcloud_event_spec.rb
|
93
|
+
- spec/soundcloud_playlist_spec.rb
|
94
|
+
- spec/soundcloud_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
- spec/soundcloud_user_spec.rb
|
97
|
+
- spec/soundcloud_track_spec.rb
|