lib/kobot/engine.rb in kobot-1.2.5 vs lib/kobot/engine.rb in kobot-1.3.0
- old
+ new
@@ -79,11 +79,14 @@
}
}
}
options = Selenium::WebDriver::Chrome::Options.new(prefs: prefs)
options.headless! if Config.browser_headless
- @browser = Selenium::WebDriver.for(:chrome, options: options)
+ caps = [
+ options
+ ]
+ @browser = Selenium::WebDriver.for(:chrome, capabilities: caps)
@wait = Selenium::WebDriver::Wait.new(timeout: Config.browser_wait_timeout)
Kobot.logger.info('Launch browser successful')
end
def close_browser
@@ -149,87 +152,98 @@
time_table.find_elements(css: 'tbody > tr').each do |tr|
date_cell = tr.find_element(css: 'td.htBlock-scrollTable_day')
next unless date_cell.text.include? @today
Kobot.logger.info('Reading today record')
- @kot_today = date_cell.text
- @kot_today_css_class = date_cell.attribute('class')
- @kot_today_type = tr.find_element(css: 'td.work_day_type').text
- @kot_today_clock_in = tr.find_element(
+ @kot_data = {}
+ @kot_data[:today] = date_cell.text
+ @kot_data[:today_css_class] = date_cell.attribute('class')
+ @kot_data[:today_type] = tr.find_element(css: 'td.work_day_type').text
+ @kot_data[:today_schedule] = tr.find_element(css: 'td.schedule').text
+ @kot_data[:today_clock_in] = tr.find_element(
css: 'td.start_end_timerecord[data-ht-sort-index="START_TIMERECORD"]'
).text
- @kot_today_clock_out = tr.find_element(
+ @kot_data[:today_clock_out] = tr.find_element(
css: 'td.start_end_timerecord[data-ht-sort-index="END_TIMERECORD"]'
).text
Kobot.logger.debug do
{
- kot_toay: @kot_today,
- kot_today_css_class: @kot_today_css_class,
- kot_today_type: @kot_today_type,
- kot_today_clock_in: @kot_today_clock_in,
- kot_today_clock_out: @kot_today_clock_out
+ kot_toay: @kot_data[:today],
+ kot_today_css_class: @kot_data[:today_css_class],
+ kot_today_type: @kot_data[:today_type],
+ kot_today_clock_in: @kot_data[:today_clock_in],
+ kot_today_clock_out: @kot_data[:today_clock_out]
}
end
break
end
end
def validate_today_record!
- raise KotRecordError, "Today=#{@today} is not found on kot" if @kot_today.strip.empty?
+ raise KotRecordError, "Today=#{@today} is not found on kot" if @kot_data[:today].strip.empty?
if kot_weekend?
- raise KotRecordError, "Today=#{@today} is marked as weekend on kot: #{@kot_today}" unless Config.force
+ raise KotRecordError, "Today=#{@today} is marked as weekend: #{@kot_data[:today]}" unless Config.force
Kobot.logger.info(
- "[Force] should have exited: today=#{@today} is marked as weekend on kot: #{@kot_today}"
+ "[Force] should have exited: today=#{@today} is marked as weekend: #{@kot_data[:today]}"
)
end
- return unless kot_public_holiday?
- raise KotRecordError, "Today=#{@today} is marked as public holiday on kot: #{@kot_today}" unless Config.force
+ if kot_public_holiday?
+ raise KotRecordError, "Today=#{@today} is marked as public holiday: #{@kot_data[:today]}" unless Config.force
- Kobot.logger.info(
- "[Force] should have exited: today=#{@today} is marked as public holiday on kot: #{@kot_today}"
- )
+ Kobot.logger.info(
+ "[Force] should have exited: today=#{@today} is marked as public holiday: #{@kot_data[:today]}"
+ )
+ end
+
+ if kot_non_work_schedule?
+ raise KotRecordError, "Today=#{@today} is non-work schedule: #{@kot_data[:today_schedule]}" unless Config.force
+
+ Kobot.logger.info(
+ "[Force] should have exited: today=#{@today} is non-work schedule: #{@kot_data[:today_schedule]}"
+ )
+ end
end
def clock_in!
Kobot.logger.warn("Clock in during the afternoon: #{@now}") if @now.hour > 12
- if @kot_today_clock_in.strip.empty?
+ if @kot_data[:today_clock_in].strip.empty?
click_clock_in_button
return if Config.dryrun
read_today_record
- raise KotClockInError, 'Clock in operation seems to have failed' if @kot_today_clock_in.strip.empty?
+ raise KotClockInError, 'Clock in operation seems to have failed' if @kot_data[:today_clock_in].strip.empty?
- Kobot.logger.info("Clock in successful: #{@kot_today_clock_in}")
+ Kobot.logger.info("Clock in successful: #{@kot_data[:today_clock_in]}")
Mailer.send(clock_notify_message(clock: :in))
else
- Kobot.logger.warn("Clock in done already: #{@kot_today_clock_in}")
+ Kobot.logger.warn("Clock in done already: #{@kot_data[:today_clock_in]}")
end
end
def clock_out!
Kobot.logger.warn("Clock out during the morning: #{@now}") if @now.hour <= 12
unless Config.dryrun
- if @kot_today_clock_in.strip.empty?
+ if @kot_data[:today_clock_in].strip.empty?
raise KotClockOutError,
- "!!!No clock in record for today=#{@kot_today}!!!"
+ "!!!No clock in record for today=#{@kot_data[:today]}!!!"
end
end
- if @kot_today_clock_out.strip.empty?
+ if @kot_data[:today_clock_out].strip.empty?
click_clock_out_button
return if Config.dryrun
read_today_record
- raise KotClockOutError, 'Clock out operation seems to have failed' if @kot_today_clock_out.strip.empty?
+ raise KotClockOutError, 'Clock out operation seems to have failed' if @kot_data[:today_clock_out].strip.empty?
- Kobot.logger.info("Clock out successful: #{@kot_today_clock_out}")
+ Kobot.logger.info("Clock out successful: #{@kot_data[:today_clock_out]}")
Mailer.send(clock_notify_message(clock: :out))
else
- Kobot.logger.warn("Clock out done already: #{@kot_today_clock_out}")
+ Kobot.logger.warn("Clock out done already: #{@kot_data[:today_clock_out]}")
end
end
def click_clock_in_button
Kobot.logger.info("Navigate to: #{@top_url}")
@@ -268,22 +282,26 @@
def kot_weekend?
[
@selector.kot_date_saturday,
@selector.kot_date_sunday
- ].any? { |weekend| @kot_today&.include? weekend }
+ ].any? { |weekend| @kot_data[:today]&.include? weekend }
end
+ def kot_non_work_schedule?
+ !@kot_data[:today_schedule]&.include?(Config.auto_skip_without)
+ end
+
def kot_public_holiday?
- return true if @kot_today_type&.include? @selector.kot_workday_time_off_text
+ return true if @kot_data[:today_type]&.include? @selector.kot_workday_time_off_text
kot_today_highlighted = %w[sunday saturday].any? do |css|
- @kot_today_css_class&.include? css
+ @kot_data[:today_css_class]&.include? css
end
if kot_today_highlighted
Kobot.logger.warn(
- "Today=#{@kot_today} is highlighted (holiday) but not marked as #{@selector.kot_workday_time_off_text}"
+ "Today=#{@kot_data[:today]} is highlighted (holiday) but not marked as #{@selector.kot_workday_time_off_text}"
)
end
kot_today_highlighted
end
@@ -291,11 +309,11 @@
color = status == :success ? 'green' : 'red'
message = [
"<b>Date:</b> #{@today}",
"<b>Status:</b> <span style='color:#{color}'>#{status}</span>"
]
- message << "<b>Clock_in:</b> #{@kot_today_clock_in}" if clock
- message << "<b>Clock_out:</b> #{@kot_today_clock_out}" if clock == :out
+ message << "<b>Clock_in:</b> #{@kot_data[:today_clock_in]}" if clock
+ message << "<b>Clock_out:</b> #{@kot_data[:today_clock_out]}" if clock == :out
message.join('<br>')
end
end
end