lib/soundcloud/models/track.rb in soundcloud-ruby-api-wrapper-0.1.6 vs lib/soundcloud/models/track.rb in soundcloud-ruby-api-wrapper-0.1.8

- old
+ new

@@ -1,17 +1,53 @@ module Soundcloud module Models # 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: + # # http://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 first song from 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? @@ -29,15 +65,16 @@ # # # 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.set_asset_data(File.new('some_sound_file.wav') + # new_track.asset_data = File.new('some_sound_file.wav') # new_track.save # - # # downloads the original soundfile. some_sound_file.wav and another_sound_file.wav should be equal - # File.open('another_sound_file.wav', 'w') {|f| f.write( new_track.download ) } + # # 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 @@ -50,19 +87,25 @@ belongs_to :user has_many :permissions, :comments can_be_a_single_changeable :favorite cattr_accessor :element_name + attr_accessor :asset_data self.element_name = 'track' - def download + def download_url raise Exception.new('Track is not downloadable') if not downloadable - begin - response = connection.handle_response( self.class.oauth_connection.get(download_url) ) - rescue ActiveResource::Redirection => redirection - response = Net::HTTP.get(URI.parse(redirection.response['Location'])) + 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 - return response end # multipart stuff, to upload a soundfile @asset_data = nil