lib/soundcloud/models/track.rb in soundcloud-ruby-api-wrapper-0.2.1 vs lib/soundcloud/models/track.rb in soundcloud-ruby-api-wrapper-0.3.1
- old
+ new
@@ -85,11 +85,10 @@
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_url
raise Exception.new('Track is not downloadable') if not downloadable
original_download_url = super
@@ -104,34 +103,47 @@
return original_download_url
end
end
# multipart stuff, to upload a soundfile
-
- @asset_data = nil
+
def set_asset_data(file)
- @asset_data = file
+ self.asset_data = file
end
def update
- if not @asset_data.nil?
+ if not self.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
def create
- if @asset_data.nil?
+ if self.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))
+ #post asset_data
+
+ # default to private
+ if self.sharing?.nil?
+ self.sharing = 'private'
+ end
+
+ params = ActiveSupport::OrderedHash.new
+ self.attributes.reject { |k,v| k.to_s == 'asset_data'}.each { |k,v|
+ params["track[#{k}]".to_sym] = v
+ }
+
+ # ignore is added because the multipart gem is adding an extra new line
+ # to the last parameter which will break parsing of track[sharing]
+ params[:ignore] = 'multipart bug'
+
+ response = connection.handle_response(self.class.send_multipart_request(:post,'/tracks','track[asset_data]',self.asset_data,params))
+
self.id = id_from_response(response)
- @asset_data = nil
- # second, 'normal' update request
- update
+ load_attributes_from_response(response)
+ self.asset_data = nil
end
end
end
end
end