Skip to content

Instantly share code, notes, and snippets.

Created September 5, 2012 21:58
Show Gist options
  • Save anonymous/d30e2d9b9e11027b9efb to your computer and use it in GitHub Desktop.
Save anonymous/d30e2d9b9e11027b9efb to your computer and use it in GitHub Desktop.
#dependencies
require "csv"
require 'sunlight'
require 'pry'
#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|
# require 'pry'; binding.pry
puts "#{line[:first_name]} #{line[:last_name]} "
# puts line.inspect
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("()-. ")
if number.length == 10
elsif number.length == 11
if number.start_with?("1")
number = number[1..-1]
else
number = "0000000000"
end
else
number = "0000000000"
end
return number
end
def clean_zipcodes(original)
zipcode = "#{INVALID_ZIPCODE}#{original}"
zipcode = zipcode[-5..-1]
return zipcode
end
def print_zipcodes
@file.each do |line|
zipcode = clean_zipcodes(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_numbers(line[:homephone])
line[:zipcode] = clean_zipcodes(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_zipcodes(line[:zipcode]))
names = legislators.collect do |leg|
first_name = leg.firstname
first_initial = first_name[0]
last_name = leg.lastname
party = leg.party
party_initial = party[0]
title = leg.title
title_abbr = title[0..2]
title_abbr +" "+ first_initial + ". " + last_name +" ("+party_initial+")"
end
puts "#{line[:last_name]}, #{line[:last_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]}")
custom_letter = custom_letter.gsub("#last_name",line[:last_name])
filename = "output/thanks_#{line[:last_name]}_#{line[:first_name]}.html"
output = File.new(output/filename,"w")
output.write(custom_letter)
end
end
def output_forms(letter)
end
end
# Script
manager = EventManager.new("event_attendees.csv")
#manager.output_data("event_attendees_cleaned.csv")
manager.create_form_letters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment