forked from ruby-conferences/ruby-conferences.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_file_validator.rb
52 lines (44 loc) · 1.1 KB
/
data_file_validator.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
48
49
50
51
52
class DataFileValidator
attr_accessor :events
def self.validate(events, allowed_keys, type = :conference)
new(events, allowed_keys, type).tap &:validate
end
def initialize(events, allowed_keys, type = :conference)
@events = events
@allowed_keys = allowed_keys
@type = type
end
def validate
events.each do |event|
missing_keys = required_keys - event.keys
unless missing_keys.empty?
puts "Missing keys: #{missing_keys}"
puts event
@missing_keys_error = true
end
bonus_keys = event.keys - @allowed_keys
unless bonus_keys.empty?
puts "Bonus keys: #{bonus_keys}"
puts event
@bonus_keys_error = true
end
end
end
def missing_keys?
@missing_keys_error
end
def bonus_keys?
@bonus_keys_error
end
private
def required_keys(type = @type)
case type
when :conference
["start_date", "end_date", "name", "location"]
when :meetup
["date", "start_time", "end_time", "name", "location"]
else
raise "required_keys: unknown type '#{type}'"
end
end
end