Class: Ruboty::Handlers::ToggleSwitch

Inherits:
Base
  • Object
show all
Defined in:
lib/ruboty/handlers/toggle_switch.rb

Constant Summary collapse

NAMESPACE =
'toggle_switch'

Instance Method Summary collapse

Instance Method Details

#show(message) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/ruboty/handlers/toggle_switch.rb', line 23

def show(message)
  switch = message[:switch]

  if (record = storage[switch])
    message.reply("#{switch} is #{record.state} by #{record.from} #{record.at.strftime('on %b %d at %H:%M')}.")
  end
end

#toggle(message) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruboty/handlers/toggle_switch.rb', line 9

def toggle(message)
  switch = message[:switch]
  state = message[:state]

  storage_state = storage[switch] && storage[switch].state

  if storage_state == state
    message.reply("#{switch} is already #{state}.")
  else
    storage[switch] = {state: state, from: message.from_name}
    message.reply("#{switch} is now #{state}.")
  end
end