From: kevid on
hi all,

Please could some on help me with a code to download an audio file in
ruby.
the code below does not work;;

url = "http://stream.esvmedia.org/mp3-play/hw/19023001-19023006.mp3"

res = Net::HTTP.get_response(URI.parse(url))

puts res.body



From: andrew mcelroy on
[Note: parts of this message were removed to make it a legal post.]

On Wed, Jun 30, 2010 at 12:50 PM, kevid <alumsimportant(a)yahoo.ca> wrote:

> hi all,
>
> Please could some on help me with a code to download an audio file in
> ruby.
> the code below does not work;;
>

Are you trying to stream a MP3 or are you trying to download it no
differently than any other binary format?

Andrew McElroy

>
> url = "http://stream.esvmedia.org/mp3-play/hw/19023001-19023006.mp3"
>
> res = Net::HTTP.get_response(URI.parse(url))
>
> puts res.body
>
>
>
>
>

From: kevid on
hi, tanks for your response.

I am trying to download it and save it locally. the file is in mp3
format.

Regards
Kevid
From: Jonathan Nielsen on
[Note: parts of this message were removed to make it a legal post.]

On Thu, Jul 1, 2010 at 10:05 AM, kevid <alumsimportant(a)yahoo.ca> wrote:

> I am trying to download it and save it locally. the file is in mp3
> format.
>
> What your current code would do is print the contents of the mp3 file to
the console. Since it's all binary data, this is not very useful.

What you need to do is open a file and write the download contents to it.
One way to do this is:

require 'net/http'
url = "http://stream.esvmedia.org/mp3-play/hw/19023001-19023006.mp3"
res = Net::HTTP.get_response(URI.parse(url))
File.open('output.mp3','w') do |f|
f.write(res.body)
end


For more information on writing to files, see
http://ruby-doc.org/core/classes/File.html

-Jonathan Nielsen

From: Jonathan Nielsen on
[Note: parts of this message were removed to make it a legal post.]

And this is probably a more useful tutorial on files in ruby:

http://www.tutorialspoint.com/ruby/ruby_input_output.htm


-Jonathan Nielsen