Created
June 20, 2012 01:24
-
-
Save dkan/2957563 to your computer and use it in GitHub Desktop.
event_manager
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
# Dependencies | |
require "csv" | |
require "sunlight" | |
require "date" | |
# Class Definition | |
class EventManager | |
INVALID_ZIPCODE = "00000" | |
INVALID_PHONE_NUMBER = "0"*10 | |
Sunlight::Base.api_key = "e179a6973728c4dd3fb1204283aaccb5" | |
def initialize(filename) | |
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 print_numbers | |
@file.rewind | |
@file.each do |line| | |
puts clean_number(line[:homephone]) | |
end | |
end | |
def clean_number(number) | |
number.delete! "-() \." | |
if number.length == 10 | |
# Do Nothing | |
elsif number.length == 11 | |
if number.start_with?("1") | |
number = number[1..-1] | |
else | |
number = INVALID_PHONE_NUMBER | |
end | |
else | |
number = INVALID_PHONE_NUMBER | |
end # Insert your "cleaning" code here | |
return number # Send the variable 'number' back to the method that called this method | |
end | |
def print_zipcodes | |
@file.rewind | |
@file.each do |line| | |
puts clean_zipcode(line[:zipcode]) | |
end | |
end | |
def clean_zipcode(original) | |
if original.nil? | |
original = INVALID_ZIPCODE | |
elsif original.length < 5 | |
original = original.rjust(5, "0") | |
else | |
# Do nothing | |
end | |
return original | |
end | |
def output_data(output_name) | |
output = CSV.open(output_name, "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" | |
#API lookup goes here | |
legislators = Sunlight::Legislator.all_in_zipcode(clean_zipcode(line[:zipcode])) | |
names = legislators.collect do |leg| | |
"#{leg.title}. #{leg.firstname[0]}. #{leg.lastname} (#{leg.party})" | |
end | |
puts "#{line[:last_name]}, #{line[:first_name]}, #{line[:zipcode]}, #{names.join(", ")}" | |
end | |
end | |
def create_form_letters | |
letter = File.open("form_letter.html", "r").read | |
20.times do | |
line = @file.readline | |
# Do your string substitutions here | |
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("#zipcode","#{line[:zipcode]}") | |
custom_letter = custom_letter.gsub("#state","#{line[:state]}") | |
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| | |
timestamp = line[:regdate] | |
hour = timestamp.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 rank_day | |
days = Array.new(7){0} | |
@file.each do |line| | |
timestamp = line[:regdate] | |
date = Date.strptime(timestamp.split[0], "%m/%d/%y") | |
day = date.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] # Find the State | |
if state_data[state].nil? # Does the state's bucket exist in state_data? | |
state_data[state] = 1 # If that bucket was nil then start it with this one person | |
else | |
state_data[state] = state_data[state] + 1 # If the bucket exists, add one | |
end | |
end | |
state_data = state_data.select{|state, counter| state}.sort_by{|state, counter| counter unless state.nil?} | |
state_data.each do |state,counter| | |
puts "#{state}: #{counter}" | |
end | |
end | |
end | |
# Script | |
manager = EventManager.new('clean_data.csv') | |
#manager.print_names | |
#manager.print_numbers | |
#manager.print_zipcodes | |
#manager.output_data('clean_data.csv') | |
# manager.rep_lookup | |
# manager.create_form_letters | |
# manager.rank_day | |
manager.state_stats |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment