Skip to content

Instantly share code, notes, and snippets.

@StaverDmitry
Created June 7, 2017 01:37
Show Gist options
  • Save StaverDmitry/ed43da82efa482e71e607694029519c4 to your computer and use it in GitHub Desktop.
Save StaverDmitry/ed43da82efa482e71e607694029519c4 to your computer and use it in GitHub Desktop.
require "csv"
class ForecastCSV < Struct.new(:forecast, :current_user_id)
def perform
data = forecast.detect{|f| f[:name] == 'forecast'}[:data]
path = "tmp/Forecast-#{Time.now.strftime("%d-%m-%Y %I:%M-%S")}.csv"
CSV.open(path, "w+") do |csv|
csv << ["date", "value"]
data.each do |date, value|
csv << [date, value]
end
end
obj = Aws::S3::Resource.new
obj = obj.bucket("sail-crm-data").object("reports/Forecast-#{Time.now.strftime("%d-%m-%Y %I:%M-%S")}.csv")
obj.upload_file(path, acl:'public-read')
Pusher.trigger("crm-user-#{current_user_id}", 'report_download', {
message: obj.public_url
})
end
def after(job)
end
handle_asynchronously :perform
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment