I have some params from a hash that I would like to send through an email. For the name of my attachment file (an invoice) I would like the name of my shop and the informations about this invoice. When I run my test I receive 9 emails instead of 3 because of my .each but I don't know how to do else, could someone help me to receive only 3 documents with the name and the shop variable.
Here is my controller :
if(params.dig('connection')!= nil)
#GET ALL MY DOCUMENTS
documents = params.dig('connection', 'subscriptions').each_with_object([]) { |x, arr| x['documents'].each { |d| arr << d['url'] } }.to_s
#GET THE NAME OF THE SHOP
shop = params.dig('connection','connector', 'name').to_s
#GET ALL THE INFORMATIONS
names = params.dig('connection', 'subscriptions').each_with_object([]) { |x, arr| x['documents'].each { |d| arr << d['name'] } }.to_s
full_name = JSON.parse(names)
full_name.each do |name| puts "CHAQUE NAME " + name
array_docs = JSON.parse(documents)
bearer_token = request.headers["Authorization"]
array_docs.each do |doc|
decoded_doc = URI.parse(URI.escape(doc)).to_s
tmp_file = Tempfile.new(['invoices', '.pdf'])
tmp_file.binmode
open(decoded_doc, "Authorization" => bearer_token) do |url_file|
tmp_file.write(url_file.read)
tmp_file.rewind
tmp_file.read
attached_file = tmp_file.path
attachment = Array.new
attachment = {filename: shop+".pdf", file: attached_file}
InvoiceMailer.send_invoice(attachment).deliver
end
end
end
end