Class: EasyIO

Inherits:
Object
  • Object
show all
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

Class Method Details

.default_options(options) ⇒ Object



52
53
54
# File 'lib/easyio.rb', line 52

def self.default_options(options)
  DefaultOptions.merge(options)
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, options = {})
  if file_source(options) == :s3
    require 'aws/s3'
    require 'open-uri'
    url = AWS::S3::S3Object.url_for(file, get_s3_bucket(options))
    open(url) do |f|
      f.each_line do |line|
        FasterCSV.parse(line, default_options(options)) do |row|
          yield row
        end
      end
    end
  else
    open_csv(file, options) 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(options = {})
  options.delete(:source) || DefaultSource
end

.get_s3_bucket(options = {}) ⇒ Object



48
49
50
# File 'lib/easyio.rb', line 48

def self.get_s3_bucket(options = {})
  options.delete(:bucket) || @default_bucket
end

.open_csv(file, options = {}) ⇒ Object



19
20
21
# File 'lib/easyio.rb', line 19

def self.open_csv(file, options = {})
  FasterCSV.open(file, default_options(options)) { |csv| yield csv }
end

.write_csv(file) ⇒ Object



15
16
17
# File 'lib/easyio.rb', line 15

def self.write_csv(file)
  FasterCSV.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