parse-cron 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.8.7"
4
+ - "1.9.3"
data/Gemfile CHANGED
@@ -4,3 +4,4 @@ source "http://rubygems.org"
4
4
  gemspec
5
5
 
6
6
  gem "ZenTest", "4.6.0"
7
+ gem "rake"
data/License ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (C) 2013 Michael Siebert <siebertm85@googlemail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without restriction,
6
+ including without limitation the rights to use, copy, modify,
7
+ merge, publish, distribute, sublicense, and/or sell copies of
8
+ the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall
12
+ be included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ # parse-cron - parse crontab syntax & determine next scheduled run [![Build Status](https://travis-ci.org/siebertm/parse-cron.png)](https://travis-ci.org/siebertm/parse-cron)
2
+
3
+ The goal of this gem is to parse a crontab timing specification and determine when the
4
+ job should be run. It is not a scheduler, it does not run the jobs.
5
+
6
+ ## API example
7
+
8
+ ```
9
+ cron_parser = CronParser.new('30 * * * *')
10
+
11
+ # Comming times
12
+ next_comming_time = cron_parser.next(Time.now)
13
+
14
+ # Times that have been
15
+ most_resently_time = cron_parser.last(Time.now)
16
+ ```
17
+
@@ -1,4 +1,5 @@
1
1
  require 'set'
2
+ require 'date'
2
3
  #
3
4
  # Parses cron expressions and computes the next occurence of the "job"
4
5
  #
@@ -28,18 +29,18 @@ class CronParser
28
29
  end
29
30
 
30
31
  SYMBOLS = {
31
- "jan" => "0",
32
- "feb" => "1",
33
- "mar" => "2",
34
- "apr" => "3",
35
- "may" => "4",
36
- "jun" => "5",
37
- "jul" => "6",
38
- "aug" => "7",
39
- "sep" => "8",
40
- "oct" => "9",
41
- "nov" => "10",
42
- "dec" => "11",
32
+ "jan" => "1",
33
+ "feb" => "2",
34
+ "mar" => "3",
35
+ "apr" => "4",
36
+ "may" => "5",
37
+ "jun" => "6",
38
+ "jul" => "7",
39
+ "aug" => "8",
40
+ "sep" => "9",
41
+ "oct" => "10",
42
+ "nov" => "11",
43
+ "dec" => "12",
43
44
 
44
45
  "sun" => "0",
45
46
  "mon" => "1",
@@ -53,6 +54,7 @@ class CronParser
53
54
  def initialize(source,time_source = Time)
54
55
  @source = source
55
56
  @time_source = time_source
57
+ validate_source
56
58
  end
57
59
 
58
60
 
@@ -114,9 +116,9 @@ class CronParser
114
116
  else
115
117
  if SUBELEMENT_REGEX === subel
116
118
  if $5 # with range
117
- stepped_range($1.to_i..($3.to_i + 1), $5.to_i)
119
+ stepped_range($1.to_i..$3.to_i, $5.to_i)
118
120
  elsif $3 # range without step
119
- stepped_range($1.to_i..($3.to_i + 1), 1)
121
+ stepped_range($1.to_i..$3.to_i, 1)
120
122
  else # just a numeric
121
123
  [$1.to_i]
122
124
  end
@@ -211,7 +213,7 @@ class CronParser
211
213
  end
212
214
 
213
215
  def substitute_parse_symbols(str)
214
- SYMBOLS.inject(str) do |s, (symbol, replacement)|
216
+ SYMBOLS.inject(str.downcase) do |s, (symbol, replacement)|
215
217
  s.gsub(symbol, replacement)
216
218
  end
217
219
  end
@@ -237,4 +239,14 @@ class CronParser
237
239
  allowed.sort.reverse.find { |val| val < current }
238
240
  end
239
241
  end
242
+
243
+ def validate_source
244
+ unless @source.respond_to?(:split)
245
+ raise ArgumentError, 'not a valid cronline'
246
+ end
247
+ source_length = @source.split(/\s+/).length
248
+ unless source_length >= 5 && source_length <= 6
249
+ raise ArgumentError, 'not a valid cronline'
250
+ end
251
+ end
240
252
  end
@@ -1,5 +1,5 @@
1
1
  module Parse
2
2
  module Cron
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
@@ -1,7 +1,7 @@
1
1
  require "time"
2
2
  require "./spec/spec_helper"
3
3
  require "cron_parser"
4
- require "time"
4
+ require "date"
5
5
 
6
6
  def parse_date(str)
7
7
  dt = DateTime.strptime(str, "%Y-%m-%d %H:%M")
@@ -10,16 +10,16 @@ end
10
10
 
11
11
  describe "CronParser#parse_element" do
12
12
  [
13
- ["*", 0..60, (0..60).to_a],
14
- ["*/10", 0..60, [0, 10, 20, 30, 40, 50]],
15
- ["10", 0..60, [10]],
16
- ["10,30", 0..60, [10, 30]],
17
- ["10-15", 0..60, [10, 11, 12, 13, 14, 15]],
18
- ["10-40/10", 0..60, [10, 20, 30, 40]],
13
+ ["*", 0..59, (0..59).to_a],
14
+ ["*/10", 0..59, [0, 10, 20, 30, 40, 50]],
15
+ ["10", 0..59, [10]],
16
+ ["10,30", 0..59, [10, 30]],
17
+ ["10-15", 0..59, [10, 11, 12, 13, 14, 15]],
18
+ ["10-40/10", 0..59, [10, 20, 30, 40]],
19
19
  ].each do |element, range, expected|
20
20
  it "should return #{expected} for '#{element}' when range is #{range}" do
21
- parser = CronParser.new('')
22
- parser.parse_element(element, range) == expected
21
+ parser = CronParser.new('* * * * *')
22
+ parser.parse_element(element, range).first.to_a.sort.should == expected.sort
23
23
  end
24
24
  end
25
25
  end
@@ -38,10 +38,14 @@ describe "CronParser#next" do
38
38
  ["0 9 * * *", "2011-12-31 10:15", "2012-01-01 09:00"],
39
39
  ["* * 12 * *", "2010-04-15 10:15", "2010-05-12 00:00"],
40
40
  ["* * * * 1,3", "2010-04-15 10:15", "2010-04-19 00:00"],
41
+ ["* * * * MON,WED", "2010-04-15 10:15", "2010-04-19 00:00"],
41
42
  ["0 0 1 1 *", "2010-04-15 10:15", "2011-01-01 00:00"],
42
43
  ["0 0 * * 1", "2011-08-01 00:00", "2011-08-08 00:00"],
43
44
  ["0 0 * * 1", "2011-07-25 00:00", "2011-08-01 00:00"],
44
45
  ["45 23 7 3 *", "2011-01-01 00:00", "2011-03-07 23:45"],
46
+ ["0 0 1 jun *", "2013-05-14 11:20", "2013-06-01 00:00"],
47
+ ["0 0 1 may,jul *", "2013-05-14 15:00", "2013-07-01 00:00"],
48
+ ["0 0 1 MAY,JUL *", "2013-05-14 15:00", "2013-07-01 00:00"],
45
49
  ].each do |line, now, expected_next|
