Skip to content

Instantly share code, notes, and snippets.

@ltk
Created April 6, 2015 18:12
Show Gist options
  • Save ltk/8bfa829729fb2ddca155 to your computer and use it in GitHub Desktop.
Save ltk/8bfa829729fb2ddca155 to your computer and use it in GitHub Desktop.
Graba Slack Responder
class Slack::Responder
def initialize(message)
@message = message
end
def respond?
response.present?
end
def response
@response ||= if room.present?
Slack::RoomMessage.new(room, availability: only_available?, people: people?, book: book?)
elsif office.present?
Slack::OfficeMessage.new(office, availability: only_available?, book: book?)
end
end
private
def office
@office ||= Office.all.detect do |office|
office if office.identifiers.any? do |identifier|
message.match(/#{identifier}/i)
end
end
end
def room
@room ||= Room.all.detect do |room|
room if room.identifiers.any? do |identifier|
message.match(/#{identifier}/i)
end
end
end
def only_available?
message.match(/(free|available|open)/i)
end
def people?
message.match(/who/i)
end
def book?
message.match(/(book|reserve|grab)/i)
end
attr_reader :message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment