dates_from_string 1.2.4 → 1.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
- SHA1:
3
- metadata.gz: 7662e99ace9fe3f7108daf45b833ffaaac7843ac
4
- data.tar.gz: e7c4679c1ef9e8ff081c56867db6a49719a29052
2
+ SHA256:
3
+ metadata.gz: 4cb8ac6233b9bb96084df2a43b1ccf83144f5321785575ca308c395c3b40bd97
4
+ data.tar.gz: ea4ed89852e3ce67aeaf675f66b49bcf3d90f50efa52b2eb0989d2a0f05c23fb
5
5
  SHA512:
6
- metadata.gz: c6466abf9543ba69ea65d1aa8a70c55a868dff061b590ee6da850bd1732d3127d8459abb8ae907c0675ffa9b45eaf8d502ecd0536b7d1d1b96b77bb982b46c9f
7
- data.tar.gz: 919b454cebf0c9db80a67b75cb2225ceb391fa092025fe665e49072d6db3ec90bfdd195418f58a0ba5a69049f52bbc7fce12634d1886471647f7c510049e4776
6
+ metadata.gz: 0260d1045bdd5b6b9aa884582f0dff2548aaac6ab1dc60e1768fc02f747f3d0419ee9a283df37177ed99eee78cc14ce3a7073dbd5f72056689194dd9053ea33b
7
+ data.tar.gz: f3b47d374c1afc2e169c741dfb981c08b13c36bdf01c9bbdba8cb89442485586e9c51a3445f3b387eb9ecec0aa205d7911c560d03b616b91f55024ff05a1fb02
@@ -1,4 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.1
4
- before_install: gem install bundler -v 1.13.6
3
+ - 2.6.3
4
+ before_install: gem install bundler -v 2.0
data/README.md CHANGED
@@ -27,8 +27,11 @@ Or install it yourself as:
27
27
  ```ruby
28
28
  text = "1988-1990 and 2000 and one more date 04.04.2015" # parsing text
29
29
  key_words = ['between','-'] # you can define special separator
30
- date_format = {date_format: :usa} # year,day,month by default year,month,day
31
- dates_from_string = DatesFromString.new(key_words,date_format) # define DatesFromString object
30
+ options = {
31
+ date_format: :usa, # year,day,month by default year,month,day
32
+ ordinals: ['nd', 'st', 'th'] # a string list that might accompany a day, default none
33
+ }
34
+ dates_from_string = DatesFromString.new(key_words, options) # define DatesFromString object
32
35
  dates_from_string.get_structure(text) # parsing text
33
36
 
34
37
  #=> returns
@@ -129,6 +132,15 @@ obj.get_structure(input)
129
132
  # [{:type=>:year, :value=>"1960", :distance=>2, :key_words=>['and']},
130
133
  # {:type=>:year, :value=>"1965", :distance=>0, :key_words=>[]},]
131
134
 
135
+ dates_from_string_with_ordinals = DatesFromString.new(['and'], ordinals: ['st', 'th', 'nd'])
136
+ input = 'around September 2nd, 2013'
137
+ dates_from_string_with_ordinals.get_structure(input)
138
+
139
+ #=> return
140
+ # [{:type=>:month, :value=>"09", :distance=>1, :key_words=>[]},
141
+ # {:type=>:day, :value=>"2", :distance=>1, :key_words=>[]},
142
+ # {:type=>:year, :value=>"2013", :distance=>0, :key_words=>[]}]
143
+
132
144
  input = "In September 2011, following a change in the law extending
133
145
  the presidential term from four years to six,[5] Putin announced
134
146
  that he would seek a third, non-consecutive term as President in
@@ -29,9 +29,9 @@ Gem::Specification.new do |spec|
29
29
  # Only with ruby 2.0.x
30
30
  spec.required_ruby_version = '~> 2.0'
31
31
 
32
- spec.add_development_dependency "bundler", "~> 1.13.6"
33
- spec.add_development_dependency "rake", "~> 10.0"
34
- spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "bundler", "~> 2.0"
33
+ spec.add_development_dependency "rake", "~> 13.0"
34
+ spec.add_development_dependency "rspec", "~> 3.9"
35
35
 
36
36
 
37
37
  spec.add_development_dependency 'pry-rails'
@@ -5,9 +5,10 @@ require 'dates_from_string/patterns'
5
5
  class DatesFromString
6
6
  include Patterns
7
7
 
8
- def initialize(key_words = [], date_format: :default)
8
+ def initialize(key_words = [], date_format: :default, ordinals: [])
9
9
  @key_words = key_words
10
10
  @date_format = date_format_by_country(date_format)
11
+ @ordinals = Array(ordinals)
11
12
  end
12
13
 
13
14
  def find_date(string)
@@ -27,6 +28,17 @@ class DatesFromString
27
28
  DATE_COUNTRY_FORMAT[date_format.to_sym].call
28
29
  end
29
30
 
31
+ def day_regex
32
+ @day_regex ||= begin
33
+ if @ordinals.any?
34
+ ordinal_regex = "(#{@ordinals.join('|')})?"
35
+ /^\d{1,2}#{ordinal_regex},?$/
36
+ else
37
+ /^\d{1,2},?$/
38
+ end
39
+ end
40
+ end
41
+
30
42
  def get_structure(string)
31
43
  unless string.nil? || string.empty?
32
44
  @main_arr = []
@@ -142,8 +154,8 @@ class DatesFromString
142
154
  end
143
155
 
144
156
  def get_day(string)
145
- if string =~ (/^\d{2}$/)
146
- string
157
+ if string =~ (day_regex)
158
+ string.gsub(Regexp.union(@ordinals + [',']), '')
147
159
  else
148
160
  nil
149
161
  end
@@ -162,7 +174,7 @@ class DatesFromString
162
174
 
163
175
  def get_month_by_list(string)
164
176
  month = ['January','February','March','April','May','June','July','August','September','October','November','December']
165
- index = month.index(string)
177
+ index = month.index(string.chomp(','))
166
178
 
167
179
  if index
168
180
  sprintf('%02d',(index+1))
@@ -174,7 +186,7 @@ class DatesFromString
174
186
 
175
187
  def get_short_month(string)
176
188
  short_month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
177
- short_index = short_month.index(string)
189
+ short_index = short_month.index(string.chomp(','))
178
190
 
179
191
  if short_index
180
192
  sprintf('%02d',(short_index+1))
@@ -1,3 +1,3 @@
1
1
  class DatesFromString
2
- VERSION = "1.2.4"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dates_from_string
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.4
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Chechaev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-02 00:00:00.000000000 Z
11
+ date: 2019-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,42 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.13.6
19
+ version: '2.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.13.6
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '3.9'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
54
+ version: '3.9'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: pry-rails
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -107,8 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  - !ruby/object:Gem::Version
108
108
  version: '0'
109
109
  requirements: []
110
- rubyforge_project:
111
- rubygems_version: 2.5.1
110
+ rubygems_version: 3.0.3
112
111
  signing_key:
113
112
  specification_version: 4
114
113
  summary: Flexible solution for finding all formats of dates in text