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<