time_range_extractor 0.2.3 → 0.3.0

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: 4dfb2e9f8bbc4e60f202ead653a032e8d049e0f930c7671d27795417e5a325ae
4
- data.tar.gz: 2c7e454395476fcd3c488801fde084fb638bf74101ea758c4245dee77cc9a916
3
+ metadata.gz: 1191ee60eb66950fd5e8dceb2276c2938a78f9ee2bfa70b013a3e67a0e0a085a
4
+ data.tar.gz: 9dce17aa803cece9005de6cb45eb59ed5ec77a66936a0e4eb0aa6a9e566dc7f2
5
5
  SHA512:
6
- metadata.gz: 72507f02f55d7d995f61f69e61dfa5e2ff3e5e9ae2b82a20d9a237c4388f137ac11625ee892421560a8a75b17dc995c4d6c61fd13ab2ab24d510c340123fe2b0
7
- data.tar.gz: 7dc407b9e440a2a81a7d74d12dba0156d345f2668f2dd6182fbb08c3484d2dbe53eebdda5597d6c21325a0df4de920a2a0f07fb0fb26db49f754ff561c3fbc4b
6
+ metadata.gz: 0cbd6549f7662daa73134c48862d30711416aaa2ef28d0a8e312b608b9e05f5f689bc8fed69aabdc10c34d05a7f120149e9d4e7289976bcf3aca990fff00ef90
7
+ data.tar.gz: 1b9f708e8551d23950d6f00e3f67525f9e5566aadad7e11861b33fbb57037b1141eb973ab1d1856512a763fe77c65c189c3efb5fb6f624dc14b26e762775e601
@@ -1,57 +1,59 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class MatchResult
4
- def initialize(match_data)
5
- @match_data = match_data
6
- end
3
+ module TimeRangeExtractor
4
+ class MatchResult
5
+ def initialize(match_data)
6
+ @match_data = match_data
7
+ end
7
8
 
8
- def valid?
9
- @match_data && (start_time.present? && end_time.present?)
10
- end
9
+ def valid?
10
+ @match_data && (start_time.present? && end_time.present?)
11
+ end
11
12
 
12
- def start_time
13
- match_data[:start_time]
14
- end
13
+ def start_time
14
+ match_data[:start_time]
15
+ end
15
16
 
16
- def start_period
17
- @start_period ||= match_data[:start_period].presence || begin
18
- force_start_period_to_am? ? 'am' : end_period
17
+ def start_period
18
+ @start_period ||= match_data[:start_period].presence || begin
19
+ force_start_period_to_am? ? 'am' : end_period
20
+ end
19
21
  end
20
- end
21
22
 
22
- def end_time
23
- match_data[:end_time]
24
- end
23
+ def end_time
24
+ match_data[:end_time]
25
+ end
25
26
 
26
- def end_period
27
- match_data[:end_period]
28
- end
27
+ def end_period
28
+ match_data[:end_period]
29
+ end
29
30
 
30
- def time_zone
31
- match_data[:time_zone]
32
- end
31
+ def time_zone
32
+ match_data[:time_zone]
33
+ end
33
34
 
34
- def start_time_string
35
- [start_time, start_period, time_zone].compact.join(' ')
36
- end
35
+ def start_time_string
36
+ [start_time, start_period, time_zone].compact.join(' ')
37
+ end
37
38
 
38
- def end_time_string
39
- [end_time, end_period, time_zone].compact.join(' ')
40
- end
39
+ def end_time_string
40
+ [end_time, end_period, time_zone].compact.join(' ')
41
+ end
41
42
 
42
- private
43
+ private
43
44
 
44
- def range?
45
- start_time.present? && end_time.present?
46
- end
45
+ def range?
46
+ start_time.present? && end_time.present?
47
+ end
47
48
 
48
- def force_start_period_to_am?
49
- start_t = start_time.to_i
50
- end_t = end_time.to_i
49
+ def force_start_period_to_am?
50
+ start_t = start_time.to_i
51
+ end_t = end_time.to_i
51
52
 
