Skip to content

Instantly share code, notes, and snippets.

@Pcushing
Created June 20, 2012 00:07
Show Gist options
  • Save Pcushing/2957263 to your computer and use it in GitHub Desktop.
Save Pcushing/2957263 to your computer and use it in GitHub Desktop.
Reading a messy file of names and doing something useful with it (cleaning, legislator lookup). Worked with Tom.
# Dependencies
require "csv"
require "sunlight"
# Class Definition
class EventManager
INVALID_ZIPCODE = "00000"
Sunlight::Base.api_key = "e179a6973728c4dd3fb1204283aaccb5"
def initialize(filename)
puts "EventManager Initialized."
@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 print_numbers
@file.each do |line|
number = clean_number(line[:homephone])
puts number
end
end
def clean_number(number)
clean_number = number.gsub(/\D/ , "")
if number.length == 10
# Do Nothing
elsif number.length == 11
if number.start_with?("1")
clean_number = number[1..-1]
else
clean_number = "0000000000"
end
else
clean_number = "0000000000"
end
clean_number
end
def clean_zipcode(original)
if original.to_s.length < 5
if original.nil?
INVALID_ZIPCODE
else
original.to_s.rjust(5, '0')
end
else
original
end
end
def print_zipcodes
@file.each do |line|
zipcode = clean_zipcode(line[:zipcode])
puts zipcode
end
end
def output_data(filename)
output = CSV.open(filename, "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
def rep_lookup
20.times do
line = @file.readline
representative = "unknown"
legislators = Sunlight::Legislator.all_in_zipcode(clean_zipcode(line[:zipcode]))
legislator_names = legislators.collect do |leg|
first_name = leg.firstname
first_initial = first_name[0]
last_name = leg.lastname
legislator_party = leg.party
legislator_title = leg.title
legislator_title + " " + first_initial + ". " + last_name + " (" + legislator_party + ")"
end
puts "#{line[:last_name]}, #{line[:first_name]}, #{line[:zipcode]}, #{legislator_names.join(", ")}"
end
end
def create_form_letters
letter = File.open("form_letter.html", "r").read
20.times do
line = @file.readline
custom_letter = letter.gsub("#first_name", "#{line[:first_name]}")
custom_letter = custom_letter.gsub("#last_name", "#{line[:last_name]}")
custom_letter = custom_letter.gsub("#street", "#{line[:street]}")
custom_letter = custom_letter.gsub("#city", "#{line[:city]}")
custom_letter = custom_letter.gsub("#state", "#{line[:state]}")
custom_letter = custom_letter.gsub("#zipcode", "#{line[:zipcode]}")
filename = "output/thanks_#{line[:last_name]}_#{line[:first_name]}.html"
output = File.new(filename, 'w')
output.write(custom_letter)
end
end
def rank_times
hours = Array.new(24){0}
@file.each do |line|
hour = line[:regdate].split(" ")[1].split(":")[0]
hours[hour.to_i] = hours[hour.to_i] + 1
end
hours.each_with_index{ |counter, hour| puts "#{hour}\t#{counter}"}
end
def day_stats
days = Array.new(7){0}
@file.each do |line|
date = line[:regdate].split(" ")[0]
day = Date.strptime(date, "%m/%d/%y").wday
days[day.to_i] = days[day.to_i] + 1
end
days.each_with_index{ |counter, day| puts "#{day}\t#{counter}"}
end
def state_stats
state_data = {}
@file.each do |line|
state = line[:state]
if state_data[state].nil?
state_data[state] = 1
else
state_data[state] = state_data[state] + 1
end
end
ranks = state_data.sort_by{|state, counter| -counter}.collect{|state, counter| state}
state_data = state_data.select{|state, counter| state}.sort_by{|state, counter| state}
state_data.each do |state,counter|
puts "#{state}:\t#{counter}\t(#{ranks.index(state) + 1})"
end
end
end
# Script
manager = EventManager.new("event_attendees.csv")
# manager.rep_lookup
# manager.output_data("event_attendees_clean.csv")
manager.state_stats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment