cogsworth 1.0.1 → 1.0.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/README.md +3 -3
- data/lib/cogsworth.rb +4 -4
- data/lib/cogsworth/version.rb +1 -1
- data/spec/cogsworth_spec.rb +20 -20
- metadata +6 -6
data/README.md
CHANGED
@@ -6,11 +6,11 @@ The name of this gem is derived from *Cogsworth*, the character from Beauty and
|
|
6
6
|
|
7
7
|
## Usage
|
8
8
|
|
9
|
-
Cogsworth.
|
9
|
+
Cogsworth.string_to_seconds('2 hours 30 minutes') => 9000
|
10
10
|
|
11
|
-
Cogsworth.
|
11
|
+
Cogsworth.seconds_to_string(142510) => '1d 15h 35m 10s'
|
12
12
|
|
13
|
-
## Acceptable Formats
|
13
|
+
## Acceptable String Formats
|
14
14
|
|
15
15
|
'2 days 1 hour 35 minutes 25 seconds'
|
16
16
|
'2days 1hr 35min 25sec'
|
data/lib/cogsworth.rb
CHANGED
@@ -7,21 +7,21 @@ module Cogsworth
|
|
7
7
|
"d" => (60 * 60 * 24)
|
8
8
|
}
|
9
9
|
|
10
|
-
def
|
10
|
+
def string_to_seconds(string)
|
11
11
|
string.gsub(' ','').downcase.scan(/(\d+)([a-zA-Z]+)/).inject(0) do |sum, pair|
|
12
12
|
sum += pair[0].to_i * MULTIPLIERS[pair[1].slice(0,1)]
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
16
|
+
def seconds_to_string(seconds, results=[])
|
17
17
|
return results.join(' ') if seconds == 0
|
18
18
|
|
19
19
|
['d','h','m','s'].each do |unit|
|
20
20
|
occurrences = seconds/MULTIPLIERS[unit]
|
21
|
-
return
|
21
|
+
return seconds_to_string(seconds%MULTIPLIERS[unit], results.push("#{occurrences}#{unit}")) if occurrences > 0
|
22
22
|
end
|
23
23
|
|
24
|
-
return
|
24
|
+
return seconds_to_string(seconds, results)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
data/lib/cogsworth/version.rb
CHANGED
data/spec/cogsworth_spec.rb
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe Cogsworth do
|
4
|
-
it { should respond_to(:
|
5
|
-
it { should respond_to(:
|
4
|
+
it { should respond_to(:string_to_seconds).with(1).argument }
|
5
|
+
it { should respond_to(:seconds_to_string).with(1).argument }
|
6
6
|
|
7
7
|
describe "parsing a string into seconds" do
|
8
8
|
|
9
9
|
it "parses '1m' to 60 seconds" do
|
10
|
-
Cogsworth.
|
10
|
+
Cogsworth.string_to_seconds("1m").should be(60)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "parses '1 min' to 60 seconds" do
|
14
|
-
Cogsworth.
|
14
|
+
Cogsworth.string_to_seconds("1 min").should be(60)
|
15
15
|
end
|
16
16
|
|
17
17
|
it "parses '2m' to 120 seconds" do
|
18
|
-
Cogsworth.
|
18
|
+
Cogsworth.string_to_seconds("2m").should be(120)
|
19
19
|
end
|
20
20
|
|
21
21
|
it "parses '2 minutes' to 120 seconds" do
|
22
|
-
Cogsworth.
|
22
|
+
Cogsworth.string_to_seconds("2 minutes").should be(120)
|
23
23
|
end
|
24
24
|
|
25
25
|
it "parses '2 mins' to 120 seconds" do
|
26
|
-
Cogsworth.
|
26
|
+
Cogsworth.string_to_seconds("2 mins").should be(120)
|
27
27
|
end
|
28
28
|
|
29
29
|
it "parses '1h' to 3600 seconds" do
|
30
|
-
Cogsworth.
|
30
|
+
Cogsworth.string_to_seconds("1h").should be(3600)
|
31
31
|
end
|
32
32
|
|
33
33
|
it "parses '1h 20m' to 4800 seconds" do
|
34
|
-
Cogsworth.
|
34
|
+
Cogsworth.string_to_seconds("1h 20m").should be(4800)
|
35
35
|
end
|
36
36
|
|
37
37
|
it "parses '2 days' to 172800 seconds" do
|
38
|
-
Cogsworth.
|
38
|
+
Cogsworth.string_to_seconds("2 days").should be(172800)
|
39
39
|
end
|
40
40
|
|
41
41
|
it "parses '1d15h35m10s' to 142510 seconds" do
|
42
|
-
Cogsworth.
|
42
|
+
Cogsworth.string_to_seconds("1d15h35m10s").should be(142510)
|
43
43
|
end
|
44
44
|
|
45
45
|
it "parses '1H10M' to 4200 seconds" do
|
46
|
-
Cogsworth.
|
46
|
+
Cogsworth.string_to_seconds("1H10M").should be(4200)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
50
|
describe "converting seconds into a formatted string" do
|
51
51
|
it "converts 30 seconds to 30s'" do
|
52
|
-
Cogsworth.
|
52
|
+
Cogsworth.seconds_to_string(30).should == '30s'
|
53
53
|
end
|
54
54
|
|
55
55
|
it "converts 60 seconds to '1m'" do
|
56
|
-
Cogsworth.
|
56
|
+
Cogsworth.seconds_to_string(60).should == '1m'
|
57
57
|
end
|
58
58
|
|
59
59
|
it "converts 120 seconds to '2m'" do
|
60
|
-
Cogsworth.
|
60
|
+
Cogsworth.seconds_to_string(120).should == '2m'
|
61
61
|
end
|
62
62
|
|
63
63
|
it "converts 150 seconds to '2m 30s'" do
|
64
|
-
Cogsworth.
|
64
|
+
Cogsworth.seconds_to_string(150).should == '2m 30s'
|
65
65
|
end
|
66
66
|
|
67
67
|
it "converts 156 seconds to '2m 36s'" do
|
68
|
-
Cogsworth.
|
68
|
+
Cogsworth.seconds_to_string(156).should == '2m 36s'
|
69
69
|
end
|
70
70
|
|
71
71
|
it "converts 3600 seconds to '1h'" do
|
72
|
-
Cogsworth.
|
72
|
+
Cogsworth.seconds_to_string(3600).should == '1h'
|
73
73
|
end
|
74
74
|
|
75
75
|
it "converts 86400 seconds to '1d'" do
|
76
|
-
Cogsworth.
|
76
|
+
Cogsworth.seconds_to_string(86400).should == '1d'
|
77
77
|
end
|
78
78
|
|
79
79
|
it "converts 142510 seconds to '1d 15h 35m 10s'" do
|
80
|
-
Cogsworth.
|
80
|
+
Cogsworth.seconds_to_string(142510).should == '1d 15h 35m 10s'
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cogsworth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jon buda
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-05-31 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
84
|
requirements: []
|
85
85
|
|
86
86
|
rubyforge_project: cogsworth
|
87
|
-
rubygems_version: 1.
|
87
|
+
rubygems_version: 1.6.2
|
88
88
|
signing_key:
|
89
89
|
specification_version: 3
|
90
90
|
summary: A simple natural language time parser for Ruby
|