Skip to content

Instantly share code, notes, and snippets.

@jessieay
Created June 20, 2012 23:55
Show Gist options
  • Save jessieay/2963017 to your computer and use it in GitHub Desktop.
Save jessieay/2963017 to your computer and use it in GitHub Desktop.
Event manager ruby project created in week 2 at DBC
# Dependencies
require "csv"
require 'sunlight'
# Class Definition
class EventManager
INVALID_ZIPCODE = "00000"
INVALID_PHONE_NUMBER = "0000000000"
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
# if clean_number != "0000000000"
# puts clean_number
# end
def clean_number(original)
if original.nil?
INVALID_PHONE_NUMBER
elsif original.length == 10
elsif original.length == 11
if original.start_with?("1")
original = original[1..-1]
else
original = "000000000"
end
else
original = "0000000000"
end
cleaned_number = original.gsub(/[^0-9]/, '')
return cleaned_number
end
def print_numbers
@file.each do |line|
number = clean_number(line[:homephone])
puts number
end
end
def clean_zipcode(original_zip)
if original_zip.nil?
INVALID_ZIPCODE
elsif original_zip.length < 5
cleaned_zip = original_zip.rjust(5, '0')
else
cleaned_zip = original_zip
end
cleaned_zip
end
def print_zipcodes
@file.each do |line|
zipcode = clean_zipcode(line[:zipcode])
puts zipcode
end
end
def output_data(filename)
output = CSV.open("event_attendees_clean.csv", "w")
@file.each do |line|
line[:homephone] = clean_number(line[:homephone])
line[:zipcode] = clean_zipcode(line[:zipcode])
if @file.lineno == 2
output << line.headers
else
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]))
names = legislators.collect do |leg|
first_name = leg.firstname
first_initial = first_name[0]
last_name = leg.lastname
first_initial + ". " + last_name
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
custom_letter = letter.gsub("#first_name", "#{line[:first_name]}".capitalize)
custom_letter = custom_letter.gsub("#last_name", "#{line[:last_name]}".capitalize)
custom_letter = custom_letter.gsub("#street", "#{line[:street]}".capitalize)
custom_letter = custom_letter.gsub("#city", "#{line[:city]}".capitalize)
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|
timestamp_line = line[:regdate]
timestamp_split = timestamp_line.split
timestamp_hour_only = timestamp_split[1].split(":")
hour = timestamp_hour_only[0].to_i
hours[hour] += 1
end
hours.each_with_index{|counter,hour| puts "#{hour}\t#{counter}"}
end
def day_stats
days = Array.new(8){0}
@file.each do |line|
# if Date.valid_date?(line[:regdate])
just_date = (line[:regdate]).split
begin
date = DateTime.parse(just_date[0])
new_date = date.wday
days[new_date] += 1
rescue
days[7] += 1
end
end
# end
days.each_with_index{|counter,date| puts "#{date}\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.state_stats
# manager.output_data("event_attendees_clean.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment