Guest User

Untitled

a guest
Oct 22nd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. # Dependencies
  2. require "csv"
  3. require 'sunlight'
  4. require 'date'
  5.  
  6.  
  7. # Class Definition
  8. class EventManager
  9. Sunlight::Base.api_key = "e179a6973728c4dd3fb1204283aaccb5"
  10. INVALID_PHONE_NUMBER = "0" * 10
  11. INVALID_ZIPCODE = "0" * 5
  12. def initialize(filename)
  13. puts "EventManager Initialized."
  14. @file = CSV.open(filename, {:headers => true, :header_converters => :symbol})
  15. end
  16.  
  17. def output_data(output_link)
  18. output = CSV.open(output_link, "w")
  19. @file.each do |line|
  20. if @file.lineno == 2
  21. output << line.headers
  22. else
  23. line[:homephone] = clean_number(line[:homephone])
  24. line[:zipcode] = clean_zipcodes(line[:zipcode])
  25. output << line
  26. end
  27. end
  28. end
  29.  
  30. def print_names
  31. @file.each do |line|
  32. # puts line.inspect
  33. puts line[:first_name] + " " + line[:last_name]
  34. # puts "#{line[2]} #{line[3]}"
  35. end
  36. end
  37.  
  38. def clean_number(number)
  39. number.delete!('./\- ()')
  40. if number.length == 11
  41. if number.start_with?("1")
  42. number = number[1..number.length]
  43. else
  44. number = INVALID_PHONE_NUMBER
  45. end
  46. elsif number.length == 10
  47. number
  48. else
  49. number = INVALID_PHONE_NUMBER
  50. end
  51. return number
  52. end
  53.  
  54. def print_numbers
  55. @file.each do |line|
  56. number = clean_number(line[:homephone])
  57. puts number
  58. end
  59. end
  60.  
  61. def print_zipcodes
  62. @file.each do |line|
  63. zipcode = clean_zipcodes(line[:zipcode])
  64. puts zipcode
  65. end
  66. end
  67.  
  68. def clean_zipcodes(original)
  69. if !original
  70. original = INVALID_ZIPCODE
  71. else
  72. while original.length < 5
  73. original = "0" + original
  74. end
  75. end
  76. original
  77. end
  78.  
  79. def rep_lookup
  80. 20.times do
  81. line = @file.readline
  82. legislators = Sunlight::Legislator.all_in_zipcode(clean_zipcodes(line[:zipcode]))
  83. names = legislators.collect do |leg|
  84. first_name = leg.firstname
  85. first_initial = first_name[0]
  86. last_name = leg.lastname
  87. leg.title + " " + first_initial + ". " + last_name + " (#{leg.party})"
  88. end
  89. representative = "unknown"
  90. puts "#{line[:last_name]}, #{line[:first_name]}, #{line[:zipcode]}, #{names.join(", ")}"
  91. end
  92. end
  93.  
  94. def create_form_letters
  95. letter = File.open("form_letter.html", "r").read
  96. 20.times do
  97. line = @file.readline
  98. custom_letter = letter.gsub("#first_name","#{line[:first_name]}")
  99. custom_letter = custom_letter.gsub("#last_name","#{line[:last_name]}")
  100. custom_letter = custom_letter.gsub("#city","#{line[:city]}")
  101. custom_letter = custom_letter.gsub("#state","#{line[:state]}")
  102. custom_letter = custom_letter.gsub("#street","#{line[:street]}")
  103. custom_letter = custom_letter.gsub("#zipcode","#{line[:zipcode]}")
  104.  
  105. filename = "output/thanks_#{line[:last_name]}_#{line[:first_name]}.html"
  106. output = File.new(filename, "w")
  107. output.write(custom_letter)
  108. end
  109. end
  110.  
  111. def rank_times
  112. hours = Array.new(24){0}
  113. @file.each do |line|
  114. hour = line[:regdate][-5,2].to_i
  115. hours[hour] += 1
  116. end
  117. hours.each_with_index{|counter,hour| puts "#{hour}\t#{counter}"}
  118. end
  119.  
  120. def day_stats
  121. days = Array.new(7){0}
  122. @file.each do |line|
  123. day_of_week = Date.strptime( line[:regdate].split[0], "%m/%d/%y").wday
  124. days[day_of_week] += 1
  125. end
  126. days.each_with_index{|counter,day| puts "#{day}\t#{counter}"}
  127. end
  128.  
  129. def state_stats
  130. state_data = {}
  131. @file.each do |line|
  132. state = line[:state] # Find the State
  133. if state_data[state].nil? # Does the state's bucket exist in state_data?
  134. state_data[state] = 1 # If that bucket was nil then start it with this one person
  135. else
  136. state_data[state] = state_data[state] + 1 # If the bucket exists, add one
  137. end
  138. end
  139. ranks = state_data.sort_by{|state, counter| - counter}.collect{|state, counter| state}
  140. state_data = state_data.select{|state, counter| state}.sort_by{|state, counter| state unless state.nil?}
  141. state_data.each do |state, counter|
  142. puts "#{state}:\t#{counter}\t(#{ranks.index(state) + 1 })"
  143. end
  144. end
  145. end
  146.  
  147. # Script
  148. manager = EventManager.new("event_attendees_clean.csv")
  149. # manager.rep_lookup
  150. # manager.create_form_letters
  151. #manager.day_stats
  152. #manager.output_data("event_attendees_clean.csv")
  153. manager.state_stats
Add Comment
Please, Sign In to add comment