Module: Eaternet::Lives_1_0::CsvParser

Included in:
Agencies::Austin, Agencies::Lives2, Agencies::Nyc, Agencies::Sf, Agencies::Snhd
Defined in:
lib/eaternet/lives_1_0/csv_parser.rb

Instance Method Summary collapse

Instance Method Details

#adapter_nameObject



4
5
6
# File 'lib/eaternet/lives_1_0/csv_parser.rb', line 4

def adapter_name
  fail 'Override #adapter_name with a name for log output, e.g. "SF"'
end

#convert(csv:, to_type:) ⇒ Object



12
13
14
# File 'lib/eaternet/lives_1_0/csv_parser.rb', line 12

def convert(csv:, to_type:)
  csv_map(csv) { |row| try_to_create to_type, from_csv_row: row }.compact
end

#csv_map(filename) ⇒ Object



22
23
24
# File 'lib/eaternet/lives_1_0/csv_parser.rb', line 22

def csv_map(filename)
  csv_rows(filename).lazy.map { |row| yield(row) }
end

#csv_rows(filename) ⇒ Object



26
27
28
# File 'lib/eaternet/lives_1_0/csv_parser.rb', line 26

def csv_rows(filename)
  csv_reader(path: File.join(zip_dir, filename))
end

#map_csvObject



16
17
18
19
20
# File 'lib/eaternet/lives_1_0/csv_parser.rb', line 16

def map_csv
  CSV.new(File.open(table_file, encoding: 'utf-8'), headers: true)
     .lazy
     .map { |row| yield(row) }
end

#try_to_create(entity_type, from_csv_row:) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/eaternet/lives_1_0/csv_parser.rb', line 34

def try_to_create(entity_type, from_csv_row:)
  send(entity_type, from_csv_row)
rescue ArgumentError => e
  logger.warn("#{adapter_name} LIVES adapter") do
    "Couldn't create a LIVES #{entity_type} from #{from_csv_row.inspect}: #{e}"
  end
  nil
end

#zip_dirObject



30
31
32
# File 'lib/eaternet/lives_1_0/csv_parser.rb', line 30

def zip_dir
  Util.download_and_extract_zipfile(zip_file_url)
end

#zip_file_urlObject



8
9
10
# File 'lib/eaternet/lives_1_0/csv_parser.rb', line 8

def zip_file_url
  fail 'Override #zip_file_url with the zip archive location'
end