cogsworth 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format doc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ # Specify your gem's dependencies in cogsworth.gemspec
4
+ gemspec
@@ -0,0 +1,18 @@
1
+ # Cogsworth: Natural Language Time Parser for Ruby
2
+
3
+ An attempt to make a natural language time parser for Ruby so you can convert from something like "1 hour 20 min" to value in seconds, and vice versa.
4
+
5
+ The name of this gem is derived from *Cogsworth*, the character from Beauty and the Beast, who is a clock. Get it?
6
+
7
+ ## Usage
8
+
9
+ Cogsworth.parse('2 hours 30 minutes') => 9000
10
+
11
+ Cogsworth.unparse(142510) => '1d 15h 35m 10s'
12
+
13
+ ## Acceptable Formats
14
+
15
+ '2 days 1 hour 35 minutes 25 seconds'
16
+ '2days 1hr 35min 25sec'
17
+ '2d1h35m25s'
18
+
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cogsworth/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cogsworth"
7
+ s.version = Cogsworth::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jon buda"]
10
+ s.email = ["jon.buda@gmail.com"]
11
+ s.homepage = "http://jonbuda.com"
12
+ s.summary = %q{A simple natural language time parser for Ruby}
13
+ s.description = %q{A simple natural language time parser for Ruby}
14
+
15
+ s.rubyforge_project = "cogsworth"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency('rspec', '>= 2.0.0')
23
+ end
@@ -0,0 +1,27 @@
1
+ module Cogsworth
2
+ class << self
3
+ MULTIPLIERS = {
4
+ "s" => (1),
5
+ "m" => (60),
6
+ "h" => (60 * 60),
7
+ "d" => (60 * 60 * 24)
8
+ }
9
+
10
+ def parse(string)
11
+ string.gsub(' ','').downcase.scan(/(\d+)([a-zA-Z]+)/).inject(0) do |sum, pair|
12
+ sum += pair[0].to_i * MULTIPLIERS[pair[1].slice(0,1)]
13
+ end
14
+ end
15
+
16
+ def unparse(seconds, strings=[])
17
+ return strings.join(' ') if seconds == 0
18
+
19
+ ['d','h','m','s'].each do |unit|
20
+ times = seconds/MULTIPLIERS[unit]
21
+ return unparse(seconds%MULTIPLIERS[unit], strings << "#{times}#{unit}") if times > 0
22
+ end
23
+
24
+ return unparse(seconds, strings)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module Cogsworth
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,83 @@
1
+ require "spec_helper"
2
+
3
+ describe Cogsworth do
4
+ it { should respond_to(:parse).with(1).argument }
5
+ it { should respond_to(:unparse).with(1).argument }
6
+
7
+ describe "parsing a string into seconds" do
8
+
9
+ it "parses '1m' to 60 seconds" do
10
+ Cogsworth.parse("1m").should be(60)
11
+ end
12
+
13
+ it "parses '1 min' to 60 seconds" do
14
+ Cogsworth.parse("1 min").should be(60)
15
+ end
16
+
17
+ it "parses '2m' to 120 seconds" do
18
+ Cogsworth.parse("2m").should be(120)
19
+ end
20
+
21
+ it "parses '2 minutes' to 120 seconds" do
22
+ Cogsworth.parse("2 minutes").should be(120)
23
+ end
24
+
25
+ it "parses '2 mins' to 120 seconds" do
26
+ Cogsworth.parse("2 mins").should be(120)
27
+ end
28
+
29
+ it "parses '1h' to 3600 seconds" do
30
+ Cogsworth.parse("1h").should be(3600)
31
+ end
32
+
33
+ it "parses '1h 20m' to 4800 seconds" do
34
+ Cogsworth.parse("1h 20m").should be(4800)
35
+ end
36
+
37
+ it "parses '2 days' to 172800 seconds" do
38
+ Cogsworth.parse("2 days").should be(172800)
39
+ end
40
+
41
+ it "parses '1d15h35m10s' to 142510 seconds" do
42
+ Cogsworth.parse("1d15h35m10s").should be(142510)
43
+ end
44
+
45
+ it "parses '1H10M' to 4200 seconds" do
46
+ Cogsworth.parse("1H10M").should be(4200)
47
+ end
48
+ end
49
+
50
+ describe "converting seconds into a formatted string" do
51
+ it "converts 30 seconds to 30s'" do
52
+ Cogsworth.unparse(30).should == '30s'
53
+ end
54
+
55
+ it "converts 60 seconds to '1m'" do
56
+ Cogsworth.unparse(60).should == '1m'
57
+ end
58
+
59
+ it "converts 120 seconds to '2m'" do
60
+ Cogsworth.unparse(120).should == '2m'
61
+ end
62
+
63
+ it "converts 150 seconds to '2m 30s'" do
64
+ Cogsworth.unparse(150).should == '2m 30s'
65
+ end
66
+
67
+ it "converts 156 seconds to '2m 36s'" do
68
+ Cogsworth.unparse(156).should == '2m 36s'
69
+ end
70
+
71
+ it "converts 3600 seconds to '1h'" do
72
+ Cogsworth.unparse(3600).should == '1h'
73
+ end
74
+
75
+ it "converts 86400 seconds to '1d'" do
76
+ Cogsworth.unparse(86400).should == '1d'
77
+ end
78
+
79
+ it "converts 142510 seconds to '1d 15h 35m 10s'" do
80
+ Cogsworth.unparse(142510).should == '1d 15h 35m 10s'
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'cogsworth'
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cogsworth
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Jon buda
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-22 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 0
34
+ version: 2.0.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: A simple natural language time parser for Ruby
38
+ email:
39
+ - jon.buda@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - .rspec
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - cogsworth.gemspec
53
+ - lib/cogsworth.rb
54
+ - lib/cogsworth/version.rb
55
+ - spec/cogsworth_spec.rb
56
+ - spec/spec_helper.rb
57
+ has_rdoc: true
58
+ homepage: http://jonbuda.com
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project: cogsworth
87
+ rubygems_version: 1.3.7
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: A simple natural language time parser for Ruby
91
+ test_files:
92
+ - spec/cogsworth_spec.rb
93
+ - spec/spec_helper.rb