Skip to content

Commit

Permalink
add new method get_clear_text and refact
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-chechaev committed Sep 8, 2015
1 parent 5cabbff commit 886ab45
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 28 deletions.
76 changes: 48 additions & 28 deletions lib/dates_from_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@

class DatesFromString


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}/,
] => -> string { string.to_s.split(".") },
[
/\d{2}\.\d{2}\.\d{4}/,
] => -> string { string.to_s.split(".").reverse },
[
/\d{4}\/\d{2}\/\d{2}/,
] => -> string { string.to_s.split("/") },
[
/\d{2}\/\d{2}\/\d{4}/,
] => -> string { string.to_s.split("/").reverse },
}

def initialize(key_words = [])
@key_words = key_words
end
Expand All @@ -12,12 +40,17 @@ def find_date(string)
parsing_structure.start
end

def get_clear_text
@clear_text.strip
end

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)
Expand Down Expand Up @@ -80,6 +113,10 @@ def get_structure(string)

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
Expand All @@ -99,35 +136,18 @@ def get_year(string)
end

def get_full_date(string)
if (result = string.match(/\d{4}-\d{2}-\d{2}/))
result.to_s.split("-")
elsif (result = string.match(/\d{2}-\d{2}-\d{4}/))
result.to_s.split("-").reverse
elsif (result = string.match(/\d{4}-\d{1}-\d{2}/))
result.to_s.split("-")
elsif (result = string.match(/\d{2}-\d{1}-\d{4}/))
result.to_s.split("-").reverse
elsif (result = string.match(/\d{4}-\d{1}-\d{1}/))
result.to_s.split("-")
elsif (result = string.match(/\d{1}-\d{1}-\d{4}/))
result.to_s.split("-").reverse
elsif (result = string.match(/\d{4}-\d{2}-\d{1}/))
result.to_s.split("-")
elsif (result = string.match(/\d{1}-\d{2}-\d{4}/))
result.to_s.split("-").reverse
elsif (result = string.match(/\d{4}\.\d{2}\.\d{2}/))
result.to_s.split(".")
elsif (result = string.match(/\d{2}\.\d{2}\.\d{4}/))
result.to_s.split(".").reverse
elsif (result = string.match(/\d{4}\/\d{2}\/\d{2}/))
result.to_s.split("/")
elsif (result = string.match(/\d{2}\/\d{2}\/\d{4}/))
result.to_s.split("/").reverse
# elsif string =~(/\d{2}\s{1}(Jan|Feb|Mar|Apr|May|Jun|Jul|Apr|Sep|Oct|Nov|Dec)\s{1}\d{4}/)
# string.to_date.to_s.split("-")
else
nil

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

return nil

end

def get_month_year_date(string)
Expand Down
21 changes: 21 additions & 0 deletions spec/dates_from_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -552,5 +552,26 @@
expect(subject.find_date(input)).to eq(output)
end

it 'find dates in simple structure 8' do
input = "Создай задачу забрать машину из ремонта 2015-02-02 в 23:00"
output = ["2015-02-02 23:00"]

expect(subject.find_date(input)).to eq(output)
end

it 'find dates in simple structure 9' do
input = "Создай задачу забрать машину из ремонта 2015-02-02 23:00"
output = "Создай задачу забрать машину из ремонта"
subject.find_date(input)
expect(subject.get_clear_text).to eq(output)
end

it 'find dates in simple structure 10' do
input = "Создай задачу забрать машину из ремонта 2015-02-02 23:30:40"
output = "Создай задачу забрать машину из ремонта"
subject.find_date(input)
expect(subject.get_clear_text).to eq(output)
end

end
end

0 comments on commit 886ab45

Please sign in to comment.