-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_manager.rb
More file actions
211 lines (184 loc) · 5.92 KB
/
Copy pathevent_manager.rb
File metadata and controls
211 lines (184 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Dependencies
#$LOAD_PATH << './' -- WITH A LOT OF REQUIREMENTS, DEFINE GLOBAL VARIABLE AND SHOVEL CURRENT DIRECTORY
require 'csv'
require 'sunlight'
# require './attendee'
# Class Definition
class EventManager
# attr_accessor :attendees
#CSV_OPTIONS {:headers => true, :header_converters => :symbol}
INVALID_ZIPCODE = "00000"
INVALID_PHONE = "0000000000"
INVALID_EMAIL = " "
Sunlight::Base.api_key = "e179a6973728c4dd3fb1204283aaccb5"
def initialize(filename)
puts "EventManager Initialized."
@file = CSV.open(filename, :headers =>true, :header_converters => :symbol)
# load_attendees
end
#@file.each do |line|
#puts line.inspect
#end
# EXECUTING BLOCKS
# def load_attendees(file)
# file.rewind
# self.attendees = file.collect { |line| Attendee.new(line) }
# end
# end
# def attendees
# @attendees.each do |attendee|
# yield attendee if block_given?
# end
# end
# def attendees(&block)
# @attendees.each do |attendee|
# block.call(attendee) unless block.nil?
# end
# end
def print_names
# puts "Printing first and last names"
# @file.each do |line|
# first_name = line[:first_name]
# last_name = line[:last_name]
# puts [line[:first_name] + " " + line[:last_name]].join(" ")
# puts line.inspect
# end
end
def clean_numbers(phone_number)
phone_number = phone_number.scan(/\d/).join
if phone_number.length == 10
phone_number
elsif phone_number.length == 11 && phone_number.start_with?("1")
phone_number[1..-1]
else
phone_number = "0"*10
end
end
def print_names_numbers
# @file.each do |line|
# puts [line [:first_name],
# line[:last_name],
# clean_numbers(line[:homephone])].join(" ")
# end
end
def clean_zipcode(original)
if original.nil?
result = INVALID_ZIPCODE
return result
elsif original.length < 5
# #WHILE LOOP (CALCULATE MISSING ZEROS) // preferred
while original.length <= 4
original.insert(0, '0')
end
return original
#STRING METHOD
#until original.length == 5
#original.insert (0, '0')
#end
# CALCULATE FIXNUM ZEROS
# x = 5 - original.length
# return "#{0*x}" + original
else
return original
end
end
def print_zipcodes
@file.each do |line|
zipcode = clean_zipcode(line[:zipcode])
puts zipcode
end
end
def rep_lookup
20.times do
line = @file.readline
representative = "unknown"
#API LOOKUP
puts "#{line[:last_name]}, #{line[:first_name]}, #{line[:zipcode]}, #{representative}"
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 output_data(filename)
output = CSV.open(filename, "w")
@file.each do |line|
if @file.lineno == 2
output << line.headers
end
line[:homephone] = clean_numbers(line[:homephone])
output << line
end
end
def create_form_letter
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])
custom_letter = custom_letter.gsub("#street", line[:street])
custom_letter = custom_letter.gsub("#city", line[:city])
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|
# Counting goes here
regdate = line[:regdate]
my_string = regdate
parts = my_string.split(" ")
hour = parts[1].split(":")
# puts hour
hours[hour.to_i] = hours[hour.to_i] + 1
end
puts hours.each_with_index{|counter,hour| puts "#{hours}\t#{counter}"}
end
def day_stats
days = Array.new(7){0}
@file.each do |line|
date = Date.strptime(line[:regdate], "%m/%d/%Y")
puts date.wday
#looks like saturday [6] & Friday [5] are the primary reg days
end
end
def state_stats
state_data = {}
@file.each do |line|
state = line[:state] # Find the State
if state_data[state].nil? # Does the state's bucket exist?
state_data[state] = 1 # If that bucket was nil then start it with this one person
else
state_data[state] = state_data[state] + 1 # If the bucket exists, add one
end
end
state_data.each do |state,counter|
# puts "#{state}: #{counter}"
end
state_data = state_data.select{|state, counter| state}.sort_by{|state, counter| state unless state.nil?}
state_data.each do |state, counter|
puts "#{state}: #{counter}" #puts alphabetical list of states and counts
end
#below code will order ascending - descending count for states
# state_data = state_data.select{|counter, state| state}.sort_by{|counter, state| state unless state.nil?}
# state_data.each do |counter, state|
# puts "#{state}: #{counter}"
# 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
em = EventManager.new("event_attendees.csv")
em.rank_times