booster-time_parser 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/time_parser.rb +3 -3
- data/tests/time_parser_test.rb +20 -20
- data/time_parser.gemspec +1 -1
- metadata +1 -1
data/lib/time_parser.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'time_parser/core_ext'
|
2
2
|
|
3
3
|
module TimeParser
|
4
|
-
VERSION = '0.0.
|
4
|
+
VERSION = '0.0.7'
|
5
5
|
|
6
6
|
MULTIPLIERS = [['seconds', 1],
|
7
7
|
['minutes', 60],
|
@@ -22,7 +22,7 @@ module TimeParser
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def self.to_days(string = nil)
|
25
|
-
parse(string).
|
25
|
+
parse(string).to_days
|
26
26
|
end
|
27
27
|
|
28
28
|
def self.to_weeks(string = nil)
|
@@ -30,7 +30,7 @@ module TimeParser
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def self.parse(string)
|
33
|
-
|
33
|
+
return if string.nil?
|
34
34
|
string.scan(/([\d.]+)\s+(hour|min|sec|day|week)/).collect {
|
35
35
|
|amt, multiplier| multiply(amt, multiplier) }.inject(0.0) { |total, val| total += val; total }
|
36
36
|
end
|
data/tests/time_parser_test.rb
CHANGED
@@ -3,67 +3,67 @@ require File.dirname(__FILE__) + '/test_helper'
|
|
3
3
|
class TimeParserTest < Test::Unit::TestCase
|
4
4
|
context "TimeParser Module" do
|
5
5
|
test "respond to #to_seconds" do
|
6
|
-
TimeParser.respond_to?(:to_seconds) == true
|
6
|
+
assert TimeParser.respond_to?(:to_seconds) == true
|
7
7
|
end
|
8
8
|
|
9
9
|
test "respond to #to_minutes" do
|
10
|
-
TimeParser.respond_to?(:to_minutes) == true
|
10
|
+
assert TimeParser.respond_to?(:to_minutes) == true
|
11
11
|
end
|
12
12
|
|
13
13
|
test "respond to #to_hours" do
|
14
|
-
TimeParser.respond_to?(:to_hours) == true
|
14
|
+
assert TimeParser.respond_to?(:to_hours) == true
|
15
15
|
end
|
16
16
|
|
17
17
|
test "respond to #to_days" do
|
18
|
-
TimeParser.respond_to?(:to_days) == true
|
18
|
+
assert TimeParser.respond_to?(:to_days) == true
|
19
19
|
end
|
20
20
|
|
21
21
|
test "respond to #to_weeks" do
|
22
|
-
TimeParser.respond_to?(:to_weeks) == true
|
22
|
+
assert TimeParser.respond_to?(:to_weeks) == true
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
context "Invalid/Blank Data" do
|
27
|
-
test "invalid should return
|
28
|
-
TimeParser.to_seconds('2 coconuts') == 0.0
|
27
|
+
test "invalid input should return 0 seconds" do
|
28
|
+
assert TimeParser.to_seconds('2 coconuts') == 0.0
|
29
29
|
end
|
30
30
|
|
31
31
|
test "mix of invalid and valid input should return 3600 seconds as a float" do
|
32
|
-
TimeParser.to_seconds('1 hour and 30 coconuts')
|
32
|
+
assert TimeParser.to_seconds('1 hour and 30 coconuts') == 3600.0
|
33
33
|
end
|
34
34
|
|
35
|
-
test "no input should return
|
36
|
-
TimeParser.to_seconds() ==
|
35
|
+
test "no input should return nil" do
|
36
|
+
assert TimeParser.to_seconds() == nil
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
40
|
context "Conversions" do
|
41
41
|
test "'2 hours' should equal '120.0'" do
|
42
|
-
TimeParser.to_minutes('2 hours') == 120.0
|
42
|
+
assert TimeParser.to_minutes('2 hours') == 120.0
|
43
43
|
end
|
44
44
|
|
45
|
-
test "'
|
46
|
-
TimeParser.to_days('
|
45
|
+
test "'24 hours' should equal '1.0'" do
|
46
|
+
assert TimeParser.to_days('24 hours') == 1.0
|
47
47
|
end
|
48
48
|
|
49
|
-
test "'
|
50
|
-
TimeParser.to_weeks('
|
49
|
+
test "'84 hours' should equal '0.5'" do
|
50
|
+
assert TimeParser.to_weeks('84 hours') == 0.5
|
51
51
|
end
|
52
52
|
|
53
|
-
test "'2 hours' should equal '
|
54
|
-
TimeParser.to_seconds('2 hours') ==
|
53
|
+
test "'2 hours' should equal '7200.0'" do
|
54
|
+
assert TimeParser.to_seconds('2 hours') == 7200.0
|
55
55
|
end
|
56
56
|
|
57
57
|
test "'2 hours' should equal '2.0'" do
|
58
|
-
TimeParser.to_hours('2 hours') == 2.0
|
58
|
+
assert TimeParser.to_hours('2 hours') == 2.0
|
59
59
|
end
|
60
60
|
|
61
61
|
test "'1 hour and 30 minutes' should equal '5400.0'" do
|
62
|
-
TimeParser.to_seconds('1 hour and 30 minutes') == 5400.0
|
62
|
+
assert TimeParser.to_seconds('1 hour and 30 minutes') == 5400.0
|
63
63
|
end
|
64
64
|
|
65
65
|
test "'1 hour and 30 minutes' should equal '1.5'" do
|
66
|
-
TimeParser.to_hours('1 hour and 30 minutes') == 1.5
|
66
|
+
assert TimeParser.to_hours('1 hour and 30 minutes') == 1.5
|
67
67
|
end
|
68
68
|
end
|
69
69
|
end
|
data/time_parser.gemspec
CHANGED