Sha256: 2511f44235290f500bb90a9c812d1f5b7ceb6f6dccd34a5d55beed7362af9346

Contents?: true

Size: 760 Bytes

Versions: 1

Compression:

Stored size: 760 Bytes

Contents

require 'csv'

module Ziptedu
  class ZipcodeParser

  	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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ziptedu-0.1.0 lib/ziptedu/zipcode_parser.rb