time_range_extractor 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ae3fd0114820e0c7e12998845f431cc773db52560cd687b96d7d77cfd69fdb5
4
- data.tar.gz: 6886b6b01041131120e3eb1255f0bcf3276b3c8b261b4063eb733347eddea671
3
+ metadata.gz: bf2c1d9136b7a6848c9d825270247db561b0ba192053d073e54082be4c9de551
4
+ data.tar.gz: 2e0e4ceea8c4d6c859f8250e531180b6d673751c711b4aca6cda97e0b6be4096
5
5
  SHA512:
6
- metadata.gz: 331bd280feae421065c43a354f23c827093ecb6b947848a88e1802d1ea5fdc60277399127edb5673c4b9c344178db6b2897401f08e0aad1b14f305f5b953ea8a
7
- data.tar.gz: ab0785859f3f17a665124698a47c85463aa40d76592c7b365e84b1fafe7351ad0535df45c61b7c325e4e90c46e7918f9f27f90e50857ce016006ef649d29ee1f
6
+ metadata.gz: b31febd8ac057bf97106c33ab121ec42fa82ff5e67717130927ffd18437f5b45f9f0a6a87b0a8fcee94f6065a766962af647978cdadfe1921b24f2dec3999cce
7
+ data.tar.gz: d3dab6a41d87700bd62353c52af31e439fd4d4e63bf0fbb4bc21e9b9d65a49b06f3e96359e1bb4ecd0adba551cbf18f9714c3a638860f92e352d458a766835c5
data/.reek.yml ADDED
@@ -0,0 +1,3 @@
1
+ detectors:
2
+ IrresponsibleModule:
3
+ enabled: false
data/.rubocop.yml ADDED
@@ -0,0 +1,30 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5.3
3
+ Exclude:
4
+ - bin/*
5
+ - tmp/**/*
6
+ - Rakefile
7
+ - Gemfile
8
+ - '*.gemspec'
9
+ - Guardfile
10
+
11
+ # Prefer x > 0 vs x.positive?
12
+ Style/NumericPredicate:
13
+ EnforcedStyle: comparison
14
+
15
+ # Raise vs Fail seems to be an on-going debate with no significant impact.
16
+ # Fixing our style to raise.
17
+ Style/SignalException:
18
+ EnforcedStyle: only_raise
19
+
20
+ # Small enough project that documentation can just be in the README
21
+ Style/Documentation:
22
+ Enabled: false
23
+
24
+ # Prefer two line empty methods
25
+ Style/EmptyMethod:
26
+ EnforcedStyle: expanded
27
+
28
+ Metrics/ClassLength:
29
+ Exclude:
30
+ - test/**/*
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- time_range_extractor (0.1.0)
4
+ time_range_extractor (0.1.2)
5
5
  activesupport (> 2.0)
6
6
 
7
7
  GEM
@@ -12,20 +12,105 @@ GEM
12
12
  i18n (>= 0.7, < 2)
13
13
  minitest (~> 5.1)
14
14
  tzinfo (~> 1.1)
15
+ ast (2.4.0)
16
+ axiom-types (0.1.1)
17
+ descendants_tracker (~> 0.0.4)
18
+ ice_nine (~> 0.11.0)
19
+ thread_safe (~> 0.3, >= 0.3.1)
20
+ codeclimate-engine-rb (0.4.1)
21
+ virtus (~> 1.0)
22
+ coderay (1.1.2)
23
+ coercible (1.0.0)
24
+ descendants_tracker (~> 0.0.1)
15
25
  concurrent-ruby (1.1.5)
26
+ descendants_tracker (0.0.4)
27
+ thread_safe (~> 0.3, >= 0.3.1)
28
+ equalizer (0.0.11)
29
+ ffi (1.11.1)
30
+ formatador (0.2.5)
31
+ guard (2.15.0)
32
+ formatador (>= 0.2.4)
33
+ listen (>= 2.7, < 4.0)
34
+ lumberjack (>= 1.0.12, < 2.0)
35
+ nenv (~> 0.1)
36
+ notiffany (~> 0.0)
37
+ pry (>= 0.9.12)
38
+ shellany (~> 0.0)
39
+ thor (>= 0.18.1)
40
+ guard-compat (1.2.1)
41
+ guard-minitest (2.4.6)
42
+ guard-compat (~> 1.2)
43
+ minitest (>= 3.0)
44
+ guard-reek (1.2.0)
45
+ guard-compat (~> 1.1)
46
+ reek
47
+ guard-rubocop (1.3.0)
48
+ guard (~> 2.0)
49
+ rubocop (~> 0.20)
16
50
  i18n (1.6.0)
17
51
  concurrent-ruby (~> 1.0)
