Class: AwesomeBotFactory::Skill

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Configurable
Defined in:
lib/awesome_bot_factory/skill.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(campfire_message = {}) ⇒ Skill

Returns a new instance of Skill.



16
17
18
19
# File 'lib/awesome_bot_factory/skill.rb', line 16

def initialize(campfire_message={})
  self.message = campfire_message
  self.message["match"] ||= [] # let's just make sure we have an hash there and don't break if it is in any case empty
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



34
35
36
37
38
39
# File 'lib/awesome_bot_factory/skill.rb', line 34

def method_missing(name, *args)
  return self.message[name.to_s] if self.message.key?(name.to_s)
  return self.message[name.to_sym] if self.message.key?(name.to_sym)
  return self.read_attribute(name) if self.attribute_defined?(name)
  super(name,*args)
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



5
6
7
# File 'lib/awesome_bot_factory/skill.rb', line 5

def message
  @message
end

Class Method Details

.call(env) ⇒ Object



11
12
13
14
# File 'lib/awesome_bot_factory/skill.rb', line 11

def self.call(env)
  message = JSON.parse(env["rack.input"].read)
  [200, {"Content-Type" => "application/json"}, [ self.new(message).reply.to_json] ]
end

.matches(*matches) ⇒ Object



7
8
9
# File 'lib/awesome_bot_factory/skill.rb', line 7

def self.matches(*matches)
  @@matches = matches
end

Instance Method Details

#attribute_defined?(attr) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/awesome_bot_factory/skill.rb', line 30

def attribute_defined?(attr)
  !@@matches.index(attr).nil?
end

#read_attribute(attr) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/awesome_bot_factory/skill.rb', line 21

def read_attribute(attr)
  index = @@matches.index(attr.to_sym)
  if index
    self.message["match"][index+1]
  else
    self.message[attr.to_s] || self.message[attr.to_sym]
  end
end