46
50
  it "should return #{expected_next} for '#{line}' when now is #{now}" do
47
51
  now = parse_date(now)
@@ -69,7 +73,11 @@ describe "CronParser#last" do
69
73
  ["0 9 * * *", "2012-01-01 00:15", "2011-12-31 09:00"],
70
74
  ["* * 12 * *", "2010-04-15 10:15", "2010-04-12 23:59"],
71
75
  ["* * * * 1,3", "2010-04-15 10:15", "2010-04-14 23:59"],
76
+ ["* * * * MON,WED", "2010-04-15 10:15", "2010-04-14 23:59"],
72
77
  ["0 0 1 1 *", "2010-04-15 10:15", "2010-01-01 00:00"],
78
+ ["0 0 1 jun *", "2013-05-14 11:20", "2012-06-01 00:00"],
79
+ ["0 0 1 may,jul *", "2013-05-14 15:00", "2013-05-01 00:00"],
80
+ ["0 0 1 MAY,JUL *", "2013-05-14 15:00", "2013-05-01 00:00"],
73
81
  ].each do |line, now, expected_next|
74
82
  it "should return #{expected_next} for '#{line}' when now is #{now}" do
75
83
  now = parse_date(now)
@@ -82,6 +90,16 @@ describe "CronParser#last" do
82
90
  end
83
91
  end
84
92
 
93
+ describe "CronParser#new" do
94
+ it 'should not raise error when given a valid cronline' do
95
+ expect { CronParser.new('30 * * * *') }.not_to raise_error
96
+ end
97
+
98
+ it 'should raise error when given an invalid cronline' do
99
+ expect { CronParser.new('* * * *') }.to raise_error('not a valid cronline')
100
+ end
101
+ end
102
+
85
103
  describe "time source" do
86
104
  it "should use an alternate specified time source" do
87
105
  ExtendedTime = Class.new(Time)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parse-cron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-07 00:00:00.000000000 Z
12
+ date: 2013-08-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -36,8 +36,10 @@ extra_rdoc_files: []
36
36
  files:
37
37
  - .gitignore
38
38
  - .rspec
39
+ - .travis.yml
39
40
  - Gemfile
40
- - README
41
+ - License
42
+ - README.md
41
43
  - Rakefile
42
44
  - lib/cron_parser.rb
43
45
  - lib/parse-cron.rb
@@ -69,6 +71,4 @@ rubygems_version: 1.8.23
69
71
  signing_key:
70
72
  specification_version: 3
71
73
  summary: Parses cron expressions and calculates the next occurence
72
- test_files:
73
- - spec/cron_parser_spec.rb
74
- - spec/spec_helper.rb
74
+ test_files: []
data/README DELETED
@@ -1,5 +0,0 @@
1
- # parse-cron - parse crontab syntax & determine next scheduled run
2
-
3
- The goal of this gem is to parse a crontab timing specification and determine when the
4
- job should be run. It is not a scheduler, it does not run the jobs.
5
-