Class: EasyIO
- Inherits:
-
Object
- Object
- EasyIO
- Defined in:
- lib/easyio.rb
Constant Summary collapse
- DefaultOptions =
{ :col_sep => ",", :headers => true, :header_converters => :symbol }
- DefaultSource =
defined?(Rails.env) && Rails.env == "production" ? :s3 : :file_system
Class Method Summary collapse
- .default_options(options) ⇒ Object
- .default_s3_bucket(bucket) ⇒ Object
- .each_row(file, options = {}) ⇒ Object
- .file_source(options = {}) ⇒ Object
- .get_s3_bucket(options = {}) ⇒ Object
- .open_csv(file, options = {}) ⇒ Object
- .parse(string, options = {}) ⇒ Object
- .write_csv(file, options = {}) ⇒ Object
- .write_file(file) ⇒ Object
Class Method Details
.default_options(options) ⇒ Object
58 59 60 |
# File 'lib/easyio.rb', line 58 def self.() DefaultOptions.merge() end |
.default_s3_bucket(bucket) ⇒ Object
7 8 9 |
# File 'lib/easyio.rb', line 7 def self.default_s3_bucket(bucket) @default_bucket = bucket end |
.each_row(file, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/easyio.rb', line 27 def self.each_row(file, = {}) if file_source() == :s3 require 'aws/s3' require 'open-uri' url = AWS::S3::S3Object.url_for(file, get_s3_bucket()) open(url) do |f| f.each_line do |line| CSV.parse(line, ()) do |row| yield row end end end else open_csv(file, ) do |csv| csv.each do |row| yield row end end end end |
.file_source(options = {}) ⇒ Object
23 24 25 |
# File 'lib/easyio.rb', line 23 def self.file_source( = {}) .delete(:source) || DefaultSource end |
.get_s3_bucket(options = {}) ⇒ Object
54 55 56 |
# File 'lib/easyio.rb', line 54 def self.get_s3_bucket( = {}) .delete(:bucket) || @default_bucket end |
.open_csv(file, options = {}) ⇒ Object
19 20 21 |
# File 'lib/easyio.rb', line 19 def self.open_csv(file, = {}) CSV.open(file, ()) { |csv| yield csv } end |
.parse(string, options = {}) ⇒ Object
48 49 50 51 52 |
# File 'lib/easyio.rb', line 48 def self.parse(string, = {}) CSV.parse(string, ()) do |row| yield row end end |
.write_csv(file, options = {}) ⇒ Object
15 16 17 |
# File 'lib/easyio.rb', line 15 def self.write_csv(file, = {}) CSV.open(file, "w", ) { |csv| yield csv } end |
.write_file(file) ⇒ Object
11 12 13 |
# File 'lib/easyio.rb', line 11 def self.write_file(file) File.open(file, "w") { |f| yield f } end |