Downloading a file from a URL and uploading it automatically

require ‘net/http’

#this will create a folder, we can run any commands using ‘system’ followed by commands in the rails coding itself like below one.
system “mkdir #{RAILS_ROOT}/public/attachments/twilio/#{ch.id}”
a=ch.record_url
a=a.split(‘http://api.twilio.com’)
Net::HTTP.start(“api.twilio.com”) { |http|
resp = http.get(“#{a[1]}”)
open(“#{RAILS_ROOT}/public/attachments/twilio/#{ch.id}/#{ch.id}.wav”, “wb”) { |file|
file.write(resp.body)
}

Advertisements