Class: Ziptedu::ZipcodeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ziptedu/zipcode_parser.rb

Class Method Summary collapse

Class Method Details

.parse(csv_file_path) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ziptedu/zipcode_parser.rb', line 6

def self.parse(csv_file_path)
  # Initialize to an empty array
   zipcodes = []

   # We only care about a subset of columns in the CSV file
   # Map the attributes in the Zipcode class to headers in CSV file
   attr_to_csv_mapping = {
   	zipcode: "Zipcode",
     type: "ZipCodeType",
     city: "City",
    	state: "State",
     latitude: "Lat",
     longitude: "Long"
   }

CSV.foreach(csv_file_path, headers: true) do |data|

 	zipcode = Zipcode.new do |zip|
 		attr_to_csv_mapping.each do |attr, csv_header_name|
 			zip.send "#{attr}=", data[csv_header_name]
 		end
 	end

     zipcodes << zipcode
   end

   zipcodes
end