-
Notifications
You must be signed in to change notification settings - Fork 0
/
holiday.rb
121 lines (101 loc) · 2.51 KB
/
holiday.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
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
require 'date'
require 'time'
require 'holidays'
require 'rubygems'
require 'zip'
class Holiday
def self.get_holidays_for(year:, region:, excluded_days:)
holidays = Holidays.cache_between(
Date.new(year, 1, 1), Date.new(year, 12, 31), region, :informal
)
# delete exluded days
excluded_days.each do |day|
holidays.delete_if { |m| m[:name] == day }
end
# potential duplicates, e.g. Reformationstag 2017
holidays.uniq { |m| m[:name] }
end
end
class Output
attr_reader :year, :region_code, :region_name, :country
def initialize(year:, region_code:, region_name:, country:)
@year = Date.new(year)
@region_code = region_code
@region_name = region_name
@country = country
end
def name
"#{year.strftime('%y')}_#{region_code}.hct"
end
def header
"#{year.strftime('%Y')};#{country.capitalize}, #{region_name.to_s}"
end
def self.generate_date_and_name(key)
key[:date].strftime('%d.%m.%Y').to_s + ';' + key[:name].to_s
end
end
class Result
attr_reader :file, :holidays
def initialize(file, holidays)
@file = file
@holidays = holidays
end
def to_archive
Zip::File.open("#{file.year.strftime('%Y')}_templates.zip", Zip::File::CREATE) do |archive|
to_file
to_zip(archive)
end
Files.delete(:templates)
end
def to_console
puts file.header
holidays.each do |key, _|
puts Output.generate_date_and_name(key)
end
end
private
def to_file
File.open(file.name, 'w+') do |f|
f.puts file.header
holidays.each do |key, _|
f.puts Output.generate_date_and_name(key)
end
end
end
def to_zip(archive)
archive.add(file.name, file.name)
end
end
class Files
def self.delete(pattern)
case pattern
when :archives
Dir['*_templates.zip'].each do |f|
File.delete(f)
end
when :templates
Dir['*.hct'].each do |f|
File.delete(f)
end
end
end
end
class TemplateFileGenerator
attr_reader :years, :regions
def self.run(year:, regions:, excluded_days:)
results = []
regions.each do |country, region|
region.each do |region_code, region_name|
template_file = Output.new(
year: year,
country: country,
region_code: region_code,
region_name: region_name
)
holidays = Holiday.get_holidays_for(year: year, region: region_code, excluded_days: excluded_days)
results << Result.new(template_file, holidays)
end
end
results
end
end