52
+ ice_nine (0.11.2)
53
+ jaro_winkler (1.5.3)
54
+ kwalify (0.7.2)
55
+ listen (3.1.5)
56
+ rb-fsevent (~> 0.9, >= 0.9.4)
57
+ rb-inotify (~> 0.9, >= 0.9.7)
58
+ ruby_dep (~> 1.2)
59
+ lumberjack (1.0.13)
60
+ method_source (0.9.2)
18
61
  minitest (5.11.3)
62
+ nenv (0.3.0)
63
+ notiffany (0.1.1)
64
+ nenv (~> 0.1)
65
+ shellany (~> 0.0)
66
+ parallel (1.17.0)
67
+ parser (2.6.3.0)
68
+ ast (~> 2.4.0)
69
+ pry (0.12.2)
70
+ coderay (~> 1.1.0)
71
+ method_source (~> 0.9.0)
72
+ psych (3.1.0)
73
+ rainbow (3.0.0)
19
74
  rake (10.5.0)
75
+ rb-fsevent (0.10.3)
76
+ rb-inotify (0.10.0)
77
+ ffi (~> 1.0)
78
+ reek (5.4.0)
79
+ codeclimate-engine-rb (~> 0.4.0)
80
+ kwalify (~> 0.7.0)
81
+ parser (>= 2.5.0.0, < 2.7, != 2.5.1.1)
82
+ psych (~> 3.1.0)
83
+ rainbow (>= 2.0, < 4.0)
84
+ rubocop (0.72.0)
85
+ jaro_winkler (~> 1.5.1)
86
+ parallel (~> 1.10)
87
+ parser (>= 2.6)
88
+ rainbow (>= 2.2.2, < 4.0)
89
+ ruby-progressbar (~> 1.7)
90
+ unicode-display_width (>= 1.4.0, < 1.7)
91
+ ruby-progressbar (1.10.1)
92
+ ruby_dep (1.5.0)
93
+ shellany (0.0.1)
94
+ thor (0.20.3)
20
95
  thread_safe (0.3.6)
21
96
  tzinfo (1.2.5)
22
97
  thread_safe (~> 0.1)
98
+ unicode-display_width (1.6.0)
99
+ virtus (1.0.5)
100
+ axiom-types (~> 0.1)
101
+ coercible (~> 1.0)
102
+ descendants_tracker (~> 0.0, >= 0.0.3)
103
+ equalizer (~> 0.0, >= 0.0.9)
23
104
 
24
105
  PLATFORMS
25
106
  ruby
26
107
 
27
108
  DEPENDENCIES
28
109
  bundler (~> 2.0)
110
+ guard (>= 2)
111
+ guard-minitest (>= 2.4)
112
+ guard-reek (>= 1.2.0)
113
+ guard-rubocop (>= 1.3.0)
29
114
  minitest (~> 5.0)
30
115
  rake (~> 10.0)
31
116
  time_range_extractor!
data/Guardfile ADDED
@@ -0,0 +1,15 @@
1
+ guard :minitest do
2
+ watch(%r{^test/(.*)\/?(.*)_test\.rb$})
3
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { 'test/time_range_extractor_test.rb' }
4
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
5
+ end
6
+
7
+ guard :rubocop, keep_failed: false do
8
+ watch(%r{.+\.rb$})
9
+ watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
10
+ end
11
+
12
+ guard 'reek' do
13
+ watch(%r{.+\.rb$})
14
+ watch('.reek')
15
+ end
@@ -1,22 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class MatchResult
2
4
  def initialize(match_data)
3
5
  @match_data = match_data
4
6
  end
5
7
 
8
+ def valid?
9
+ @match_data && (start_time.present? || end_time.present?)
10
+ end
11
+
6
12
  def start_time
7
13
  match_data[:start_time]
8
14
  end
9
15
 
10
16
  def start_period
11
17
  @start_period ||= match_data[:start_period].presence || begin
12
- start_t = start_time.to_i
13
- end_t = end_time.to_i
14
-
15
- if end_period.casecmp("pm") == 0 && (start_t > end_t || (end_t == 12 && start_t < end_t))
16
- "am"
17
- else
18
- end_period
19
- end
18
+ force_start_period_to_am? ? 'am' : end_period
20
19
  end
21
20
  end
22
21
 
@@ -52,5 +51,13 @@ class MatchResult
52
51
  start_time.present? && end_time.present?
53
52
  end
54
53
 
54
+ def force_start_period_to_am?
55
+ start_t = start_time.to_i
56
+ end_t = end_time.to_i
57
+
58
+ end_period.casecmp('pm') == 0 &&
59
+ (start_t > end_t || (end_t == 12 && start_t < end_t))
60
+ end
61
+
55
62
  attr_reader :match_data
56
63
  end
@@ -1,8 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Parse out time values from a string of text. Uses the provided date as the
4
+ # basis for the DateTime generation.
1
5
  class Parser
