Class: Slcsp::ZipParser

Inherits:
Object
  • Object
show all
Defined in:
lib/slcsp/zip_parser.rb

Overview

parses the csv with zip and save data to Index in the form e.g. => [‘VA 11’, ‘VA 12’], ‘75025’ => [‘TX 1’, ‘TX 2’]

Constant Summary collapse

KEY_FIELDS =
%w[zipcode].freeze
VALUE_FIELDS =
%w[state rate_area].freeze

Instance Method Summary collapse

Constructor Details

#initialize(path, zip_index) ⇒ ZipParser

Returns a new instance of ZipParser.



12
13
14
15
# File 'lib/slcsp/zip_parser.rb', line 12

def initialize(path, zip_index)
  @path = path
  @zip_index = zip_index
end

Instance Method Details

#parse_and_recordObject



17
18
19
20
21
22
23
24
# File 'lib/slcsp/zip_parser.rb', line 17

def parse_and_record
  File.open(@path) do |file|
    CSV.foreach(file, headers: true) do |row|
      key, value = row[KEY_FIELDS.join(' ')], "#{row[VALUE_FIELDS[0]]} #{row[VALUE_FIELDS[1]]}"
      @zip_index.set(key, value)
    end
  end
end