Class: Tjplurk::Robot

Inherits:
Object
  • Object
show all
Defined in:
lib/tjplurk/robot.rb,
lib/tjplurk/robot/topic.rb,
lib/tjplurk/robot/response.rb

Defined Under Namespace

Classes: Response, Topic

Instance Method Summary collapse

Constructor Details

#initialize(topics) ⇒ Robot

Returns a new instance of Robot.



3
4
5
# File 'lib/tjplurk/robot.rb', line 3

def initialize topics
  @topics = topics
end

Instance Method Details

#respond(input) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/tjplurk/robot.rb', line 7

def respond input
  if matched = @topics.each{|topic| topic.pattern = Regexp.new(topic.pattern) if topic.pattern.is_a? String}.select{|topic| topic.pattern.match(input) }.sample
    response = matched.responses.sample.content
    match_data = matched.pattern.match(input)
    response.gsub(/\\(\d+)/){|s| match_data[$1.to_i]}
  end
end