holiday_tools 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvmarchflags="-arch x86_64"
2
+ rvm use 1.9.3@holidays --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in holiday_tools.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 kittekat
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # HolidayTools
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'holiday_tools'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install holiday_tools
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bin/holiday_tools ADDED
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ # CLI for the HolidayTools::Locator
5
+ # list the countries/subregions which have a holiday
6
+
7
+ # kittekat /may/jun 2013
8
+
9
+ require "date"
10
+ require "optparse"
11
+ require "optparse/date"
12
+ require "ostruct"
13
+
14
+ require "holiday_tools"
15
+
16
+ # dummy entry will keep the ruby interpeter happy
17
+ # as the "real" @opts is generated in an begin/rescue/end block
18
+ @opts={}
19
+
20
+ # collect the commandline options
21
+ def get_opts
22
+ options = OpenStruct.new
23
+
24
+ #set the defaults
25
+ options.mode = :d
26
+ options.date = Date.today
27
+ options.debug = false
28
+ #options.list = false
29
+
30
+ # prepare the parser
31
+ opts = OptionParser.new
32
+ opts.banner = "Usage: #{__FILE__} [options] [region [region] ...]"
33
+
34
+ opts.on( '-m', '--mode MODE', [:d, :w, :m, :y],
35
+ "d(ay), w(eek), m(onth), y(ear) [Default: d]") do |m|
36
+ options.mode = m
37
+ end
38
+
39
+ opts.on( '-d', '--date YYYY-MM-DD', Date,
40
+ "Year-Month-Day [Default: today]") do |d|
41
+ options.date = d
42
+ end
43
+
44
+ opts.on( '-v', '--verbose', "Flag for debug output [Default: false]") do |v|
45
+ options.debug = v
46
+ end
47
+
48
+ opts.on( '-l', '--list', 'List available regions') do |l|
49
+ options.list = l
50
+ end
51
+
52
+ opts.on_tail( '-h', '--help', "Show this message") do
53
+ puts opts
54
+ exit
55
+ end
56
+
57
+ opts.parse!(ARGV) # start the parser
58
+
59
+ # collect additional parameters as filter
60
+ options.filter = ARGV
61
+
62
+ options # return the struct
63
+ end
64
+
65
+
66
+ # start with the commandline options
67
+ # recover from bad user input for options
68
+ begin
69
+ @opts = get_opts
70
+ if @opts.debug
71
+ puts "mode: %s" % [@opts.mode]
72
+ puts "date: %s" % [@opts.date]
73
+ puts "filter: %s" % [@opts.filter]
74
+ puts "list: %s" % [@opts.list]
75
+ end
76
+ rescue OptionParser::InvalidArgument
77
+ puts "Invalid date argument"
78
+ puts "Use --help for a format hint"
79
+ exit
80
+ rescue OptionParser::MissingArgument
81
+ puts "Missing argument"
82
+ puts "Use --help for hints"
83
+ exit
84
+ rescue OptionParser::InvalidOption
85
+ puts "Invalid option"
86
+ puts "Use --help for a list"
87
+ exit
88
+ end
89
+
90
+ holidays = HolidayTools::Locator.new(@opts.filter)
91
+ if @opts.list
92
+ puts holidays.show_regions
93
+ exit
94
+ end
95
+
96
+ # distribute depending on mode
97
+ date = @opts.date
98
+ case @opts.mode
99
+ when :d
100
+ puts holidays.show_holiday(date)
101
+ when :w
102
+ puts holidays.show_week(date)
103
+ when :m
104
+ puts holidays.show_month(date)
105
+ when :y
106
+ puts holidays.show_year(date)
107
+ end
108
+
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/holiday_tools/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["kittekat"]
6
+ gem.email = ["wolfkibe@gmail.com"]
7
+ gem.description = %q{List regions of public holidays}
8
+ gem.summary = %q{Select date, period and regions for public holidays}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "holiday_tools"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = HolidayTools::VERSION
17
+
18
+ gem.add_dependency 'holidays'
19
+ gem.add_development_dependency 'rspec', '~> 2.7'
20
+ end
@@ -0,0 +1,3 @@
1
+ module HolidayTools
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,284 @@
1
+ # encoding: utf-8
2
+
3
+ require "holiday_tools/version"
4
+
5
+ module HolidayTools
6
+ class Locator
7
+
8
+ # prerequisit: gem install holidays
9
+ # see https://github.com/alexdunae/holidays
10
+
11
+ require "holidays"
12
+ require "holidays/at"
13
+ require "holidays/au"
14
+ require "holidays/br"
15
+ require "holidays/ca"
16
+ # require "holidays/ch"
17
+ require "holidays/cz"
18
+ require "holidays/de"
19
+ require "holidays/dk"
20
+ require "holidays/el"
21
+ require "holidays/es"
22
+ require "holidays/fi"
23
+ require "holidays/fr"
24
+ require "holidays/gb"
25
+ # require "holidays/hr"
26
+ # require "holidays/hu"
27
+ require "holidays/ie"
28
+ require "holidays/is"
29
+ require "holidays/it"
30
+ require "holidays/jp"
31
+ require "holidays/li"
32
+ require "holidays/mx"
33
+ require "holidays/nl"
34
+ require "holidays/no"
35
+ require "holidays/nz"
36
+ require "holidays/pl"
37
+ require "holidays/pt"
38
+ require "holidays/se"
39
+ require "holidays/us"
40
+ # require "holidays/vz"
41
+ require "holidays/za"
42
+
43
+ def initialize( filter )
44
+ @filter = filter
45
+
46
+ # names of the regions/subregions used in the holidays gem
47
+ @reg_names = {
48
+ at: 'Austria',
49
+ au: 'Australia',
50
+ au_act: 'Australia/Australian Capital Terretory',
51
+ au_nsw: 'Australia/New South Wales',
52
+ au_nt: 'Australia/Nothern Terretory',
53
+ au_qld: 'Australia/Queensland',
54
+ au_qld_brisbane: 'Australia/Queensland and Brisbane',
55
+ au_sa: 'Australia/South Australia',
56
+ au_tas: 'Australia/Tasmania',
57
+ au_vic: 'Australia/Victoria',
58
+ au_wa: 'Australia/Western Australia',
59
+ br: 'Brasilia',
60
+ ca: 'Canada',
61
+ ca_ab: 'Canada/Alberta',
62
+ ca_bc: 'Canada/British Columbia',
63
+ ca_mb: 'Canada/Manitoba',
64
+ ca_nb: 'Canada/New Brunswick',
65
+ ca_nf: 'Canada/Newfoundland',
66
+ ca_ns: 'Canada/Nova Scotia',
67
+ ca_nt: 'Canada/Northwest Territories',
68
+ ca_nu: 'Canada/Nunavut',
69
+ ca_on: 'Canada/Ontario',
70
+ ca_pe: 'Canada/Prince Edward Island',
71
+ ca_sk: 'Canada/Saskatchewan',
72
+ ca_qc: 'Canada/Quebeck',
73
+ ca_yk: 'Canada/Yukon',
74
+ cz: 'Czech Republic',
75
+ de: 'Germany',
76
+ de_bb: 'Germany/Brandenburg',
77
+ # de_be: 'Germany/Berlin',
78
+ de_bw: 'Germany/Baden-Württemberg',
79
+ de_by: 'Germany/Bayern',
80
+ # de_hb: 'Germany/Freie Hansestadt Bremen',
81
+ de_he: 'Germany/Hessen',
82
+ # de_hh: 'Germany/Hamburg',
83
+ de_mv: 'Germany/Mecklenburg-Vorpommern',
84
+ # de_ni: 'Germany/Niedersachsen',
85
+ de_nw: 'Germany/Nordrhein-Westfalen',
86
+ de_rp: 'Germany/Rheinland-Pfalz',
87
+ # de_sh: 'Germany/Schleswig-Holstein',
88
+ de_sl: 'Germany/Saarland',
89
+ de_sn: 'Germany/Sachsen',
90
+ de_st: 'Germany/Sachsen-Anhalt',
91
+ de_th: 'Germany/Thüringen',
92
+ dk: 'Denmark',
93
+ el: 'Greece',
94
+ es: 'Spain',
95
+ es_an: 'Spain/Andalusia',
96
+ es_ar: 'Spain/Aragon',
97
+ es_ce: 'Spain/Ceuta',
98
+ es_cl: 'Spain/Castile and Leon',
99
+ es_cm: 'Spain/Castile-La Mancha',
100
+ es_cn: 'Spain/Canary Island',
101
+ es_ct: 'Spain/Catalonia',
102
+ es_ex: 'Spain/Extremadura',
103
+ es_ga: 'Spain/Galicia',
104
+ es_ib: 'Spain/Islas Baleares',
105
+ es_lo: 'Spain/La Rioja',
106
+ es_m: 'Spain/Madrid',
107
+ es_mu: 'Spain/Murcia',
108
+ es_na: 'Spain/Navarre',
109
+ es_o: 'Spain/Asturias',
110
+ es_pv: 'Spain/Euskadi (Pais Vasco - Basque Country)',
111
+ es_v: 'Spain/Valencia',
112
+ es_vc: 'Spain/Valencia',
113
+ fi: 'Finland',
114
+ fr: 'France',
115
+ gb: 'Great Britain',
116
+ gb_eng: 'Great Britain/England',
117
+ gb_wls: 'Great Britain/Wales',
118
+ gb_eaw: 'Great Britain/England and Wales',
119
+ gb_nir: 'Great Britain/Norther Ireland',
120
+ gb_sct: 'Great Britain/Scotland',
121
+ gb_jsy: 'Great Britain/Jersey',
122
+ gb_gsy: 'Great Britain/Guernsey',
123
+ gb_con: 'Great Britain/Cornwall',
124
+ gb_iom: 'Great Britain/Isle of Man',
125
+ gg: 'Guernsey',
126
+ ie: 'ireland',
127
+ im: 'Isle of Man',
128
+ it: 'Italy',
129
+ is: 'Iceland',
130
+ je: 'Jersey',
131
+ jp: 'Japan',
132
+ li: 'Liechtenstein',
133
+ mx: 'Mexico',
134
+ mx_pue: 'Mexico/Puebla',
135
+ nz: 'New Zealand',
136
+ nz_ak: 'New Zealand/Auckland',
137
+ nz_ca: 'New Zealand/Canterbury',
138
+ nz_ch: 'New Zealand/Chatham',
139
+ nz_hb: "New Zealand/Hawke's Bay",
140
+ nz_mb: 'New Zealand/Marlboro',
141
+ nz_nl: 'New Zealand/Northland',
142
+ nz_ot: 'New Zealand/Otago',
143
+ nz_sc: 'New Zealand/South Canterbury',
144
+ nz_sl: 'New Zealand/Southland',
145
+ nz_we: 'New Zealand/Wellington',
146
+ nz_wl: 'New Zealand/Westland',
147
+ nl: 'Netherlands',
148
+ no: 'Norway',
149
+ pl: 'Poland',
150
+ pt: 'Portugal',
151
+ se: 'Sweden',
152
+ us: 'United States of America',
153
+ us_dc: 'United States of America/Washington DC',
154
+ za: 'South Africa'
155
+ }
156
+ end
157
+
158
+ # loop over region names
159
+ def show_regions
160
+ txt = []
161
+ txt << "Region \tName"
162
+ @reg_names.each do |key, val|
163
+ txt << "%s \t%s" % [key, val]
164
+ end
165
+ txt
166
+ end
167
+
168
+ # get the name of a holiday and the regions, where it is observed
169
+ def show_holiday(date)
170
+ ans = Holidays.on(date, :any, :observed)
171
+ # dat = date.to_s
172
+ if ans.size > 0
173
+ if @filter.size == 0
174
+ show_all(date, ans)
175
+ else
176
+ show_filtered(date, ans)
177
+ end
178
+ end
179
+ end
180
+
181
+ # loop over a week
182
+ def show_week(date)
183
+ fw = date - date.wday # from sunday
184
+ tw = fw + 6 # to saturday
185
+ show_holidays(fw, tw)
186
+ end
187
+
188
+ # loop over a month
189
+ def show_month(date)
190
+ fm = date - date.mday + 1 # from 1. in month
191
+ tm = (fm >> 1) - 1 # to last in month ( '>> 1' advances one month )
192
+ show_holidays(fm, tm)
193
+ end
194
+
195
+
196
+ # loop over a year
197
+ def show_year(date)
198
+ fy = date - date.yday + 1
199
+ ty = Date.civil(fy.year, 12, 31)
200
+ show_holidays(fy, ty)
201
+ end
202
+
203
+ private
204
+ # get the name of a weekday
205
+ def wday_nam(wday)
206
+ name = %w( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )
207
+ name[wday]
208
+ end
209
+
210
+ # show the date
211
+ def show_dat(dat)
212
+ "%s (%s)" % [dat.to_s, wday_nam(dat.wday)]
213
+ end
214
+
215
+ # show the name of the holiday
216
+ def show_nam(nam)
217
+ "\t %s" % [ nam ]
218
+ end
219
+
220
+ # show the expanded name of a region
221
+ def show_reg(reg)
222
+ rn = @reg_names[reg]
223
+ if rn
224
+ "\t \t %s" % [ rn ]
225
+ else
226
+ "\t \t :%s (?)" % [ reg ]
227
+ end
228
+ end
229
+
230
+ # show without region filter
231
+ def show_all(dat, ans)
232
+ txt = []
233
+ txt << show_dat(dat)
234
+ ans.each do |entry|
235
+ nam = entry[:name]
236
+ txt << show_nam(nam)
237
+ entry[:regions].each do |reg|
238
+ txt << show_reg(reg)
239
+ end
240
+ end
241
+ txt
242
+ end
243
+
244
+ # show with region filter
245
+ def show_filtered(dat, ans)
246
+ txt = []
247
+ date_shown = false
248
+ ans.each do |entry|
249
+ nam = entry[:name]
250
+ name_shown = false
251
+ entry[:regions].each do |reg|
252
+ @filter.each do |pat|
253
+ # construct a pattern
254
+ pattern = Regexp.new(pat)
255
+ if reg.to_s =~ pattern
256
+ # we have a match
257
+ unless date_shown
258
+ txt << show_dat(dat)
259
+ date_shown = true
260
+ end
261
+ unless name_shown
262
+ txt << show_nam(nam)
263
+ name_shown = true
264
+ end
265
+ txt << show_reg(reg)
266
+ end
267
+ end
268
+ end
269
+ end
270
+ txt
271
+ end
272
+
273
+ # loop over a range of dates
274
+ def show_holidays(from_date, to_date)
275
+ txt = []
276
+ from_date.upto(to_date) do |date|
277
+ res = show_holiday(date)
278
+ txt << res if res
279
+ end
280
+ txt
281
+ end
282
+
283
+ end # class
284
+ end # module
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: holiday_tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - kittekat
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: holidays
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.7'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.7'
46
+ description: List regions of public holidays
47
+ email:
48
+ - wolfkibe@gmail.com
49
+ executables:
50
+ - holiday_tools
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rvmrc
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - bin/holiday_tools
61
+ - holiday_tools.gemspec
62
+ - lib/holiday_tools.rb
63
+ - lib/holiday_tools/version.rb
64
+ homepage: ''
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.24
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Select date, period and regions for public holidays
88
+ test_files: []