spanner 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/{README.rdoc → README.md} +10 -9
- data/Rakefile +10 -28
- data/lib/spanner.rb +39 -40
- data/lib/spanner/version.rb +3 -0
- data/spanner.gemspec +27 -0
- metadata +76 -52
- data/VERSION +0 -1
- data/spec/spanner_spec.rb +0 -40
- data/spec/spec.opts +0 -7
- data/spec/spec_helper.rb +0 -1
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '082aae9dadaa848bff6868d920d15d9cf837c2d78409272621c0c5ce0f39f173'
|
4
|
+
data.tar.gz: e4cd05bbfa0e2e1acfa3357dc82093d5f618a3c6acbf9f5ca415298065d80b2e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0ebb8c6f50e970f2f551760079d8b81fca888d103c96edf8c953ad0386ce60a0309219975ddc925fe36f508b19bb23c9af03fa017fc8b28858c94a98402ae881
|
7
|
+
data.tar.gz: 4086710464fa7280770f7fdb8e6b081755798c8eb513d7119e774604ebe2e431261a0a242b45300e2cbf61d9525be04df71037588bae8e1206b509fa2eac5798
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 joshbuddy
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/{README.rdoc → README.md}
RENAMED
@@ -1,14 +1,15 @@
|
|
1
|
-
|
1
|
+
# Spanner
|
2
2
|
|
3
3
|
Easy way to parse natural language time spans as periods expressed in seconds. Supports float point notions of spans as well.
|
4
4
|
|
5
|
-
|
5
|
+
## Usage
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
Spanner.parse('1s')
|
10
|
-
=> 1
|
11
|
-
|
12
|
-
Spanner.parse('23 hours 12 minutes')
|
13
|
-
=> 83520
|
7
|
+
```ruby
|
8
|
+
require 'spanner'
|
14
9
|
|
10
|
+
Spanner.parse('1s')
|
11
|
+
# 1
|
12
|
+
|
13
|
+
Spanner.parse('23 hours 12 minutes')
|
14
|
+
# 83520
|
15
|
+
```
|
data/Rakefile
CHANGED
@@ -1,28 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
Jeweler::GemcutterTasks.new
|
12
|
-
rescue LoadError
|
13
|
-
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
14
|
-
end
|
15
|
-
|
16
|
-
require 'spec'
|
17
|
-
require 'spec/rake/spectask'
|
18
|
-
task :spec => 'spec:all'
|
19
|
-
namespace(:spec) do
|
20
|
-
Spec::Rake::SpecTask.new(:all) do |t|
|
21
|
-
t.spec_opts ||= []
|
22
|
-
t.spec_opts << "-rubygems -rdirge"
|
23
|
-
t.spec_opts << "--options" << "spec/spec.opts"
|
24
|
-
t.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test"
|
6
|
+
t.libs << "lib"
|
7
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
8
|
+
end
|
9
|
+
|
10
|
+
task :default => :test
|
data/lib/spanner.rb
CHANGED
@@ -1,46 +1,46 @@
|
|
1
|
-
require
|
1
|
+
require "spanner/version"
|
2
|
+
require "date"
|
2
3
|
|
3
4
|
class Spanner
|
4
|
-
|
5
5
|
ParseError = Class.new(RuntimeError)
|
6
6
|
|
7
7
|
def self.parse(str, opts = nil)
|
8
8
|
Spanner.new(opts).parse(str)
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
attr_reader :value, :raise_on_error, :from
|
12
|
-
|
12
|
+
|
13
13
|
def initialize(opts)
|
14
14
|
@value = value
|
15
15
|
@on_error = opts && opts.key?(:on_error) ? opts[:on_error] : :raise
|
16
16
|
@length_of_month = opts && opts[:length_of_month]
|
17
|
-
|
17
|
+
|
18
18
|
@from = if opts && opts.key?(:from)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
19
|
+
case opts[:from]
|
20
|
+
when :now
|
21
|
+
Time.new.to_i
|
22
|
+
else
|
23
|
+
opts[:from].to_i
|
24
|
+
end
|
25
|
+
else
|
26
|
+
0
|
27
|
+
end
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def self.days_in_month(year, month)
|
31
|
-
(Date.new(year, 12, 31) << (12-month)).day
|
31
|
+
(Date.new(year, 12, 31) << (12 - month)).day
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
def length_of_month
|
35
35
|
@length_of_month ||= Spanner.parse("#{Spanner.days_in_month(Time.new.year, Time.new.month)} days")
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def error(err)
|
39
39
|
if on_error == :raise
|
40
40
|
raise ParseError.new(err)
|
41
41
|
end
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
def parse(value)
|
45
45
|
parts = []
|
46
46
|
part_contextualized = nil
|
@@ -54,39 +54,38 @@ class Spanner
|
|
54
54
|
error "Part has already been contextualized with #{part_contextualized}"
|
55
55
|
return nil
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
if parts.empty?
|
59
59
|
parts << 1
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
# part is context
|
63
63
|
multiplier = case part
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
64
|
+
when "s", "sec", "seconds" then 1
|
65
|
+
when "h", "hours", "hrs" then 3600
|
66
|
+
when "m", "min", "minutes" then 60
|
67
|
+
when "d", "days" then 86_400
|
68
|
+
when "w", "wks", "weeks" then 604_800
|
69
|
+
when "months", "month", "M" then length_of_month
|
70
|
+
when "years", "y" then 31_556_926
|
71
|
+
when /\As/ then 1
|
72
|
+
when /\Am/ then 60
|
73
|
+
when /\Ah/ then 86_400
|
74
|
+
when /\Aw/ then 604_800
|
75
|
+
when /\AM/ then length_of_month
|
76
|
+
when /\Ay/ then 31_556_926
|
77
|
+
end
|
78
|
+
|
79
79
|
part_contextualized = part
|
80
80
|
parts << (parts.pop * multiplier)
|
81
81
|
end
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
if parts.empty?
|
85
85
|
nil
|
86
86
|
else
|
87
|
-
value = parts.inject(from) {|s, p| s += p}
|
87
|
+
value = parts.inject(from) { |s, p| s += p }
|
88
88
|
value.ceil == value ? value.ceil : value
|
89
89
|
end
|
90
90
|
end
|
91
|
-
|
92
|
-
end
|
91
|
+
end
|
data/spanner.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "spanner/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "spanner"
|
8
|
+
spec.version = Spanner::VERSION
|
9
|
+
spec.authors = ["joshbuddy"]
|
10
|
+
spec.email = ["joshbuddy@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Natural language time span parsing"
|
13
|
+
spec.description = "Natural language time span parsing"
|
14
|
+
spec.homepage = "https://github.com/joshbuddy/spanner"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
end
|
metadata
CHANGED
@@ -1,69 +1,93 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: spanner
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
version: 0.0.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
10
5
|
platform: ruby
|
11
|
-
authors:
|
12
|
-
-
|
6
|
+
authors:
|
7
|
+
- joshbuddy
|
13
8
|
autorequire:
|
14
9
|
bindir: bin
|
15
10
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
11
|
+
date: 2019-07-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
21
55
|
description: Natural language time span parsing
|
22
|
-
email:
|
56
|
+
email:
|
57
|
+
- joshbuddy@gmail.com
|
23
58
|
executables: []
|
24
|
-
|
25
59
|
extensions: []
|
26
|
-
|
27
|
-
|
28
|
-
-
|
29
|
-
|
30
|
-
-
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
31
66
|
- Rakefile
|
32
|
-
- VERSION
|
33
67
|
- lib/spanner.rb
|
34
|
-
-
|
35
|
-
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
68
|
+
- lib/spanner/version.rb
|
69
|
+
- spanner.gemspec
|
70
|
+
homepage: https://github.com/joshbuddy/spanner
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
41
74
|
post_install_message:
|
42
|
-
rdoc_options:
|
43
|
-
|
44
|
-
require_paths:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
45
77
|
- lib
|
46
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
-
requirements:
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
48
80
|
- - ">="
|
49
|
-
- !ruby/object:Gem::Version
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
-
requirements:
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
55
85
|
- - ">="
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
|
58
|
-
- 0
|
59
|
-
version: "0"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
60
88
|
requirements: []
|
61
|
-
|
62
|
-
rubyforge_project:
|
63
|
-
rubygems_version: 1.3.6
|
89
|
+
rubygems_version: 3.0.1
|
64
90
|
signing_key:
|
65
|
-
specification_version:
|
91
|
+
specification_version: 4
|
66
92
|
summary: Natural language time span parsing
|
67
|
-
test_files:
|
68
|
-
- spec/spanner_spec.rb
|
69
|
-
- spec/spec_helper.rb
|
93
|
+
test_files: []
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.0.2
|
data/spec/spanner_spec.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require ~'spec_helper'
|
2
|
-
|
3
|
-
describe Spanner do
|
4
|
-
|
5
|
-
it "should return nil for empty strings" do
|
6
|
-
Spanner.parse('').should be_nil
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should assume seconds" do
|
10
|
-
Spanner.parse('1').should == 1
|
11
|
-
end
|
12
|
-
|
13
|
-
#simple
|
14
|
-
{ '.5s' => 0.5, '1s' => 1, '1.5s' => 1.5, '1m' => 60, '1.5m' => 90, '1d' => 86400, '1.7233312d' => 148895.81568, '1M' => Spanner.days_in_month(Time.new.year, Time.new.month) * 24 * 60 * 60 }.each do |input, output|
|
15
|
-
it "should parse #{input} and return #{output}" do
|
16
|
-
Spanner.parse(input).should == output
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
#complex
|
21
|
-
{ '1m23s' => 83 }.each do |input, output|
|
22
|
-
it "should parse #{input} and return #{output}" do
|
23
|
-
Spanner.parse(input).should == output
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should let you set the length of a month" do
|
28
|
-
Spanner.parse("4 months", :length_of_month => 1234).should == 4936
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should accept time as from option" do
|
32
|
-
now = Time.new
|
33
|
-
Spanner.parse('23s', :from => now).should == now.to_i + 23
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should accept special :now as from option" do
|
37
|
-
Spanner.parse('23s', :from => :now).should == Time.new.to_i + 23
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
data/spec/spec.opts
DELETED
data/spec/spec_helper.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require ~'../lib/spanner'
|