Class: DatesFromString
- Inherits:
-
Object
- Object
- DatesFromString
- Includes:
- Patterns
- Defined in:
- lib/dates_from_string.rb,
lib/dates_from_string/version.rb
Constant Summary collapse
- VERSION =
"1.3.0"
Constants included from Patterns
Patterns::DATE_COUNTRY_FORMAT, Patterns::PATTERNS
Instance Method Summary collapse
- #add_to_structure(type, value, index, next_index, data_arr, key_word = nil) ⇒ Object
- #calc_index(index) ⇒ Object
- #date_format_by_country(date_format) ⇒ Object
- #day_regex ⇒ Object
- #email_date(email) ⇒ Object
- #find_date(string) ⇒ Object
- #get_clear_text ⇒ Object
- #get_dash_data(string) ⇒ Object
- #get_day(string) ⇒ Object
- #get_full_date(string) ⇒ Object
- #get_month_by_list(string) ⇒ Object
- #get_month_year_date(string) ⇒ Object
- #get_short_month(string) ⇒ Object
- #get_structure(string) ⇒ Object
- #get_time(string) ⇒ Object
- #get_year(string) ⇒ Object
-
#initialize(key_words = [], date_format: :default, ordinals: []) ⇒ DatesFromString
constructor
A new instance of DatesFromString.
- #set_structura ⇒ Object
Constructor Details
#initialize(key_words = [], date_format: :default, ordinals: []) ⇒ DatesFromString
Returns a new instance of DatesFromString.
8 9 10 11 12 |
# File 'lib/dates_from_string.rb', line 8 def initialize(key_words = [], date_format: :default, ordinals: []) @key_words = key_words @date_format = date_format_by_country(date_format) @ordinals = Array(ordinals) end |
Instance Method Details
#add_to_structure(type, value, index, next_index, data_arr, key_word = nil) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/dates_from_string.rb', line 198 def add_to_structure (type ,value, index, next_index, data_arr, key_word = nil) set_structura if value @first_index << index @structura[:type] = type @structura[:value] = value end if value && @main_arr.last @main_arr.last[:distance] = calc_index(index) end if @key_words && @key_words.include?(data_arr[next_index]) @structura[:key_words] << data_arr[next_index] end if key_word @structura[:key_words] << key_word end if value @main_arr << @structura value = nil end end |
#calc_index(index) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/dates_from_string.rb', line 224 def calc_index(index) result = nil @indexs << index if @indexs.count > 1 result = (index - @indexs[-2]) elsif @first_index[0] < index result = (index - @first_index[0]) else result = index end end |
#date_format_by_country(date_format) ⇒ Object
27 28 29 |
# File 'lib/dates_from_string.rb', line 27 def date_format_by_country(date_format) DATE_COUNTRY_FORMAT[date_format.to_sym].call end |
#day_regex ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/dates_from_string.rb', line 31 def day_regex @day_regex ||= begin if @ordinals.any? ordinal_regex = "(#{@ordinals.join('|')})?" /^\d{1,2}#{ordinal_regex},?$/ else /^\d{1,2},?$/ end end end |
#email_date(email) ⇒ Object
23 24 25 |
# File 'lib/dates_from_string.rb', line 23 def email_date(email) email.match(/(?:(?:19|20)[0-9]{2})/).to_s end |
#find_date(string) ⇒ Object
14 15 16 17 |
# File 'lib/dates_from_string.rb', line 14 def find_date(string) parsing_structure = ParsingStructure.new(get_structure(string)) parsing_structure.start end |
#get_clear_text ⇒ Object
19 20 21 |
# File 'lib/dates_from_string.rb', line 19 def get_clear_text @clear_text.strip end |
#get_dash_data(string) ⇒ Object
165 166 167 168 169 170 171 172 173 |
# File 'lib/dates_from_string.rb', line 165 def get_dash_data(string) if (result = string.match(/\d{4}-\d{4}/)) result.to_s.split("-") elsif (result = string.match(/\d{4}–\d{4}/)) result.to_s.split("–") else nil end end |
#get_day(string) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/dates_from_string.rb', line 156 def get_day(string) if string =~ (day_regex) string.gsub(Regexp.union(@ordinals + [',']), '') else nil end end |
#get_full_date(string) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/dates_from_string.rb', line 133 def get_full_date(string) PATTERNS.keys.each do |patterns| patterns.each do |pattern| if (result = string.match(pattern)) @clear_text.slice!(result.to_s) return PATTERNS[patterns].call result end end end nil end |
#get_month_by_list(string) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/dates_from_string.rb', line 175 def get_month_by_list(string) month = ['January','February','March','April','May','June','July','August','September','October','November','December'] index = month.index(string.chomp(',')) if index sprintf('%02d',(index+1)) else nil end end |
#get_month_year_date(string) ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/dates_from_string.rb', line 146 def get_month_year_date(string) if (result = string.match(/^\d{2}\.\d{4}$/)) result.to_s.split(".").reverse elsif (result = string.match(/^\d{4}\.\d{2}$/)) result.to_s.split(".") else nil end end |
#get_short_month(string) ⇒ Object
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/dates_from_string.rb', line 187 def get_short_month(string) short_month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] short_index = short_month.index(string.chomp(',')) if short_index sprintf('%02d',(short_index+1)) else nil end end |
#get_structure(string) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dates_from_string.rb', line 42 def get_structure(string) unless string.nil? || string.empty? @main_arr = [] data_arr = string.split(" ") @indexs = [] @first_index = [] @clear_text = string.clone data_arr.each_with_index do |data, index| value_year = get_year(data) value_full_date = get_full_date(data) value_month_year_date = get_month_year_date(data) value_dash = get_dash_data(data) value_month = get_month_by_list(data) value_short_month = get_short_month(data) value_time = get_time(data) value_day = get_day(data) next_index = index + 1 if value_year add_to_structure(:year ,value_year, index, next_index, data_arr) end if value_full_date if @main_arr.size == 0 index = 0 end add_to_structure(@date_format[0], value_full_date[0], index, next_index, data_arr) add_to_structure(@date_format[1], value_full_date[1], index, next_index, data_arr) add_to_structure(@date_format[2], value_full_date[2], index, next_index, data_arr) end if value_month_year_date add_to_structure(:year , value_month_year_date[0], index, next_index, data_arr) add_to_structure(:month ,value_month_year_date[1], index, next_index, data_arr) end if value_dash add_to_structure(:year ,value_dash[0], index, next_index, data_arr, '-') add_to_structure(:year ,value_dash[1], index, next_index, data_arr) end if value_month add_to_structure(:month ,value_month, index, next_index, data_arr) end if value_short_month add_to_structure(:month ,value_short_month, index, next_index, data_arr) end if value_day add_to_structure(:day ,value_day, index, next_index, data_arr) end if value_time add_to_structure(:time ,value_time, index, next_index, data_arr) end end return @main_arr else nil end end |
#get_time(string) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/dates_from_string.rb', line 109 def get_time(string) if (result = string.match(/\d{2}:\d{2}:\d{2}/)) @clear_text.slice!(result.to_s) result.to_s elsif (result = string.match(/\d{2}:\d{2}/)) @clear_text.slice!(result.to_s) result.to_s else nil end end |
#get_year(string) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/dates_from_string.rb', line 121 def get_year(string) if string =~ /^\d{4}$/ string elsif string =~ /^\d{4}\.$/ string.delete!('.') elsif string =~ /^\d{4}\,$/ string.delete!(',') else nil end end |
#set_structura ⇒ Object
236 237 238 |
# File 'lib/dates_from_string.rb', line 236 def set_structura @structura = {type: nil, value: nil, distance: 0, key_words: []} end |