lib/kobot/engine.rb in kobot-1.2.2 vs lib/kobot/engine.rb in kobot-1.2.3
- old
+ new
@@ -22,36 +22,23 @@
# the method by logging and/or email notifications if enabled.
#
# System errors or any unknown exceptions occurred if any are
# to be popped up and should be handled by the outside caller.
def start
- if weekend?
- if Config.force
- Kobot.logger.info("[Force] should have exited: today=#{@today} is weekend")
- else
- Kobot.logger.info("Today=#{@today} is weekend")
- return
- end
- end
- if skip?
- Kobot.logger.info("Today=#{@today} is skipped as per: --skip=#{Config.skip}")
- return
- end
- unless %i[in out].include? Config.clock
- Kobot.logger.warn("Invalid clock operation: #{Config.clock}")
- return
- end
+ validate_today!
launch_browser
login
read_today_record
- verify_today_record!
+ validate_today_record!
if Config.clock == :in
clock_in!
else
clock_out!
end
logout
+ rescue KotSkip => e
+ Kobot.logger.warn(e.message)
rescue KotRecordError => e
Kobot.logger.warn(e.message)
Mailer.send(clock_notify_message(status: e.message))
logout
rescue KotClockInError => e
@@ -66,16 +53,24 @@
Kobot.logger.error(e.message)
Kobot.logger.error(e.backtrace)
Mailer.send(clock_notify_message(status: e.message))
logout
ensure
- Kobot.logger.info('Close browser')
- @browser&.quit
+ close_browser
end
private
+ def validate_today!
+ raise KotSkip, "Today=#{@today} is skipped as per: --skip=#{Config.skip}" if skip?
+
+ return unless weekend?
+ raise KotSkip, "Today=#{@today} is weekend" unless Config.force
+
+ Kobot.logger.info("[Force] should have exited: today=#{@today} is weekend")
+ end
+
def launch_browser
prefs = {
profile: {
default_content_settings: {
geolocation: Config.browser_geolocation ? 1 : 2
@@ -87,10 +82,17 @@
@browser = Selenium::WebDriver.for(:chrome, options: options)
@wait = Selenium::WebDriver::Wait.new(timeout: Config.browser_wait_timeout)
Kobot.logger.info('Launch browser successful')
end
+ def close_browser
+ return unless @browser
+
+ Kobot.logger.info('Close browser')
+ @browser.quit
+ end
+
def login
Kobot.logger.info("Navigate to: #{@top_url}")
@browser.get @top_url
@wait.until { @browser.find_element(id: 'modal_window') }
Kobot.logger.info "Page title: #{@browser.title}"
@@ -157,10 +159,10 @@
end
break
end
end
- def verify_today_record!
+ def validate_today_record!
raise KotRecordError, "Today=#{@today} is not found on kot" if @kot_today.strip.empty?
if kot_weekend?
raise KotRecordError, "Today=#{@today} is marked as weekend on kot: #{@kot_today}" unless Config.force