medjool 0.5.1 → 0.5.2
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/lib/medjool/parser.rb +12 -1
- data/lib/medjool/regexs.rb +3 -1
- data/lib/medjool.rb +1 -1
- data/test/test_medjool.rb +8 -1
- metadata +3 -3
data/lib/medjool/parser.rb
CHANGED
|
@@ -38,7 +38,18 @@ class Medjool::Parser
|
|
|
38
38
|
text = "#{text.strip}st"
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
begin
|
|
42
|
+
base_date = Date.parse(text)
|
|
43
|
+
rescue ArgumentError => e
|
|
44
|
+
# If Date.parse is used on the string "31st" during
|
|
45
|
+
# a month with only 30 days, it will explode
|
|
46
|
+
if Medjool::END_OF_MONTH_MATCHER.match(text)
|
|
47
|
+
# This is a little bit hacky, but switch to a date that will definitely work
|
|
48
|
+
base_date = Date.parse("#{text} January")
|
|
49
|
+
else
|
|
50
|
+
return nil
|
|
51
|
+
end
|
|
52
|
+
end
|
|
42
53
|
|
|
43
54
|
unless @context[:now]
|
|
44
55
|
# In the absence of context, always fall back on the default guess
|
data/lib/medjool/regexs.rb
CHANGED
|
@@ -4,7 +4,9 @@ module Medjool
|
|
|
4
4
|
MONTH_MATCHER = /\s*#{MONTH_NAME_MATCHER},?\s*/
|
|
5
5
|
YYYY_MM_DD_MATCHER = /(\s*[0-9]{4}-[0-9]{2}-[0-9]{2}\s*)/
|
|
6
6
|
DM_DM_YYYY_MATCHER = /(\s*[0-9]{2}[-\/][0-9]{2}[-\/][0-9]{2,4}\s*)/
|
|
7
|
-
|
|
7
|
+
ORDINAL_POSTFIX = /(st|rd|nd|th)/
|
|
8
|
+
ORDINAL_MATCHER = /(\s*([0-9]{1,2}#{ORDINAL_POSTFIX}?),?\s*)/
|
|
9
|
+
END_OF_MONTH_MATCHER = /^\s*(29|30|31)#{ORDINAL_POSTFIX}?\s*$/
|
|
8
10
|
YEAR_MATCHER = /(\s*([0-9]{4}\s*|\s*'?[0-9]{2}),?\s*)/
|
|
9
11
|
TEXT_DATE_MATCHER = /(#{DAYNAME_MATCHER}|#{ORDINAL_MATCHER}|#{YEAR_MATCHER}|#{MONTH_MATCHER})/
|
|
10
12
|
DATE_MATCHER = /^[^a-zA-Z0-9]*(#{TEXT_DATE_MATCHER}+|#{DM_DM_YYYY_MATCHER}|#{YYYY_MM_DD_MATCHER})/
|
data/lib/medjool.rb
CHANGED
data/test/test_medjool.rb
CHANGED
|
@@ -37,6 +37,9 @@ class TestMedjool < TestCase
|
|
|
37
37
|
def test_ordinal_only
|
|
38
38
|
assert_equal "2013-09-01".to_date, Medjool.parse("1st", {:now => "2013-08-23".to_date})
|
|
39
39
|
assert_equal "2013-08-01".to_date, Medjool.parse("1st", {:now => "2013-08-01".to_date})
|
|
40
|
+
|
|
41
|
+
assert_equal "2013-10-31".to_date, Medjool.parse("31st", {:now => "2013-09-01".to_date})
|
|
42
|
+
assert_equal "2013-09-30".to_date, Medjool.parse("30th", {:now => "2013-09-01".to_date})
|
|
40
43
|
end
|
|
41
44
|
|
|
42
45
|
def test_day_only
|
|
@@ -69,7 +72,6 @@ class TestMedjool < TestCase
|
|
|
69
72
|
assert_equal "2013-01-12".to_date, date_range.start_date
|
|
70
73
|
assert_equal "2013-01-15".to_date, date_range.end_date
|
|
71
74
|
assert parser.parse_date_range("October 15").nil?
|
|
72
|
-
|
|
73
75
|
end
|
|
74
76
|
|
|
75
77
|
def test_lone_numbers
|
|
@@ -77,4 +79,9 @@ class TestMedjool < TestCase
|
|
|
77
79
|
assert_equal Date.parse("1st"), parser.parse("1")
|
|
78
80
|
assert_equal Date.parse("2st"), parser.parse("2 ")
|
|
79
81
|
end
|
|
82
|
+
|
|
83
|
+
def test_invalid_dates
|
|
84
|
+
assert_equal nil, Medjool.parse("Blah")
|
|
85
|
+
assert_equal nil, Medjool.parse("31st February")
|
|
86
|
+
end
|
|
80
87
|
end
|
metadata
CHANGED