2013-09-03 11:15:51 +02:00
|
|
|
module SMSd
|
|
|
|
class AnsweringMachine
|
2013-11-07 11:03:25 +01:00
|
|
|
attr_accessor :actions, :default_action, :specific_actions
|
2013-09-03 11:15:51 +02:00
|
|
|
|
2013-11-07 11:03:25 +01:00
|
|
|
def initialize(default_answer = nil, specific_actions = {}, &block)
|
2013-09-03 11:15:51 +02:00
|
|
|
self.actions = []
|
|
|
|
self.default_action = Action.new(nil, default_answer, &block)
|
2013-11-07 11:03:25 +01:00
|
|
|
|
|
|
|
self.specific_actions = {}
|
|
|
|
specific_actions.each do |key, value|
|
|
|
|
self.specific_actions[key] = Action.new(nil, value)
|
|
|
|
end
|
2013-09-03 11:15:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_action(regexp, answer = nil, &block)
|
2013-09-11 11:12:22 +02:00
|
|
|
actions << Action.new(regexp, answer, &block)
|
2013-09-03 11:15:51 +02:00
|
|
|
end
|
|
|
|
|
2013-11-07 11:03:25 +01:00
|
|
|
def execute_action(action, from, to, message)
|
|
|
|
specific_actions[action].answer(from, to, message) if specific_actions.key?(action)
|
|
|
|
end
|
|
|
|
|
2013-09-03 11:15:51 +02:00
|
|
|
def execute(from, to, message)
|
|
|
|
actions.each do |action|
|
|
|
|
return action.answer(from, to, message) if message =~ action.regexp
|
|
|
|
end
|
|
|
|
|
|
|
|
default_action.answer(from, to, message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|