Class: DatesFromString
- Inherits:
-
Object
- Object
- DatesFromString
- Defined in:
- lib/dates_from_string.rb,
lib/dates_from_string/version.rb
Constant Summary collapse
- PATTERNS =
{ [ /\d{4}-\d{2}-\d{2}/, /\d{4}-\d{1}-\d{2}/, /\d{4}-\d{1}-\d{1}/, /\d{4}-\d{2}-\d{1}/, ] => -> string { string.to_s.split("-") }, [ /\d{2}-\d{2}-\d{4}/, /\d{2}-\d{1}-\d{4}/, /\d{1}-\d{1}-\d{4}/, /\d{1}-\d{2}-\d{4}/, ] => -> string { string.to_s.split("-").reverse }, [ /\d{4}\.\d{2}\.\d{2}/, /\d{4}\.\d{2}\.\d{1}/, ] => -> string { string.to_s.split(".") }, [ /\d{2}\.\d{2}\.\d{4}/, /\d{1}\.\d{2}\.\d{4}/, ] => -> string { string.to_s.split(".").reverse }, [ /\d{4}\/\d{2}\/\d{2}/, /\d{4}\/\d{2}\/\d{1}/, ] => -> string { string.to_s.split("/") }, [ /\d{2}\/\d{2}\/\d{4}/, /\d{1}\/\d{2}\/\d{4}/, ] => -> string { string.to_s.split("/").reverse }, }
- DATE_COUNTRY_FORMAT =
{ default: -> {[:year,:month,:day]}, usa: -> {[:year,:day,:month]} }
- VERSION =
"1.2.2"
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
- #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) ⇒ DatesFromString
constructor
A new instance of DatesFromString.
- #set_structura ⇒ Object
Constructor Details
#initialize(key_words = [], date_format: :default) ⇒ DatesFromString
Returns a new instance of DatesFromString.
43 44 45 46 |
# File 'lib/dates_from_string.rb', line 43 def initialize(key_words = [], date_format: :default) @key_words = key_words @date_format = date_format_by_country(date_format) end |
Instance Method Details
#add_to_structure(type, value, index, next_index, data_arr, key_word = nil) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/dates_from_string.rb', line 221 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
247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/dates_from_string.rb', line 247 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
61 62 63 |
# File 'lib/dates_from_string.rb', line 61 def date_format_by_country(date_format) DATE_COUNTRY_FORMAT[date_format.to_sym].call end |
#email_date(email) ⇒ Object
57 58 59 |
# File 'lib/dates_from_string.rb', line 57 def email_date(email) email.match(/(?:(?:19|20)[0-9]{2})/).to_s end |
#find_date(string) ⇒ Object
48 49 50 51 |
# File 'lib/dates_from_string.rb', line 48 def find_date(string) parsing_structure = ParsingStructure.new(get_structure(string)) parsing_structure.start end |
#get_clear_text ⇒ Object
53 54 55 |
# File 'lib/dates_from_string.rb', line 53 def get_clear_text @clear_text.strip end |
#get_dash_data(string) ⇒ Object
188 189 190 191 192 193 194 195 196 |
# File 'lib/dates_from_string.rb', line 188 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
179 180 181 182 183 184 185 |
# File 'lib/dates_from_string.rb', line 179 def get_day(string) if string =~ (/^\d{2}$/) string else nil end end |
#get_full_date(string) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/dates_from_string.rb', line 156 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
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/dates_from_string.rb', line 198 def get_month_by_list(string) month = ['January','February','March','April','May','June','July','August','September','October','November','December'] index = month.index(string) if index sprintf('%02d',(index+1)) else nil end end |
#get_month_year_date(string) ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'lib/dates_from_string.rb', line 169 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
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/dates_from_string.rb', line 210 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) if short_index sprintf('%02d',(short_index+1)) else nil end end |
#get_structure(string) ⇒ Object
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/dates_from_string.rb', line 65 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
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/dates_from_string.rb', line 132 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
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/dates_from_string.rb', line 144 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
259 260 261 |
# File 'lib/dates_from_string.rb', line 259 def set_structura @structura = {type: nil, value: nil, distance: 0, key_words: []} end |