2
- PATTERN = Regexp.new(
3
- '(?i)((?<start_time>[0-9]{1,2}:?[0-9]{0,2}?)\s?(?<start_period>am|pm)?\s?(-|until)\s?)?(?<end_time>[0-9]{1,2}:?[0-9]{0,2})?\s?(?<end_period>am|pm)\s?(?<time_zone>[a-z][sd]t)?\b',
4
- Regexp::IGNORECASE
5
- ).freeze
6
+ PATTERN = /
7
+ (\A|\s|\() # space or round bracket, to support: "Call Jim (8-9pm)"
8
+ (
9
+ (?<start_time>[1-2]?[0-9]:?[0-9]{2}?)\s?
10
+ (?<start_period>am|pm)?\s?
11
+ (to|-|until)\s?
12
+ )?
13
+ (?<end_time>[1-2]?[0-9]:?[0-9]{2}?)?\s?
14
+ (?<end_period>am|pm)\s?
15
+ (?<time_zone>(
16
+ [ABCDEFGHIJKLMNOPRSTUVWY]
17
+ [A-Z]
18
+ [ACDEGHKLMNORSTUVW]?
19
+ [CDNSTW]?
20
+ [T]?
21
+ ))?\b
22
+ /xi.freeze
6
23
 
7
24
  def initialize(text, date: Date.current)
8
25
  @text = text
@@ -10,11 +27,16 @@ class Parser
10
27
  end
11
28
 
12
29
  def call
13
- result = PATTERN.match(@text)
14
- return nil unless result
30
+ match = PATTERN.match(@text)
31
+ result = MatchResult.new(match)
32
+ return nil unless result.valid?
33
+
34
+ time_range_from(result)
35
+ end
15
36
 
16
- match_result = MatchResult.new(result)
37
+ private
17
38
 
39
+ def time_range_from(match_result)
18
40
  start_time = time_from_string(match_result.start_time_string)
19
41
  end_time = time_from_string(match_result.end_time_string)
20
42
 
@@ -23,15 +45,16 @@ class Parser
23
45
  elsif start_time > end_time
24
46
  start_time..(end_time + 1.day)
25
47
  end
48
+ rescue ArgumentError
49
+ nil
26
50
  end
27
51
 
28
- private
29
-
30
52
  def time_from_string(string)
31
- time_parser.parse("#{@date.to_s(:db)} #{string}")
53
+ time_parser.parse(string, @date.to_time)
32
54
  end
33
55
 
56
+ # :reek:UtilityFunction so that we can optionally include ActiveSupport
34
57
  def time_parser
35
- ::Time.zone ? ::Time.zone : ::Time
58
+ ::Time.zone || ::Time
36
59
  end
37
60
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TimeRangeExtractor
2
- VERSION = "0.1.1"
4
+ VERSION = '0.1.2'
3
5
  end
@@ -1,10 +1,12 @@
1
- require "time"
2
- require "date"
3
- require "active_support/time"
1
+ # frozen_string_literal: true
4
2
 
5
- require "time_range_extractor/match_result"
6
- require "time_range_extractor/parser"
7
- require "time_range_extractor/version"
3
+ require 'time'
4
+ require 'date'
5
+ require 'active_support/time'
6
+
7
+ require 'time_range_extractor/match_result'
8
+ require 'time_range_extractor/parser'
9
+ require 'time_range_extractor/version'
8
10
 
9
11
  module TimeRangeExtractor
10
12
  class Error < StandardError; end
@@ -30,4 +30,8 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency "bundler", "~> 2.0"
31
31
  spec.add_development_dependency "rake", "~> 10.0"
32
32
  spec.add_development_dependency "minitest", "~> 5.0"
33
+ spec.add_development_dependency "guard", ">= 2"
34
+ spec.add_development_dependency "guard-minitest", ">= 2.4"
35
+ spec.add_development_dependency "guard-reek", ">= 1.2.0"
36
+ spec.add_development_dependency "guard-rubocop", ">= 1.3.0"
33
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_range_extractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Baker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-29 00:00:00.000000000 Z
11
+ date: 2019-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -66,6 +66,62 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '2.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '2.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-reek
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 1.2.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 1.2.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 1.3.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 1.3.0
69
125
  description:
70
126
  email:
71
127
  - jhubert@gmail.com
@@ -74,9 +130,12 @@ extensions: []
74
130
  extra_rdoc_files: []
75
131
  files:
76
132
  - ".gitignore"
133
+ - ".reek.yml"
134
+ - ".rubocop.yml"
77
135
  - ".travis.yml"
78
136
  - Gemfile
79
137
  - Gemfile.lock
138
+ - Guardfile
80
139
  - LICENSE.txt
81
140
  - README.md
82
141
  - Rakefile