class EventManager
Public Class Methods
new(input_file = "required_files/event_attendees.csv")
click to toggle source
# File lib/event_manager.rb, line 10 def initialize(input_file = "required_files/event_attendees.csv") puts "EventManager initialized." hours = {} days = {} (1..24).each {|x| hours[x] = 0} (0..6).each {|x| days[x] = 0} Sunlight::Congress.api_key = "e179a6973728c4dd3fb1204283aaccb5" main(input_file, hours, days) end
Private Instance Methods
check_file(name, remote_url)
click to toggle source
# File lib/event_manager.rb, line 103 def check_file(name, remote_url) Dir.mkdir("required_files") unless Dir.exists?("required_files") if(File.file?("required_files/" + name) == false) open("required_files/" + name, "wb") do |file| file << open(remote_url + name).read end end end
clean_phone_number(number)
click to toggle source
# File lib/event_manager.rb, line 89 def clean_phone_number(number) number = number.gsub(/[^0-9a-z ]/i, '').gsub(/\s+/, "") if(number.length < 10 || number.length > 11) number = "none"; elsif(number.length == 11) if(number[0] == "1") number = number[1..-1]; else number = "none"; end end number end
clean_zipcode(zipcode)
click to toggle source
# File lib/event_manager.rb, line 61 def clean_zipcode(zipcode) zipcode.to_s.rjust(5,"0")[0..4] end
legislators_by_zipcode(zipcode)
click to toggle source
# File lib/event_manager.rb, line 65 def legislators_by_zipcode(zipcode) Sunlight::Congress::Legislator.by_zipcode(zipcode) end
main(input_file, hours, days)
click to toggle source
# File lib/event_manager.rb, line 22 def main(input_file, hours, days) remote_url = "http://testappt.comeze.com/files/" check_file("event_attendees.csv", remote_url) if(File.file?(input_file)) contents = CSV.open input_file, headers: true, header_converters: :symbol check_file("form_letter.erb", remote_url) template_letter = File.read("required_files/form_letter.erb") erb_template1 = ERB.new template_letter contents.each do |row| id = row[0] name = row[:first_name] zipcode = clean_zipcode(row[:zipcode]) phone = clean_phone_number(row[:homephone]) regdate = row[:regdate] datetime = DateTime.strptime(regdate , '%m/%d/%Y %H:%M') hours[datetime.hour.to_i] += 1; 0 days[datetime.wday.to_i] += 1; 0 legislators = legislators_by_zipcode(zipcode) form_letter = erb_template1.result(binding) save_letters(id,form_letter) end check_file("form_charts.erb", remote_url) template_charts = File.read("required_files/form_charts.erb") erb_template2 = ERB.new template_charts form_charts = erb_template2.result(binding) save_registration_statistics(form_charts) FileUtils.rm_rf('required_files') puts "Ended. Check the output folder." else puts "Error loading input data file! File not found." end end
save_letters(id, form_letter)
click to toggle source
# File lib/event_manager.rb, line 69 def save_letters(id, form_letter) Dir.mkdir("output") unless Dir.exists?("output") filename = "output/thanks_#{id}.html" File.open(filename,'w') do |file| file.puts form_letter end end
save_registration_statistics(form_charts)
click to toggle source
# File lib/event_manager.rb, line 79 def save_registration_statistics(form_charts) Dir.mkdir("output") unless Dir.exists?("output") filename = "output/registration_statistics.html" File.open(filename,'w') do |file| file.puts form_charts end end