Skip to content

Instantly share code, notes, and snippets.

@kennyt
Created October 27, 2012 09:47
Show Gist options
  • Save kennyt/3963816 to your computer and use it in GitHub Desktop.
Save kennyt/3963816 to your computer and use it in GitHub Desktop.
event_manager
require "csv"
require "sunlight"
class EventManager
INVALD_ZIPCODE = '00000'
INVALID_PHONENUMBER = '0000000000'
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[:first_name]} #{line[:last_name]}"
end
end
def print_numbers
@file.each do |line|
number = clean_numbers(line[:homephone])
puts number
end
end
def clean_numbers (original)
number = original.delete('.').delete('(').delete(')').delete('-').delete(' ')
if number.length == 10
elsif number.length == 11
if number.start_with?(1)
number = number[1..-1]
else
number = INVALID_PHONENUMBER
end
else
number = INVALID_PHONENUMBER
end
number
end
def print_zipcodes
@file.each do |line|
zipcode = clean_zipcodes(line[:zipcode])
puts zipcode
end
end
def clean_zipcodes (original)
return INVALD_ZIPCODE if original.nil?
until original.length == 5 do
original = (original.scan(/./).reverse<<'0').reverse.join
end
original
end
def output_data (filename)
output = CSV.open(filename, "w")
@file.each do |line|
output << line if @file.lineno == 2
line[:homephone] = clean_numbers(line[:homephone])
line[:zipcode] = clean_zipcodes(line[:zipcode])
output << line
end
end
def rep_lookup
20.times do
line = @file.readline
legislators = Sunlight::Legislator.all_in_zipcode(clean_zipcodes(line[:zipcode]))
title_name_party = legislators.collect do |leg|
first_name = leg.firstname
first_initial = first_name[0]
last_name = leg.lastname
name_together = first_initial + ". " + last_name
party = leg.party[0]
title = leg.title[0..2]
"#{title} #{name_together} (#{party})"
end
representative = "unknown"
puts "#{line[:last_name]}, #{line[:first_name]}, #{line[:zipcode]}, #{title_name_party.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.to_s.split("/")[2].split(":")[0].split(" ")[1]
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|
line_date = line.to_s.split(",")[1].split(" ")[0]
date = Date.strptime(line_date, "%m/%d/%y").wday
days[date.to_i] = days[date.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 unless state.nil?}
state_data.each do |state, counter|
puts "#{state}:\t #{counter}\t(#{ranks.index(state)+1})"
end
end
end
manager = EventManager.new("event_attendees.csv")
manager.state_stats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment