Skip to content

Instantly share code, notes, and snippets.

@chadwickdonald
Created June 20, 2012 22:04
Show Gist options
  • Save chadwickdonald/2962506 to your computer and use it in GitHub Desktop.
Save chadwickdonald/2962506 to your computer and use it in GitHub Desktop.
event manager
require "csv"
require "sunlight"
class EventManager
INVALID_ZIPCODE = "0"*5
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_number(line[:homephone])
end
end
def clean_number(original)
clean_number = original.delete(".")
clean_number.delete!("-")
clean_number.delete!(" ")
clean_number.delete!("(")
clean_number.delete!(")")
if clean_number.length == 10
#good
elsif clean_number.length == 11
if clean_number.start_with?("1")
clean_number = clean_number[1..-1]
else
clean_number = "0"*10
end
else
clean_number = "0"*10
end
clean_number
end
def print_zipcodes
@file.each do |line|
zipcode = clean_zipcode(line[:zipcode])
end
end
def clean_zipcode(original)
if original == nil
original = INVALID_ZIPCODE
elsif original.length < 5
clean_zipcode(original.insert(0,"0"))
end
original
end
def rep_lookup
20.times do
line = @file.readline
legislators = Sunlight::Legislator.all_in_zipcode(clean_zipcode(line[:zipcode]))
names = legislators.collect do |leg|
first_name = leg.firstname
first_initial = first_name[0]
last_name = leg.lastname
first_initial + ". " + last_name
end
representative = "unknown"
# API Lookup Goes Here
puts "#{line[:last_name]}, #{line[:first_name]}, #{line[:zipcode]}, #{names.join(", ")}"
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 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("#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|
# Do the counting here
date_and_time = line[:regdate].split(" ")
time = date_and_time[1]
colon = time.index(":")
hour = time[0,colon]
#puts hour
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|
day = Date.strptime(line[:regdate], "%m/%d/%y").wday
puts day.inspect
days[day] = days[day] + 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 |key,value|
# puts "#{key}: #{value}"
#
# 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.print_names
#manager.print_numbers
#manager.output_data("event_attendees_clean.csv")
manager = EventManager.new("event_attendees_clean.csv")
#manager.rep_lookup
#manager.create_form_letters
#manager.rank_times
#manager.day_stats
manager.state_stats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment