Class: Soundcloud::Models::Track

Inherits:
Base
  • Object
show all
Defined in:
lib/soundcloud/models/track.rb

Overview

Look up the resource attributes and filtering usage here:

SC API Attributes (as of 26/05/09):

  • id

  • user_id

  • permalink

  • description

  • sharing

  • bpm

  • comments_count

  • created_at

  • downloadable

  • downloads_count

  • duration

  • genre

  • streamable

  • uri

  • user (overwritten by wrapper)

  • permalink_url

  • playback_count

  • artwork_url

  • waveform_url

  • purchase_url

  • stream_url

  • user_playback_count

  • user_favorite

Custom Wrapper Attributes/Methods:

  • user

  • permissions

  • comments

  • is_favorite?

  • add_favorite!

  • remove_favorite!

  • asset_data (= File.new(‘/your file..’))

Look up the resource attributes and filtering usage here:

wiki.github.com/soundcloud/api/documentation#track

Examples:

# gets 50 (Soundcloud API limit) tracks from some_user
some_user_tracks = some_user.tracks

# gets the latest song from some_user_tracks
first_song = some_user_tracks.first

# prints 50 (Soundcloud API limit) comments of first_song with username, timestamp (can be nil) and comment body
first_song.comments.each do |comment| 
  if comment.timestamp.nil?
    timestamp = ""
  else
    timestamp = "@#{comment.timestamp/1000}"
  end
  p "#{comment.user.username} #{timestamp}: #{comment.body}"
end

# gets 50 (Soundcloud API limit) tracks with a BPM <= 100    
slow_tracks  = sc_client.Track.find(:all, :params => { "bpm[to]" => "100"} )

# create a new Track on Soundcloud with some_sound_file.mp3 as asset data    
new_track = sc_client.Track.new
new_track.title = 'New Track'
new_track.sharing = 'private'
new_track.asset_data = File.new('some_sound_file.wav')
new_track.save

# downloads the original soundfile. 
#some_sound_file.wav and downloaded_file should be the same (the open call requires the 'open-uri' gem)
downloaded_file = open new_track.download_url

# gives some_user permission to access the new_track    
some_user = sc_client.User.find('some_user')
new_track.permissions << some_user
new_track.permissions.save

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

can_be_a_single_changeable, has_many_single_changeable

Instance Attribute Details

#asset_dataObject

Returns the value of attribute asset_data.



92
93
94
# File 'lib/soundcloud/models/track.rb', line 92

def asset_data
  @asset_data
end

Instance Method Details

#createObject



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/soundcloud/models/track.rb', line 125

def create
    if @asset_data.nil?
      super
    else
     #post asset_data            
     params = {'track[title]' => self.title,'track[sharing]' => self.sharing}
     response = connection.handle_response(self.class.send_multipart_request(:post,'/tracks','track[asset_data]',@asset_data,params))
     self.id = id_from_response(response)
     @asset_data = nil
     # second, 'normal' update request
     update
  end
end

#download_urlObject

Raises:

  • (Exception)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/soundcloud/models/track.rb', line 95

def download_url
  raise Exception.new('Track is not downloadable') if not downloadable
  original_download_url = super       
  if sharing == "private" 
    begin
      response = connection.handle_response( self.class.oauth_connection.get( original_download_url ) )
      return original_download_url
    rescue ActiveResource::Redirection => redirection          
      return redirection.response['Location']
    end
  else
    return original_download_url
  end
end

#set_asset_data(file) ⇒ Object



113
114
115
# File 'lib/soundcloud/models/track.rb', line 113

def set_asset_data(file)
  @asset_data = file          
end

#updateObject



117
118
119
120
121
122
123
# File 'lib/soundcloud/models/track.rb', line 117

def update
  if not @asset_data.nil?
    raise 'Multipart update is NotImplemented'
    self.class.send_multipart_request(:put,'/tracks/#{self.id}','replacement[asset_data]',@asset_data)
  end
  super
end