52
- end_period.casecmp('pm') == 0 &&
53
- (start_t > end_t || (end_t == 12 && start_t < end_t))
54
- end
53
+ end_period.casecmp('pm') == 0 &&
54
+ (start_t > end_t || (end_t == 12 && start_t < end_t))
55
+ end
55
56
 
56
- attr_reader :match_data
57
+ attr_reader :match_data
58
+ end
57
59
  end
@@ -2,59 +2,61 @@
2
2
 
3
3
  # Parse out time values from a string of text. Uses the provided date as the
4
4
  # basis for the DateTime generation.
5
- class Parser
6
- PATTERN = /
7
- (\A|\s|\() # space or round bracket, to support: "Call Jim (8-9pm)"
8
- (
9
- (?<start_time>(2[0-4]|1[0-9]|[1-9]):?([0-5][0-9])?)\s?
10
- (?<start_period>am|pm)?\s?
11
- (to|-|until|\s)\s?
12
- )
13
- (?<end_time>(2[0-4]|1[0-9]|[1-9]):?([0-5][0-9])?)?\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
23
-
24
- def initialize(text, date: Date.current)
25
- @text = text
26
- @date = date
27
- end
5
+ module TimeRangeExtractor
6
+ class Parser
7
+ PATTERN = /
8
+ (\A|\s|\() # space or round bracket, to support: "Call Jim (8-9pm)"
9
+ (
10
+ (?<start_time>(2[0-4]|1[0-9]|[1-9]):?([0-5][0-9])?)\s?
11
+ (?<start_period>am|pm)?\s?
12
+ (to|-|until|\s)\s?
13
+ )
14
+ (?<end_time>(2[0-4]|1[0-9]|[1-9]):?([0-5][0-9])?)?\s?
15
+ (?<end_period>am|pm)\s?
16
+ (?<time_zone>(
17
+ [ABCDEFGHIJKLMNOPRSTUVWY]
18
+ [A-Z]
19
+ [ACDEGHKLMNORSTUVW]?
20
+ [CDNSTW]?
21
+ [T]?
22
+ ))?\b
23
+ /xi.freeze
24
+
25
+ def initialize(text, date: Date.current)
26
+ @text = text
27
+ @date = date
28
+ end
28
29
 
29
- def call
30
- match = PATTERN.match(@text)
31
- result = MatchResult.new(match)
32
- return nil unless result.valid?
30
+ def call
31
+ match = PATTERN.match(@text)
32
+ result = MatchResult.new(match)
33
+ return nil unless result.valid?
33
34
 
34
- time_range_from(result)
35
- end
35
+ time_range_from(result)
36
+ end
36
37
 
37
- private
38
+ private
38
39
 
39
- def time_range_from(match_result)
40
- start_time = time_from_string(match_result.start_time_string)
41
- end_time = time_from_string(match_result.end_time_string)
40
+ def time_range_from(match_result)
41
+ start_time = time_from_string(match_result.start_time_string)
42
+ end_time = time_from_string(match_result.end_time_string)
42
43
 
43
- if start_time <= end_time
44
- start_time..end_time
45
- elsif start_time > end_time
46
- start_time..(end_time + 1.day)
44
+ if start_time <= end_time
45
+ start_time..end_time
46
+ elsif start_time > end_time
47
+ start_time..(end_time + 1.day)
48
+ end
49
+ rescue ArgumentError
50
+ nil
47
51
  end
48
- rescue ArgumentError
49
- nil
50
- end
51
52
 
52
- def time_from_string(string)
53
- time_parser.parse(string, @date.to_time)
54
- end
53
+ def time_from_string(string)
54
+ time_parser.parse(string, @date.to_time)
55
+ end
55
56
 
56
- # :reek:UtilityFunction so that we can optionally include ActiveSupport
57
- def time_parser
58
- ::Time.zone || ::Time
57
+ # :reek:UtilityFunction so that we can optionally include ActiveSupport
58
+ def time_parser
59
+ ::Time.zone || ::Time
60
+ end
59
61
  end
60
62
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TimeRangeExtractor
4
- VERSION = '0.2.3'
4
+ VERSION = '0.3.0'
5
5
  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.2.3
4
+ version: 0.3.0
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-30 00:00:00.000000000 Z
11
+ date: 2019-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport