export_to_cloud 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +33 -0
- data/Rakefile +27 -0
- data/lib/export_to_cloud.rb +61 -0
- data/lib/export_to_cloud/version.rb +3 -0
- data/lib/tasks/export_to_cloud_tasks.rake +4 -0
- metadata +144 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
export\_to_cloud
|
2
|
+
================
|
3
|
+
|
4
|
+
export\_to_cloud provides a simple way to export all rows of an ActiveRecord model in Rails to Amazon S3 as a time-stamped single CSV file.
|
5
|
+
|
6
|
+
Usage
|
7
|
+
=====
|
8
|
+
|
9
|
+
### Require the gem
|
10
|
+
|
11
|
+
gem 'export_to_cloud', :git => "git://github.com/mattfordham/export_to_cloud.git"
|
12
|
+
|
13
|
+
### Configure
|
14
|
+
|
15
|
+
Create an initializer in 'config/initializers' called something like 'export\_to_cloud.rb'. Set the following config variables:
|
16
|
+
|
17
|
+
ExportToCloud.aws_access_key_id = "your_access_id"
|
18
|
+
ExportToCloud.aws_secret_access_key = "your_secret_key"
|
19
|
+
ExportToCloud.s3_bucket = "your_bucket_name"
|
20
|
+
ExportToCloud.path = "path/to/directory/in/bucket/"
|
21
|
+
|
22
|
+
### Call
|
23
|
+
|
24
|
+
Call the method on any of your models, like so...
|
25
|
+
|
26
|
+
Person.export_to_cloud
|
27
|
+
|
28
|
+
I usually make this call in a cron job or manually via the console.
|
29
|
+
|
30
|
+
To-do
|
31
|
+
=====
|
32
|
+
|
33
|
+
Add some tests. If anyone has an idea how to pull if off, given the S3 integration, I'm all ears :)
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'ExportToCloud'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require 'fog'
|
3
|
+
|
4
|
+
module ExportToCloud
|
5
|
+
|
6
|
+
mattr_accessor :aws_access_key_id, :aws_secret_access_key, :s3_bucket, :path
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
def export_to_cloud
|
15
|
+
ExportToCloud::create_local_csv_file(self)
|
16
|
+
ExportToCloud::send_to_s3(self)
|
17
|
+
File.delete("#{Rails.root}/tmp/#{self.to_s}.csv")
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.create_local_csv_file(model_to_export)
|
23
|
+
columns = model_to_export.column_names.collect {|x| x.titleize}
|
24
|
+
|
25
|
+
file = File.open("#{Rails.root}/tmp/#{model_to_export.to_s}.csv", 'w') do |f|
|
26
|
+
f.puts columns.to_csv
|
27
|
+
model_to_export.find_each do |record|
|
28
|
+
values = []
|
29
|
+
record.attribute_names.each do |attr_name|
|
30
|
+
value = record[attr_name]
|
31
|
+
value = value.strftime('%Y-%m-%d at %I:%M%p') if value.respond_to?(:strftime)
|
32
|
+
values.push value
|
33
|
+
end
|
34
|
+
f.puts values.to_csv
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.send_to_s3(model_to_export)
|
40
|
+
connection = Fog::Storage.new({
|
41
|
+
:provider => 'AWS',
|
42
|
+
:aws_access_key_id => ExportToCloud.aws_access_key_id,
|
43
|
+
:aws_secret_access_key => ExportToCloud.aws_secret_access_key
|
44
|
+
})
|
45
|
+
|
46
|
+
directory = connection.directories.get(ExportToCloud.s3_bucket)
|
47
|
+
path = "#{ExportToCloud.path}" if ExportToCloud.path.present?
|
48
|
+
filename = "#{model_to_export.to_s.pluralize.downcase}-#{Time.now.to_formatted_s(:number)}.csv"
|
49
|
+
|
50
|
+
file = directory.files.create(
|
51
|
+
:key => "#{path}#{filename}",
|
52
|
+
:body => File.open("#{Rails.root}/tmp/#{model_to_export.to_s}.csv"),
|
53
|
+
:public => true
|
54
|
+
)
|
55
|
+
|
56
|
+
file.save
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
ActiveRecord::Base.send(:include, ExportToCloud)
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: export_to_cloud
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matt Fordham
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-04-18 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: fog
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 3.2.3
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: sqlite3
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rspec-rails
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: guard
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *id005
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: guard-rspec
|
73
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
type: :development
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: *id006
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: ruby-debug19
|
84
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: *id007
|
93
|
+
description: Adds a class method to all ActiveRecord models in Rails that exports all rows in a single CSV file to an Amazon S3 bucket
|
94
|
+
email:
|
95
|
+
- matt@revolvercreative.com
|
96
|
+
executables: []
|
97
|
+
|
98
|
+
extensions: []
|
99
|
+
|
100
|
+
extra_rdoc_files: []
|
101
|
+
|
102
|
+
files:
|
103
|
+
- lib/export_to_cloud/version.rb
|
104
|
+
- lib/export_to_cloud.rb
|
105
|
+
- lib/tasks/export_to_cloud_tasks.rake
|
106
|
+
- MIT-LICENSE
|
107
|
+
- Rakefile
|
108
|
+
- README.md
|
109
|
+
has_rdoc: true
|
110
|
+
homepage: https://github.com/mattfordham/export_to_cloud
|
111
|
+
licenses: []
|
112
|
+
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
hash: 1265487334413498692
|
124
|
+
segments:
|
125
|
+
- 0
|
126
|
+
version: "0"
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
hash: 1265487334413498692
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
version: "0"
|
136
|
+
requirements: []
|
137
|
+
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 1.6.2
|
140
|
+
signing_key:
|
141
|
+
specification_version: 3
|
142
|
+
summary: Exports all rows of a Rails model as a CSV file to Amazon S3
|
143
|
+
test_files: []
|
144
|
+
|