I need some informations for the best practices in Ruby on Rails, especially with Controller who have to do a lot of things, so, a simple "show" action is now up to lines. I know, it's not really good, and I have specific code in it.
Here is a sample code:
def show
sound = Sound.find(params[:id])
@xml_path = File.dirname(sound.file.path)
s3 = AWS::S3.new(
:access_key_id => 'XXX',
:secret_access_key => 'XXX')
@url = s3.buckets['dev'].objects[sound.file.path[1..-1]].url_for(:read, :expires => 10*60)
if sound.id_job != 0 && sound.transcript_progress != 100
@response = Savon.client("http://srap.php?wsdl").request(:avance) do
soap.body = {
:Jeton => "abcdef",
:ID_job => sound.id_job,
}
end
@response = @response.to_hash
@progress = @response[:avance][:avancement].to_s.split("#")[1]# ID_job received is formed like "OK#123", we keep "123"
if @progress == "Termine"
sound.transcript_progress = 100
elsif @progress == "ERROR"
flash.now[:alert] = "Oups, il semblerait que le fichier soit illisible, ou qu'il n'y ait rien a ecouter !"
elsif @progress != "Queued"
sound.transcript_progress = @response[:avance_response][:avancement].to_s.split("#")[2].split("%")[0].to_i
end
sound.save
end
if sound.transcript_progress == 100 # If transcription finished
# Get XML File URL on the FTP
@xml_path = Savon.client("http://srap.php?wsdl").request(:donneResultat) do
soap.body = {
:Jeton => "XXX",
:FichierSon => sound.id_job
}
end
# Parse XML Path URL on Kimsufi
@xml_path = @xml_path.to_hash[:donne_resultat_transposition_response][:chemin_fichier].to_s.split("#")[2].to_s.split("/")[5]
# Create local directory (/tmp/sounds) for XML Temp Save
if ! File.directory?(Rails.root.to_s + '/tmp/sounds')
Dir.mkdir(Rails.root.to_s + '/tmp/sounds')
end
# Get XML from FTP
ftp=Net::FTP.new
ftp.connect("ftp.com", 21)
ftp.login("XXX", "XXX")
if ftp.closed?
flash.now[:alert] = "Oups, il semblerait qu'il y ait eu un problème ! Merci d'actualiser la page"
else
ftp.passive = true
ftp.chdir('results')
ftp.getbinaryfile(@xml_path, Rails.root.to_s + '/tmp/sounds/' + @xml_path)
ftp.close
end
# Send XML on S3
s3 = AWS::S3.new(
:access_key_id => 'XXX',
:secret_access_key => 'XXX')
@xml_new = (File.dirname(@sound.file.path) + '/' + File.basename(@xml_path))[1..-1]
s3.buckets['dev'].objects[@xml_new].write(Pathname.new(Rails.root.to_s + '/tmp/sounds/' + @xml_path))
@file = s3.buckets['dev'].objects[@xml_new].read()
end
# A lot of logic again, i've not did it yet
end
As you can see, I have a lot of logic here,
I have to check if the transcription is over,
if not, update the progress_bar (@sound.transcript_progress),
if yes, i first have to connect to a soap action to get the XML path, then get the XML via FTP, then stock it to the Amazon S3 (Shitty SOAP, i have to reparse all response...).
In all my action controller, i have to connect on S3 / SOAP / FTP, not
in the same order.. So i i'm thinking to do a class for each, like in
C++, an abstraction. I want the stuff done, i don't care (a lot) how
it's done. But what's the best practice with the MVC? I have to make a
new folder "Class?" A new controller?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…