I need to shorten/simplify this code:
require "time"
def parse(timings)
validdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
hours = {}
if timings.include?("Open everyday from")
validdays.each do |day|
open_close = timings.gsub(/.*everyday\s*from\s*(.*).*/,'\1')
save_day_array(open_close,day,hours)
end
else
if !timings.start_with?('Mon')
timings.gsub!(/.*(Mon.*)/,'\1')
end
timings.slice! "|"
ap timings
normalizie_days = {
"Sun" => "Sunday",
"Mon" => "Monday",
"Tue" => "Tuesday",
"Wed" => "Wednesday",
"Thu" => "Thursday",
"Fri" => "Friday",
"Sat" => "Saturday"}
#the following Step scans for the Short Days(Mon Tue .. ) and normalizie it (Monday, Tuesday ...)
if timings.scan(/\b[a-zA-Z]{3}\b/).length > 0
timings.scan(/\b[a-zA-Z]{3}\b/).each do |d|
timings.gsub!(/#{d}/,"#{normalizie_days[d]}")
end
end
# Adds all the Matching days to match days array
match_days = []
validdays.each do |day|
if timings.match(day)
match_days << day
end
end
if match_days.length == 2
#eg : Monday - Sunday : 10:00am-04:00pm
if timings.match(/^[a-zA-Z]+\s?-\s?[a-zA-Z]+:.*/)
validdays.each do |day|
open_close = timings.gsub(/.*Sunday:(.*)/,'\1')
save_day_array(open_close,day,hours)
end
end
elsif match_days.length == 3
validdays.each do |day|
if timings.match(/^[a-zA-Z]+\s+?-\s+?[a-zA-Z]+:.*\s+?[a-zA-Z]+:?.*/)
open_close = []
#eg: Monday - Saturday: 10:00am-7:00pm Sunday: 10:00am - 5:00pm
if match_days.include?("Sunday")
if day == "Sunday" && timings.match(day)
open_close = timings.gsub(/.*Sunday:?(.*)/,'\1')
else
open_close = timings.gsub(/^[a-zA-Z]+\s+?-\s+?[a-zA-Z]+:?\s+?(.*)\s+Sunday.*/,'\1')
end
else
hours["sunday"] = [{open: "closed",close: "closed"}]
if day == "Saturday" && timings.match(day)
open_close = timings.gsub(/.*Saturday:?\s?\-?\s?(.*)/,'\1')
else
open_close = timings.gsub(/^[a-zA-Z]+\s?-\s+?[a-zA-Z]+:?\s?(.*)\s?Saturday.*/,'\1')
end
end
save_day_array(open_close,day,hours)
end
end
elsif match_days.length == 4
#eg: Sunday - Tuesday: 9:00am-5:00pm Wednesday-Saturday: 9:00am - 7:00pm
validdays.each do |day|
if timings.match(/^[a-zA-Z]+\s?\-?\s?[a-zA-Z]+:?.*\s+?[a-zA-Z]+:?.*/)
open_close = []
if (day == "Sunday") && timings.match(day)
open_close = timings.gsub(/.*Sunday:?(.*)/,'\1')
elsif (day == "Saturday") && timings.match(/.*Saturday\s?\&?\s?Sunday.*|.*Saturday\s?\-?\s?Sunday.*/)
open_close = timings.gsub(/.*Sunday:?(.*)/,'\1')
elsif (day == "Saturday") && timings.match(/.*Saturday.*Sunday.*/)
open_close = timings.gsub(/.*Saturday(.*)Sunday.*/,'\1')
elsif (day == "Saturday") && timings.match(day)
open_close = timings.gsub(/.*Sunday:?(.*)/,'\1')
else
open_close = timings.gsub(/^[a-zA-Z]+\s?-\s?[a-zA-Z]+:?\s+?(.*)\s+Sunday.*/,'\1')
end
save_day_array(open_close,day,hours)
elsif timings.match(/^[a-zA-Z]+\s?-\s?[a-zA-Z]+:?.*\s+?[a-zA-Z]+:?.*/)
end
end
elsif match_days.length == 7
#eg: Monday: 10:00am-5:00pm Tuesday: 10:00am-5:00pm Wednesday: 10:00am-5:00pm Thrusday: 10:00am-5:00pm Friday: 10:00am-9:00pm Saturday: 10:00am-9:00pm Sunday: Closed
match_days.each_with_index do |day, index|
regex = /.*#{day}(.*)#{match_days[index+1]}.*/
open_close = timings.gsub(regex,'\1')
save_day_array(open_close,day,hours)
end
else
ap timings
end
end
return hours
end
save_day_array to save the day time to hours array created to avoid code repetition
def save_day_array(open_close,day,hours)
if !open_close.nil?
open_close = open_close.split("-")
end
parsed_open = Time.parse("#{open_close[0]}").strftime("%H:%M")
parsed_closed = Time.parse("#{open_close[1]}").strftime("%H:%M")
if !parsed_open.nil? && !parsed_closed.nil?
hours[day.downcase] = {opens_at: parsed_open,closes_at: parsed_closed}
else
hours[day.downcase] = {opens_at: "closed",closes_at: "closed" }
end
end
The following strings need to be passed to get the below output:
"Monday - Friday 10:00am-7:00pm Saturday: 10:00am-4:00pm Sunday: 11:00am-4:00pm"
"Monday - Friday 10 am - 6 pm Saturday 10 am - 5 pm Sunday 11 am - 5 pm"
"Mon - Sun: 9:00am-7:00pm"
Output :
{
"sunday": {"opens_at": 11:00, "closes_at": 16:00},
"monday": {"opens_at": 10:00, "closes_at": 16:00},
"tuesday": {"opens_at": 10:00, "closes_at": 16:00},
"wednesday": {"opens_at": 10:00, "closes_at": 16:00},
"thrusday": {"opens_at": 10:00, "closes_at": 16:00},
"friday": {"opens_at": 10:00, "closes_at": 16:00},
"saturday": {"opens_at": 10:00, "closes_at": 16:00}
}
I get the output, but I need to clean up and simplify the code.