Class: Kobot::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/kobot/engine.rb

Overview

The core class that launches browser, logins to KOT, reads today record, and conducts clock in or clock out action based on config.

Instance Method Summary collapse

Constructor Details

#initializeEngine

Returns a new instance of Engine.



9
10
11
12
13
# File 'lib/kobot/engine.rb', line 9

def initialize
  @now = Time.now.getlocal(Config.kot_timezone_offset)
  @today = @now.strftime(Config.kot_date_format)
  @top_url = Config.kot_url
end

Instance Method Details

#startObject

The entrance where the whole flow starts.

It exits early if today is weekend or marked as to skip by the #Config.skip specified from command line option --skip.

Unexpected behavior such as record appearing as holiday on the web or failure of clock in/out action is handled within 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.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kobot/engine.rb', line 26

def start
  return unless should_run_today?

  launch_browser
  
  read_today_record
  validate_today_record!
  if Config.clock == :in
    clock_in!
  else
    clock_out!
  end
  logout
rescue KotRecordError => e
  Kobot.logger.warn(e.message)
  Mailer.send(clock_notify_message(status: e.message))
  logout
rescue KotClockInError => e
  Kobot.logger.warn e.message
  Mailer.send(clock_notify_message(clock: :in, status: e.message))
  logout
rescue KotClockOutError => e
  Kobot.logger.warn e.message
  Mailer.send(clock_notify_message(clock: :out, status: e.message))
  logout
rescue StandardError => e
  Kobot.logger.error(e.message)
  Kobot.logger.error(e.backtrace)
  Mailer.send(clock_notify_message(status: e.message))
  logout
ensure
  close_browser
end