dates_from_string 0.9.7 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -11
- data/dates_from_string.gemspec +2 -0
- data/lib/dates_from_string/version.rb +1 -1
- data/lib/dates_from_string.rb +15 -5
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a15baf7f399df239e4e2497649b6d4394d07e85
|
4
|
+
data.tar.gz: bdf8b4537603763030136f63815e8fb13634f1f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4a4151bbcbe1b5339934ac38d3665f9306299c700dbc7979cb5001a99afa41294b61618266183565ed456f5ef3e5c565f811e51b202f40c62433a6e3e83c0c5
|
7
|
+
data.tar.gz: 3df4a1d46c9f11da278f1c0f0a8281637fd94f2ac2a9a9d475e59a162b746dfa953726dfbe202e1b13f1422388763c495f6681c125ac80d0795bd346c9afd17d
|
data/README.md
CHANGED
@@ -23,25 +23,38 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
|
25
25
|
```ruby
|
26
|
-
text = "1988-1990 and 2000 and one more date
|
27
|
-
key_words = ['between','-']
|
28
|
-
|
29
|
-
dates_from_string.
|
26
|
+
text = "1988-1990 and 2000 and one more date 04.04.2015" # parsing text
|
27
|
+
key_words = ['between','-'] # you can define special separator
|
28
|
+
date_format = {date_format: :usa} # year,day,month by default year,month,day
|
29
|
+
dates_from_string = DatesFromString.new(key_words,date_format) # define DatesFromString object
|
30
|
+
dates_from_string.get_structure(text) # parsing text
|
30
31
|
|
31
32
|
#=> returns
|
32
33
|
# [{:type=>:year, :value=>"1988", :distance=>0, :key_words=>["-"]},
|
33
34
|
# {:type=>:year, :value=>"1990", :distance=>2, :key_words=>[]},
|
34
35
|
# {:type=>:year, :value=>"2000", :distance=>5, :key_words=>[]},
|
35
36
|
# {:type=>:year, :value=>"2015", :distance=>0, :key_words=>[]},
|
36
|
-
# {:type=>:
|
37
|
-
# {:type=>:
|
37
|
+
# {:type=>:day, :value=>"04", :distance=>0, :key_words=>[]},
|
38
|
+
# {:type=>:month, :value=>"04", :distance=>0, :key_words=>[]}]
|
38
39
|
|
39
40
|
:type # type of date year, month or day
|
40
41
|
:value # value of date
|
41
42
|
:distance # distance for next date type
|
42
43
|
:key_words # special words, symbols that separate dates
|
44
|
+
|
45
|
+
# find date in structure
|
46
|
+
text = "забрать машину из ремонта 2015-02-02 23:00:10"
|
47
|
+
dates_from_string = DatesFromString.new
|
48
|
+
dates_from_string.find_date(text)
|
49
|
+
|
50
|
+
#=> return
|
51
|
+
# ["2015-02-02 23:00:10"]
|
43
52
|
```
|
44
53
|
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
45
58
|
Examples:
|
46
59
|
|
47
60
|
```ruby
|
@@ -106,11 +119,11 @@ obj.get_structure(input)
|
|
106
119
|
# [{:type=>:year, :value=>"1960", :distance=>2, :key_words=>['and']},
|
107
120
|
# {:type=>:year, :value=>"1965", :distance=>0, :key_words=>[]},]
|
108
121
|
|
109
|
-
input = "In September 2011, following a change in the law extending
|
110
|
-
the presidential term from four years to six,[5] Putin announced
|
111
|
-
that he would seek a third, non-consecutive term as President in
|
112
|
-
the 2012 presidential election, an announcement which led to
|
113
|
-
large-scale protests in many Russian cities. In March 2012 he won the election,
|
122
|
+
input = "In September 2011, following a change in the law extending
|
123
|
+
the presidential term from four years to six,[5] Putin announced
|
124
|
+
that he would seek a third, non-consecutive term as President in
|
125
|
+
the 2012 presidential election, an announcement which led to
|
126
|
+
large-scale protests in many Russian cities. In March 2012 he won the election,
|
114
127
|
which was criticized for procedural irregularities, and is serving a six-year term"
|
115
128
|
dates_from_string.get_structure(input)
|
116
129
|
|
data/dates_from_string.gemspec
CHANGED
@@ -26,6 +26,8 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.bindir = "exe"
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = ["lib"]
|
29
|
+
# Only with ruby 2.0.x
|
30
|
+
spec.required_ruby_version = '~> 2.0'
|
29
31
|
|
30
32
|
spec.add_development_dependency "bundler", "~> 1.9"
|
31
33
|
spec.add_development_dependency "rake", "~> 10.0"
|
data/lib/dates_from_string.rb
CHANGED
@@ -31,8 +31,14 @@ class DatesFromString
|
|
31
31
|
] => -> string { string.to_s.split("/").reverse },
|
32
32
|
}
|
33
33
|
|
34
|
-
|
34
|
+
DATE_COUNTRY_FORMAT = {
|
35
|
+
default: -> {[:year,:month,:day]},
|
36
|
+
usa: -> {[:year,:day,:month]}
|
37
|
+
}
|
38
|
+
|
39
|
+
def initialize(key_words = [], date_format: :default)
|
35
40
|
@key_words = key_words
|
41
|
+
@date_format = date_format_by_country(date_format)
|
36
42
|
end
|
37
43
|
|
38
44
|
def find_date(string)
|
@@ -44,6 +50,10 @@ class DatesFromString
|
|
44
50
|
@clear_text.strip
|
45
51
|
end
|
46
52
|
|
53
|
+
def date_format_by_country(date_format)
|
54
|
+
DATE_COUNTRY_FORMAT[date_format.to_sym].call
|
55
|
+
end
|
56
|
+
|
47
57
|
def get_structure(string)
|
48
58
|
unless string.nil? || string.empty?
|
49
59
|
@main_arr = []
|
@@ -72,13 +82,13 @@ class DatesFromString
|
|
72
82
|
if @main_arr.size == 0
|
73
83
|
index = 0
|
74
84
|
end
|
75
|
-
add_to_structure(
|
76
|
-
add_to_structure(
|
77
|
-
add_to_structure(
|
85
|
+
add_to_structure(@date_format[0], value_full_date[0], index, next_index, data_arr)
|
86
|
+
add_to_structure(@date_format[1], value_full_date[1], index, next_index, data_arr)
|
87
|
+
add_to_structure(@date_format[2], value_full_date[2], index, next_index, data_arr)
|
78
88
|
end
|
79
89
|
|
80
90
|
if value_month_year_date
|
81
|
-
add_to_structure(:year ,value_month_year_date[0], index, next_index, data_arr)
|
91
|
+
add_to_structure(:year , value_month_year_date[0], index, next_index, data_arr)
|
82
92
|
add_to_structure(:month ,value_month_year_date[1], index, next_index, data_arr)
|
83
93
|
end
|
84
94
|
|
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: 0.
|
4
|
+
version: 1.0.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:
|
11
|
+
date: 2016-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -83,9 +83,9 @@ require_paths:
|
|
83
83
|
- lib
|
84
84
|
required_ruby_version: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
|
-
- - "
|
86
|
+
- - "~>"
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version: '0'
|
88
|
+
version: '2.0'
|
89
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
90
|
requirements:
|
91
91
|
- - ">="
|