class Ruboty::Handlers::ToggleSwitch

Constants

NAMESPACE

Public Instance Methods

list(message) click to toggle source
# File lib/ruboty/handlers/toggle_switch.rb, line 53
def list(message)
  storage.each do |key, record|
    message.reply("- #{key} is #{record.state}.")
  end
end
show(message) click to toggle source
# File lib/ruboty/handlers/toggle_switch.rb, line 39
def show(message)
  switch = message[:switch]

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

    message.reply(text)
  end
end
toggle(message) click to toggle source
# File lib/ruboty/handlers/toggle_switch.rb, line 24
def toggle(message)
  switch = message[:switch]
  state  = message[:state]
  note   = message[:note]

  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, note: note }
    message.reply("#{switch} is now #{state}.")
  end
end

Private Instance Methods

storage() click to toggle source
# File lib/ruboty/handlers/toggle_switch.rb, line 61
def storage
  @storage ||= Ruboty::ToggleSwitch::Storage.new(robot.brain)
end