finnish-holidays 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d2dfb1a6d3381e3a80162fd2bc5f72902c077f41
4
+ data.tar.gz: cc31433453f61f50914f9cd75686f8f45f887d1f
5
+ SHA512:
6
+ metadata.gz: 23631d87033f9b7a61692dac692f0971bffc3b351c3763664ce48926d8bb9f0b2f93e54ef39849dc0342fc33b09685111dc8bfc5719f5a28529d1362c9302e63
7
+ data.tar.gz: 2ce7bbb62c9c6fa165106b7bbaca57659f534a5ba9ec5c3bd0917238bb1f4eafde22c11428e7004fcd314ab5be2b6fa6d99cbc80e3af0382405e9b3c77f52347
data/.editorconfig ADDED
@@ -0,0 +1,10 @@
1
+ # editorconfig.org
2
+ root = true
3
+
4
+ [*]
5
+ indent_style = space
6
+ indent_size = 2
7
+ end_of_line = lf
8
+ charset = utf-8
9
+ trim_trailing_whitespace = true
10
+ insert_final_newline = true
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .bundle/
2
+ data/
3
+ !data/.gitkeep
4
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'nokogiri'
4
+ gem 'json'
data/Gemfile.lock ADDED
@@ -0,0 +1,14 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ json (1.8.2)
5
+ mini_portile (0.6.2)
6
+ nokogiri (1.6.6.2)
7
+ mini_portile (~> 0.6.0)
8
+
9
+ PLATFORMS
10
+ ruby
11
+
12
+ DEPENDENCIES
13
+ json
14
+ nokogiri
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Eric Nishio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ finnish-holidays
2
+ ================
3
+
4
+ A tool for displaying Finnish national holidays.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'finnish-holidays'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install finnish-holidays
21
+
22
+ ## Usage
23
+
24
+ ```
25
+ ruby bin/holidays.rb
26
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "finnish/holidays"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/holidays.rb ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require_relative '../lib/calendar'
5
+
6
+ options = {}
7
+
8
+ optparse = OptionParser.new do |opts|
9
+ options[:count] = 3
10
+ opts.on('-cCOUNT', '--count=COUNT', 'Number of holidays to show') do |count|
11
+ options[:count] = count.to_i
12
+ end
13
+
14
+ options[:all] = false
15
+ opts.on('-a', '--all', 'Exclude holidays falling on a weekend') do
16
+ options[:all] = true
17
+ end
18
+
19
+ opts.on('-h', '--help', 'Show this screen') do
20
+ puts opts
21
+ exit
22
+ end
23
+ end
24
+
25
+ optparse.parse!
26
+
27
+ Calendar.new.holidays_print(options[:count], options[:all])
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/data/.gitkeep ADDED
File without changes
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "finnish-holidays"
8
+ spec.version = FinnishHolidays::VERSION
9
+ spec.authors = ["Eric Nishio"]
10
+ spec.email = ["eric@self-learner.com"]
11
+
12
+ spec.summary = %q{A tool for displaying Finnish national holidays.}
13
+ spec.description = %q{A tool for displaying Finnish national holidays.}
14
+ spec.homepage = "https://github.com/ericnishio/finnish-holidays"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.9"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ end
data/lib/calendar.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'json'
2
+ require 'time'
3
+ require_relative 'year'
4
+ require_relative 'date-utils'
5
+
6
+ class Calendar
7
+ MAX_HOLIDAYS = 100
8
+
9
+ def initialize()
10
+ @y = Time.now.year
11
+ @m = Time.now.month
12
+ @d = Time.now.day
13
+ @year = Year.new(@y)
14
+ end
15
+
16
+ def holidays(count = 3, all = false)
17
+ if count > MAX_HOLIDAYS
18
+ raise "Cannot request more than #{MAX_HOLIDAYS} holidays at once."
19
+ end
20
+
21
+ holidays = Array.new
22
+
23
+ while holidays.length < count
24
+ month_index = @m.to_s
25
+
26
+ if !all
27
+ @year.discard_weekends()
28
+ end
29
+
30
+ if defined? @year.holidays[month_index] and @year.holidays[month_index].is_a? Array
31
+ @year.holidays[month_index].each do |holiday|
32
+ if holidays.length < count and holiday['day'].to_i >= @d
33
+ holidays.push(holiday)
34
+ end
35
+ end
36
+ end
37
+
38
+ if holidays.length < count
39
+ next_month()
40
+ end
41
+ end
42
+
43
+ holidays
44
+ end
45
+
46
+ def holidays_print(count = 3, all = false)
47
+ holidays = self.holidays(count, all)
48
+
49
+ holidays.each do |holiday|
50
+ t = DateUtils.create_date(holiday['year'], holiday['month'], holiday['day'])
51
+ date = t.strftime('%a, %b %e, %Y')
52
+
53
+ puts "#{date} #{holiday['description']}"
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def next_month()
60
+ if @m == 12
61
+ @m = 1
62
+ @y += 1
63
+ @d = 1
64
+ else
65
+ @m += 1
66
+ @d = 1
67
+ end
68
+
69
+ @year = Year.new(@y)
70
+ end
71
+ end
data/lib/date-utils.rb ADDED
@@ -0,0 +1,37 @@
1
+ class DateUtils
2
+ def self.create_date(year, month, day)
3
+ day = self.zerofy(day)
4
+ month = self.zerofy(month)
5
+ year = year.to_s
6
+
7
+ Time.parse("#{year}-#{month}-#{day}")
8
+ end
9
+
10
+ def self.parse_day(date_string)
11
+ parts = date_string.split('.')
12
+ parts[0].to_i
13
+ end
14
+
15
+ def self.parse_month(date_string)
16
+ parts = date_string.split('.')
17
+ parts[1].to_i
18
+ end
19
+
20
+ def self.is_weekend(year, month, day)
21
+ t = self.create_date(year, month, day)
22
+ day_of_week = t.strftime('%u').to_i
23
+ day_of_week == 6 or day_of_week == 7
24
+ end
25
+
26
+ def self.zerofy(num)
27
+ num = num.to_i
28
+
29
+ if num < 10
30
+ num = '0' + num.to_s
31
+ else
32
+ num = num.to_s
33
+ end
34
+
35
+ num
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ require_relative 'calendar'
2
+
3
+ class FinnishHolidays
4
+ def self.holidays(count = 3, all = false)
5
+ Calendar.new.holidays
6
+ end
7
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module FinnishHolidays
2
+ VERSION = "0.1.0"
3
+ end
data/lib/year.rb ADDED
@@ -0,0 +1,101 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require 'json'
4
+ require 'time'
5
+ require_relative 'date-utils'
6
+
7
+ class Year
8
+ def initialize(year)
9
+ if year.is_a? Integer and year > 0
10
+ @year = year.to_i
11
+ @holidays = {}
12
+ load_data()
13
+ else
14
+ raise "Invalid year: #{year}"
15
+ end
16
+ end
17
+
18
+ def holidays()
19
+ @holidays
20
+ end
21
+
22
+ def discard_weekends()
23
+ holidays = {}
24
+
25
+ @holidays.each do |month, array|
26
+ array.each do |holiday|
27
+ if !DateUtils.is_weekend(holiday['year'], holiday['month'], holiday['day'])
28
+ if !holidays[month].is_a? Array
29
+ holidays[month] = []
30
+ end
31
+
32
+ holidays[month].push(holiday)
33
+ end
34
+ end
35
+ end
36
+
37
+ @holidays = holidays
38
+ end
39
+
40
+ private
41
+
42
+ def load_data()
43
+ file = get_cache_file_path()
44
+
45
+ if File.exist?(file)
46
+ load_from_file()
47
+ else
48
+ load_from_web()
49
+ cache()
50
+ end
51
+ end
52
+
53
+ def load_from_file()
54
+ json = File.read(get_cache_file_path())
55
+ @holidays = JSON.parse(json)
56
+ end
57
+
58
+ def load_from_web()
59
+ page = Nokogiri::HTML(open('http://www.webcal.fi/fi-FI/pyhat.php?y=' + @year.to_s))
60
+
61
+ page.css('table.basic tr').each do |el|
62
+ if el.css('th:last-child').text != 'Päivämäärä' # TODO: Improve check.
63
+ date_el = el.css('td:nth-child(4)')
64
+ description_el = el.css('td:nth-child(2)')
65
+
66
+ month = DateUtils.parse_month(date_el.text)
67
+ day = DateUtils.parse_day(date_el.text)
68
+ description = description_el.text
69
+
70
+ add_holiday(@year, month, day, description)
71
+ end
72
+ end
73
+ end
74
+
75
+ def add_holiday(year, month, day, description)
76
+ year = year.to_i
77
+ month = month.to_i
78
+ day = day.to_i
79
+
80
+ if !@holidays[month].is_a? Array
81
+ @holidays[month] = []
82
+ end
83
+
84
+ holiday = {
85
+ 'year' => year,
86
+ 'month' => month,
87
+ 'day' => day,
88
+ 'description' => description
89
+ }
90
+
91
+ @holidays[month].push(holiday)
92
+ end
93
+
94
+ def cache()
95
+ File.write(get_cache_file_path(), @holidays.to_json)
96
+ end
97
+
98
+ def get_cache_file_path()
99
+ return File.dirname(__FILE__) + "/../data/#{@year}.json"
100
+ end
101
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: finnish-holidays
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Nishio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: A tool for displaying Finnish national holidays.
42
+ email:
43
+ - eric@self-learner.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .editorconfig
49
+ - .gitignore
50
+ - .rspec
51
+ - .travis.yml
52
+ - Gemfile
53
+ - Gemfile.lock
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - bin/console
58
+ - bin/holidays.rb
59
+ - bin/setup
60
+ - data/.gitkeep
61
+ - finnish-holidays.gemspec
62
+ - lib/calendar.rb
63
+ - lib/date-utils.rb
64
+ - lib/finnish-holidays.rb
65
+ - lib/version.rb
66
+ - lib/year.rb
67
+ homepage: https://github.com/ericnishio/finnish-holidays
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ allowed_push_host: 'TODO: Set to ''http://mygemserver.com'''
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.0.14
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: A tool for displaying Finnish national holidays.
92
+ test_files: []