Class: TickingAway::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/ticking_away/bot.rb

Constant Summary collapse

TIMEAT_CMD =
'!timeat '.freeze
TIMEPOPULARITY_CMD =
'!timepopularity '.freeze
DEFAULT_TIME_API =
'http://worldtimeapi.org/api'.freeze
EXCUSES =
[
  'Time is an illusion',
  'What is time, really?',
  'The linear progression of time is currently on hold'
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage: nil, time_api: nil) ⇒ Bot

Allow the caller to pass in the storage method via primitive DI. Other storage methods must implement increment_stat(<tz_info>) and get_stat(<tz_info or prefix>)



17
18
19
20
# File 'lib/ticking_away/bot.rb', line 17

def initialize(storage: nil, time_api: nil)
  @storage = storage || TickingAway::JSONFileStorage.new
  @time_api = ENV['TIME_API'] || time_api || DEFAULT_TIME_API
end

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



12
13
14
# File 'lib/ticking_away/bot.rb', line 12

def storage
  @storage
end

#time_apiObject (readonly)

Returns the value of attribute time_api.



12
13
14
# File 'lib/ticking_away/bot.rb', line 12

def time_api
  @time_api
end

Instance Method Details

#chat(msg) ⇒ Object

Send a string to the Bot in the format of <user_name>: <message> or just <message> Only !timeat <tz_info> and !timepopularity <tz_info or prefix> commands will return a string response



26
27
28
29
30
31
32
33
34
35
# File 'lib/ticking_away/bot.rb', line 26

def chat(msg)
  message = strip_username(msg)

  case message.partition(' ')[0]
  when TIMEAT_CMD.strip
    time_check(message)
  when TIMEPOPULARITY_CMD.strip
    stat_check(message)
  end
end

#stat_check(msg) ⇒ Object

Return the statistic for the provided tz_info or prefix



48
49
50
# File 'lib/ticking_away/bot.rb', line 48

def stat_check(msg)
  storage.get_stat(parse_message(msg, TIMEPOPULARITY_CMD.length))
end

#time_check(msg) ⇒ Object

Check time for the timezone provided against the provided time api.



39
40
41
42
43
44
45
# File 'lib/ticking_away/bot.rb', line 39

def time_check(msg)
  tz_info = parse_message(msg, TIMEAT_CMD.length)

  puts "Event: Checking Time for timezone: #{tz_info}"

  time_message(tz_info)
end