Module: ExportToCloud
- Defined in:
- lib/export_to_cloud.rb,
lib/export_to_cloud/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
- .create_local_csv_file(model_to_export) ⇒ Object
- .included(base) ⇒ Object
- .send_to_s3(model_to_export) ⇒ Object
Class Method Details
.create_local_csv_file(model_to_export) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/export_to_cloud.rb', line 22 def self.create_local_csv_file(model_to_export) columns = model_to_export.column_names.collect {|x| x.titleize} file = File.open("#{Rails.root}/tmp/#{model_to_export.to_s}.csv", 'w') do |f| f.puts columns.to_csv model_to_export.find_each do |record| values = [] record.attribute_names.each do |attr_name| value = record[attr_name] value = value.strftime('%Y-%m-%d at %I:%M%p') if value.respond_to?(:strftime) values.push value end f.puts values.to_csv end end end |
.included(base) ⇒ Object
8 9 10 |
# File 'lib/export_to_cloud.rb', line 8 def self.included(base) base.extend(ClassMethods) end |
.send_to_s3(model_to_export) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/export_to_cloud.rb', line 39 def self.send_to_s3(model_to_export) connection = Fog::Storage.new({ :provider => 'AWS', :aws_access_key_id => ExportToCloud.aws_access_key_id, :aws_secret_access_key => ExportToCloud.aws_secret_access_key }) directory = connection.directories.get(ExportToCloud.s3_bucket) path = "#{ExportToCloud.path}" if ExportToCloud.path.present? filename = "#{model_to_export.to_s.pluralize.downcase}-#{Time.now.to_formatted_s(:number)}.csv" file = directory.files.create( :key => "#{path}#{filename}", :body => File.open("#{Rails.root}/tmp/#{model_to_export.to_s}.csv"), :public => true ) file.save end |