cron_calc 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
+ SHA256:
3
+ metadata.gz: 5757a8135782488cf246c22cd72ccc0aa04e00d06a726203df41205c0df79ef8
4
+ data.tar.gz: d33394dd5136e58f41a4ea7d8e6759d5e51326e61bb9bc4303d9778d6f85de60
5
+ SHA512:
6
+ metadata.gz: 708988f704dee9a33f9331c3508485c2bcc45d7184fac956c590dad69e468e8eabb282a863d391f13f655c1816ef2aae62e77b8d66db755cec4122530ddde178
7
+ data.tar.gz: 4dfc4c4c89ea7e983d95837dfb014992c2916b0cb54d731cb2f95441687cddf94bfeb90133cc180fddd4e3a96d23254819c8ec416db1498200270a67c291af5b
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+
5
+ Metrics/BlockLength:
6
+ Exclude:
7
+ - spec/cron_calc_spec.rb
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-11-27
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Jakub Miziński
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,33 @@
1
+ # CronCalc
2
+
3
+ CronCalc: A Ruby gem for calculating and analyzing scheduled CRON job occurrences and timings within specified intervals.
4
+
5
+ ## Installation
6
+
7
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mizinsky/cron_calc.
30
+
31
+ ## License
32
+
33
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/cron_calc.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/cron_calc/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'cron_calc'
7
+ spec.version = CronCalc::VERSION
8
+ spec.authors = ['Jakub Miziński']
9
+ spec.email = ['jakubmizinski@gmail.com']
10
+
11
+ spec.summary = 'calculates cron job occurrences'
12
+ spec.description = 'calculates cron job occurrences within a specified period'
13
+ spec.homepage = 'https://github.com/mizinsky/cron_calc'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.6.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/mizinsky/cron_calc'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/mizinsky/cron_calc/blob/main/CHANGELOG.md'
20
+ spec.metadata['rubygems_mfa_required'] = 'true'
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) ||
27
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
28
+ end
29
+ end
30
+ spec.bindir = 'exe'
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ['lib']
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ spec.add_dependency 'rspec', '~> 3.12'
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CronCalc
4
+ VERSION = '0.1.0'
5
+ end
data/lib/cron_calc.rb ADDED
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'cron_calc/version'
4
+ require 'time'
5
+
6
+ # The CronCalc is gem-wrapper module for the Parser class
7
+ module CronCalc
8
+ class Error < StandardError; end
9
+
10
+ def self.new(cron_string, period)
11
+ Parser.new(cron_string, period)
12
+ end
13
+
14
+ # The Parser class provides functionality to parse and calculate occurrences
15
+ # of cron jobs within a given time period. It interprets cron strings and
16
+ # calculates when cron jobs will occur
17
+ class Parser
18
+ attr_reader :cron_string, :cron_parts, :period
19
+
20
+ RANGE = {
21
+ minutes: 0..59,
22
+ hours: 0..23,
23
+ days: 1..31,
24
+ months: 1..12
25
+ }.freeze
26
+
27
+ def initialize(cron_string, period)
28
+ @cron_string = cron_string
29
+ @period = period
30
+ @cron_parts = split_cron_string
31
+ end
32
+
33
+ def occurrences
34
+ raise 'Cron expression is not supported or invalid' unless cron_string_valid?
35
+
36
+ minutes, hours, days, months, years = parse_cron_expression
37
+
38
+ years.product(months, days, hours, minutes).each_with_object([]) do |(year, month, day, hour, minute), occ|
39
+ time = Time.new(year, month, day, hour, minute)
40
+ occ << time if Date.valid_date?(year, month, day) && period.include?(time)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def split_cron_string
47
+ splitted = cron_string.split
48
+
49
+ {
50
+ minutes: splitted[0],
51
+ hours: splitted[1],
52
+ days: splitted[2],
53
+ months: splitted[3]
54
+ }
55
+ end
56
+
57
+ def parse_cron_expression
58
+ %i[minutes hours days months].map { |unit| parse_cron_part(unit) } << (period.min.year..period.max.year).to_a
59
+ end
60
+
61
+ # rubocop:disable Metrics
62
+ def parse_cron_part(time_unit)
63
+ range = RANGE[time_unit]
64
+ part = cron_parts[time_unit]
65
+
66
+ case part
67
+ when '*'
68
+ range.to_a
69
+ when /,/
70
+ part.split(',').map(&:to_i)
71
+ when /-/
72
+ (part.split('-').first.to_i..part.split('-').last.to_i).to_a
73
+ when %r{/}
74
+ range.step(part.split('/').last.to_i).to_a
75
+ else
76
+ [part.to_i]
77
+ end
78
+ end
79
+ # rubocop:enable Metrics
80
+
81
+ def cron_string_valid?
82
+ # rubocop:disable Layout/LineLength
83
+ regex = %r{\A(\*|([0-5]?\d)(,([0-5]?\d))*|(\*/\d+)|(\d+-\d+)) (\*|([01]?\d|2[0-3])(,([01]?\d|2[0-3]))*|(\*/\d+)|(\d+-\d+)) (\*|([12]?\d|3[01])(,([12]?\d|3[01]))*|(\*/\d+)|(\d+-\d+)) (\*|([1-9]|1[0-2])(,([1-9]|1[0-2]))*|(\*/\d+)|(\d+-\d+)) \*\z}
84
+ # rubocop:enable Layout/LineLength
85
+ cron_string.match?(regex)
86
+ end
87
+ end
88
+ end
data/sig/cron_calc.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module CronCalc
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cron_calc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jakub Miziński
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-12-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.12'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.12'
27
+ description: calculates cron job occurrences within a specified period
28
+ email:
29
+ - jakubmizinski@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - cron_calc.gemspec
41
+ - lib/cron_calc.rb
42
+ - lib/cron_calc/version.rb
43
+ - sig/cron_calc.rbs
44
+ homepage: https://github.com/mizinsky/cron_calc
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/mizinsky/cron_calc
49
+ source_code_uri: https://github.com/mizinsky/cron_calc
50
+ changelog_uri: https://github.com/mizinsky/cron_calc/blob/main/CHANGELOG.md
51
+ rubygems_mfa_required: 'true'
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.6.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.4.22
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: calculates cron job occurrences
71
+ test_files: []