Created
December 27, 2012 23:51
-
-
Save anonymous/4393292 to your computer and use it in GitHub Desktop.
This file contains 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
require 'csv' | |
require 'sunlight' | |
require 'date' | |
class EventManager | |
INVALID_ZIPCODE = "00000" | |
INVALID_PHONENUMBER = "0000000000" | |
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[:first_name] + " " + line[:last_name] | |
end | |
end | |
def clean_phonenumber(original) | |
number = original.to_s.delete!("./ -/(/)") | |
if number.length == 10 | |
number | |
elsif number.length == 11 | |
if number.start_with?("1") | |
number = number[1..-1] | |
else | |
INVALID_PHONENUMBER | |
end | |
else | |
INVALID_PHONENUMBER | |
end | |
end | |
def print_numbers | |
@file.each do |line| | |
number = clean_phonenumber(line[:homephone]) | |
puts number | |
end | |
end | |
def clean_zipcodes(original) | |
number = original | |
if number.to_s.length == 5 | |
number | |
elsif number.to_s.length == 4 | |
"0" + number | |
else | |
INVALID_ZIPCODE | |
end | |
end | |
def print_zipcodes | |
@file.each do |line| | |
number = clean_zipcodes(line[:zipcode]) | |
puts number | |
end | |
end | |
def output_data | |
output = CSV.open("event_attendees_clean.csv", "w") | |
@file.each do |line| | |
begin | |
if @file.lineno == 2 | |
output << line.headers | |
else | |
line[:homephone] = clean_phonenumber(line[:homephone]) | |
#puts line[:zipcode] | |
line[:zipcode] = clean_zipcodes(line[:zipcode]) | |
#puts line[:zipcode].to_i | |
output << line | |
end | |
rescue Exception => e | |
#puts e.to_s | |
end | |
end | |
end | |
end | |
manager = EventManager.new | |
puts manager.output_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment