calendar_days 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a723a72a48eabe4ed80faa6215059d9dcdb7c09
4
+ data.tar.gz: 5ede3a3cd1cb75789780f4bccb2d658d28a07e28
5
+ SHA512:
6
+ metadata.gz: 0a89bf000bfa2ff3a6fea0002eeed170e0c4c8b9596fa9f8160bb8285d77290911ac744fad75f8345d1eff66cfcf4e3255057eb326c87ee9ad267abe3e66a845
7
+ data.tar.gz: d2813156ae7aae63294b71897d9acbe16b41183fec10880f05246ff75694e68d65be8b3f3da7182acf2802b8d7730e8fa4d7bfae8245b358669b2db79d5d6dd2
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.ics
10
+ Gemfile.lock
11
+
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in calendar_days.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 YAMAMOTO, Masayuki
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,39 @@
1
+ # CalendarDays
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/calendar_days`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'calendar_days'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install calendar_days
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mephistobooks/calendar_days.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "calendar_days"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "calendar_days/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "calendar_days"
8
+ spec.version = CalendarDays::VERSION
9
+ spec.authors = ["YAMAMOTO, Masayuki"]
10
+ spec.email = ["martin.route66.blues+github@gmail.com"]
11
+
12
+ spec.summary = %q{calendar_days.gem is to know weekdays, holidays and weekends.}
13
+ spec.description = %q{calendar_days uses webcal (ics file). the installation of icalendar.gem is needed.}
14
+ spec.homepage = "https://github.com/mephistobooks/calendar_days"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing 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 " \
23
+ # "public gem pushes."
24
+ #end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ # spec.add_development_dependency "minitest", "~> 5.0"
36
+ spec.add_development_dependency "test-unit", "~> 3.2"
37
+ spec.add_development_dependency "icalendar", "~> 2.4"
38
+ end
data/exe/calendar_days ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
+ require "calendar_days"
5
+
6
+ year = ARGV[0] || DateTime.now.year
7
+ year = year.to_i
8
+ month = ARGV[1]
9
+ month = month.to_i unless month.nil?
10
+ cald = CalendarDays.new(year, month)
11
+
12
+ puts cald.holidays.map{|ar| [ar.first.to_s.gsub(/T.+$/, ''), ar.last].join(' ') }
13
+
@@ -0,0 +1,209 @@
1
+ #
2
+ #
3
+ #
4
+
5
+ require 'icalendar'
6
+ require 'date'
7
+
8
+ require "calendar_days/net_cache"
9
+
10
+
11
+ #
12
+ #
13
+ #
14
+ class CalendarDays < ::DateTime
15
+
16
+ include NetCache
17
+
18
+ end
19
+
20
+
21
+ #
22
+ #
23
+ #
24
+ class CalendarDays
25
+ #
26
+ # https://stackoverflow.com/questions/9049123/why-does-date-new-not-call-initialize
27
+ #
28
+ def self.new( in_year = DateTime.now.year, since_month = nil )
29
+ since_month_tmp = since_month || 1
30
+ tmp = super(in_year, since_month_tmp, 1)
31
+ tmp.instance_eval{ initialize(in_year, since_month) }
32
+ end
33
+ alias :in_year :year
34
+ alias :since_month :month
35
+ alias :since_day :day
36
+ def since; DateTime.new(in_year, since_month, since_day); end
37
+
38
+ def initialize( in_year, until_month = nil )
39
+
40
+ until_month ||= 12
41
+ @until_month = until_month
42
+ @until_day = DateTime.new(in_year, until_month, -1).day
43
+
44
+ prepare_repo
45
+
46
+ #
47
+ ics_file = File.open(repo_file_fullpath)
48
+ @ics_events = Icalendar::Event.parse(ics_file).sort_by{|ev| ev.dtstart.to_datetime}
49
+ ics_file.close
50
+
51
+ self
52
+ end
53
+ attr_accessor :until_month, :until_day
54
+ attr_reader :ics_events
55
+ def until; DateTime.new(in_year, until_month, until_day); end
56
+ def ics_start
57
+ ics_events.first.dtstart.to_datetime
58
+ end
59
+ def ics_end
60
+ ics_events.last.dtstart.to_datetime
61
+ end
62
+ def ics_since; DateTime.new(ics_start.year, ics_start.month, ics_start.day); end
63
+ def ics_until; DateTime.new(ics_end.year, ics_end.month, ics_end.day ); end
64
+
65
+ end
66
+
67
+
68
+ #
69
+ #
70
+ #
71
+ class CalendarDays
72
+
73
+ def date_to_datetime( date )
74
+ date = DateTime.parse(date) if date.is_a? ::String
75
+ date
76
+ end
77
+
78
+ # 指定した年(もしくは月)の平日 Weekday (土日祝日を抜いた日) を得る
79
+ #
80
+ #
81
+ def __weekday_list
82
+ ret = []
83
+
84
+ dt_since = DateTime.new(in_year, since_month, since_day)
85
+ dt_until = DateTime.new(in_year, until_month, until_day)
86
+
87
+ dt_tmp = dt_since
88
+ begin
89
+ # if is_weekend?(dt_tmp) or is_holiday?(dt_tmp)
90
+ if yield(dt_tmp)
91
+ ret.push dt_tmp
92
+ end
93
+
94
+ dt_tmp += 1
95
+ end while dt_tmp <= dt_until
96
+
97
+ ret
98
+ end
99
+ def weekday_list
100
+ ret = __weekday_list{|dt| not(is_weekend?(dt)) and not(is_holiday?(dt)) }
101
+ ret
102
+ end
103
+
104
+ #
105
+ # ==== Attention
106
+ # there exists such days which are both weekend and holiday.
107
+ def weekend_list
108
+ ret = __weekday_list{|dt| is_weekend?(dt) }
109
+ ret
110
+ end
111
+ alias :weekdays :weekday_list
112
+ alias :workingdays :weekday_list
113
+ alias :weekends :weekend_list
114
+
115
+ def is_saturday?( date )
116
+ date_to_datetime(date).saturday?
117
+ end
118
+ alias :is_sat? :is_saturday?
119
+ alias :is_sat? :is_saturday?
120
+
121
+ def is_sunday?( date )
122
+ date_to_datetime(date).sunday?
123
+ end
124
+ alias :is_sun? :is_sunday?
125
+
126
+ def is_weekend?(date)
127
+ is_saturday?(date) or is_sunday?(date)
128
+ end
129
+
130
+ end
131
+
132
+
133
+ #
134
+ #
135
+ #
136
+ class CalendarDays
137
+
138
+ # all holidays defined in ics file.
139
+ #
140
+ #
141
+ def ics_holiday_list( events: self.ics_events )
142
+ events.map{|ev| [ev.dtstart.to_datetime, ev.summary] }
143
+ end
144
+ alias :ics_holidays :ics_holiday_list
145
+
146
+ # holidays in since..until
147
+ #
148
+ #
149
+ def holiday_list( events: self.ics_events )
150
+ idx_since = ics_holiday_list.bsearch_index{|al| self.since <= al.first }
151
+ idx_until = ics_holiday_list.bsearch_index{|al| self.until <= al.first } - 1
152
+ # $stderr.puts "idx_since: #{idx_since}, idx_until: #{idx_until}"
153
+ ics_holiday_list[idx_since..idx_until]
154
+ end
155
+ alias :holidays :holiday_list
156
+
157
+ def is_holiday?( date )
158
+ if date.is_a? ::String
159
+ date = DateTime.parse(date)
160
+ end
161
+ __dt = date
162
+
163
+ #
164
+ dt_first = ics_events().first.dtstart.to_datetime
165
+ dt_last = ics_events().last.dtstart.to_datetime
166
+ if __dt.year < dt_first.year
167
+ # return nil
168
+ raise ArgumentError,
169
+ "year #{__dt.year} in #{date} is too old;" \
170
+ " specify after #{dt_first.year}."
171
+ elsif __dt.year > dt_last.year
172
+ # return nil
173
+ raise ArgumentError,
174
+ "year #{__dt.year} in #{date} is too new;" \
175
+ " specify before #{dt_last.year}."
176
+ end
177
+
178
+ dt = __dt.to_s.gsub(/T.+$/, '')
179
+ holiday_list.map{|e| e.first.to_s }.grep( /^#{dt}/ ).size > 0
180
+ end
181
+
182
+ def holiday_name( date )
183
+ if date.is_a? ::String
184
+ date = DateTime.parse(date)
185
+ end
186
+ __dt = date
187
+ dt = __dt.to_s.gsub(/T.+$/, '')
188
+
189
+ if is_holiday?(date)
190
+ holiday_list.select{|e| e.first.to_s =~ /^#{dt}/}.first.last
191
+ else
192
+ nil
193
+ end
194
+ end
195
+
196
+ def holiday_date( name )
197
+ ret = unless block_given?
198
+ holiday_list.select{|e| e.last =~ /#{name}/i }.map{|e| e.first }
199
+ else
200
+ holiday_list.select{|e| yield(e) }.map{|e| e.first }
201
+ end
202
+ ret = ret.first if ret.size == 1
203
+ ret
204
+ end
205
+
206
+ end
207
+
208
+
209
+ ####
@@ -0,0 +1,64 @@
1
+ #
2
+ #
3
+ #
4
+ require 'fileutils'
5
+
6
+ module NetCache
7
+
8
+ #
9
+ # ==== Description
10
+ # ユーザは, repo_{uri,dir,file}, およびdownload_repoを実装すればよい.
11
+ #
12
+ def repo_uri
13
+ 'http://files.apple.com/calendars/Japanese32Holidays.ics'
14
+ end
15
+ def repo_dir
16
+ # "#{ENV['HOME']}/lib/ics"
17
+ File.expand_path("~/lib/ics")
18
+ end
19
+ def repo_file
20
+ "Japanese32Holidays.ics"
21
+ end
22
+ def repo_file_fullpath
23
+ File.join(repo_dir, repo_file)
24
+ end
25
+ def download_repo
26
+ ret = `curl -L '#{repo_uri}' -o '#{repo_file_fullpath}'; echo $?`.chomp.to_i
27
+ ret
28
+ end
29
+ # module_function :repo_uri, :repo_dir, :repo_file
30
+ # module_function :download_repo
31
+
32
+ #
33
+ def create_repo_dir
34
+ unless repo_dir_exist?
35
+ FileUtils.mkdir_p repo_dir
36
+ end
37
+ end
38
+ def repo_dir_exist?
39
+ File.exist? repo_dir
40
+ end
41
+ def repo_exist?
42
+ File.exist? File.join(repo_dir, repo_file)
43
+ end
44
+ # module_function :create_repo_dir
45
+ # module_function :repo_dir_exist?, :repo_exist?
46
+
47
+ #
48
+ def prepare_repo!
49
+ create_repo_dir
50
+ download_repo
51
+ end
52
+ def prepare_repo
53
+ unless repo_exist?
54
+ prepare_repo!
55
+ else
56
+ nil
57
+ end
58
+ end
59
+ # module_function :prepare_repo!, :prepare_repo
60
+
61
+ end
62
+
63
+
64
+ ####
@@ -0,0 +1,3 @@
1
+ class CalendarDays
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ #
2
+ #
3
+ #
4
+
5
+ require "calendar_days/calendar_days"
6
+ require "calendar_days/version"
7
+
8
+ ####
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: calendar_days
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - YAMAMOTO, Masayuki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-04 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: icalendar
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.4'
69
+ description: calendar_days uses webcal (ics file). the installation of icalendar.gem
70
+ is needed.
71
+ email:
72
+ - martin.route66.blues+github@gmail.com
73
+ executables:
74
+ - calendar_days
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - calendar_days.gemspec
87
+ - exe/calendar_days
88
+ - lib/calendar_days.rb
89
+ - lib/calendar_days/calendar_days.rb
90
+ - lib/calendar_days/net_cache.rb
91
+ - lib/calendar_days/version.rb
92
+ homepage: https://github.com/mephistobooks/calendar_days
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.5.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: calendar_days.gem is to know weekdays, holidays and weekends.
116
+ test_files: []