-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.rb
47 lines (37 loc) · 943 Bytes
/
parser.rb
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
require 'csv'
class Controller
attr_reader :filename
def initialize(filename)
@filename = filename
end
def read_file(filename)
CSV.read(filename, headers: true)
end
def convert_file_to_hash(data)
data.map{|person| person.to_hash}
end
def filter_by_email(people)
people.uniq{|person| person["email"]}
end
def sort_by_last_name(people)
people.sort{|person1, person2| person1["last_name"] <=> person2["last_name"]}
end
def print_list(people)
people.each do |person|
View.print_person(person)
end
end
def run
data = read_file(filename)
people = convert_file_to_hash(data)
sorted_people = sort_by_last_name(filter_by_email(people))
print_list(sorted_people)
end
end
class View
def self.print_person(person)
puts "#{person['email']},#{person['first_name']},#{person['last_name']}"
end
end
controller = Controller.new('sample_data.csv')
controller.run