lib/echonest/api.rb in ruby-echonest-0.3.0 vs lib/echonest/api.rb in ruby-echonest-0.4.0
- old
+ new
@@ -11,10 +11,11 @@
USER_AGENT = '%s/%s' % ['ruby-echonest', ::Echonest::VERSION]
include TraditionalApiMethods
class Error < StandardError; end
+ class UnsupportedFiletypeError < StandardError; end
attr_reader :user_agent
def initialize(api_key = nil)
@api_key = api_key || read_api_key_from_file
@@ -104,11 +105,11 @@
def upload(options)
options.update(:bucket => 'audio_summary')
if options.has_key?(:filename)
filename = options.delete(:filename)
- filetype = filename.to_s.match(/\.(mp3|au|ogg)$/)[1]
+ filetype = parse_filetype(filename)
open(filename) do |f|
@api.request('track/upload',
:post,
options.merge(:filetype => filetype),
@@ -160,9 +161,23 @@
when 'unavailable'
analyze(:md5 => md5)
end
sleep 5
+ end
+ end
+
+ private
+
+ def parse_filetype(filename)
+ filetype = filename.to_s.match(/\.(wav|mp3|au|ogg|m4a|mp4|aif|aiff)$/)
+
+ if !filetype
+ raise Api::UnsupportedFiletypeError
+ elsif ['aif', 'aiff'].include? filetype[1]
+ 'wav'
+ else
+ filetype[1]
end
end
end
end
end