Created
June 19, 2012 06:46
-
-
Save Pcushing/2952662 to your computer and use it in GitHub Desktop.
work in progress event_manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dependencies | |
require "csv" | |
# Class Definition | |
class EventManager | |
attr_reader :file | |
INVALID_ZIPCODE = "00000" | |
def initialize | |
puts "EventManager Initialized." | |
filename = "event_attendees.csv" | |
@file = CSV.open(filename, {:headers => true, :header_converters => :symbol}) | |
end | |
def print_names | |
@file.each do |line| | |
puts line.inspect | |
# puts line[2] | |
end | |
end | |
def print_numbers | |
@file.each do |line| | |
number = clean_number(line[:homephone]) | |
puts number | |
end | |
end | |
def clean_number(dirty_number) | |
new_number = dirty_number.gsub(/\D/,'') | |
if new_number.length == 10 | |
#Nada | |
elsif new_number.length == 11 | |
if new_number.start_with?("1") | |
new_number = new_number[1..-1] | |
else | |
new_number = "0000000000" | |
end | |
else | |
new_number = "0000000000" | |
end | |
new_number | |
end | |
def print_zipcodes | |
@file.each do |line| | |
zipcode = clean_zipcode(line[:zipcode]) | |
puts zipcode | |
end | |
end | |
def clean_zipcode(dirty_zip) | |
if dirty_zip.to_s.length < 5 | |
if dirty_zip.to_s.rjust(5, "0") == "00000" | |
INVALID_ZIPCODE | |
else | |
dirty_zip.to_s.rjust(5, "0") | |
end | |
else | |
dirty_zip | |
end | |
end | |
def output_data | |
output = CSV.open("event_attendees_clean.csv", "w") | |
@file.each do |line| | |
if @file.lineno == 2 | |
output << line.headers | |
else | |
line[:homephone] = clean_number(line[:homephone]) | |
line[:zipcode] = clean_zipcode(line[:zipcode]) | |
output << line | |
end | |
end | |
end | |
end | |
# Script | |
manager = EventManager.new | |
# manager.print_names | |
# manager.print_numbers | |
# manager.print_zipcodes | |
manager.output_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment