Class: Walle::Robot::Definitions

Inherits:
Object
  • Object
show all
Includes:
Middlewares::Helper
Defined in:
lib/walle/robot/definitions.rb

Defined Under Namespace

Classes: Stack

Constant Summary collapse

EVENTS =
i{start hello close closed message}
METHODS =
EVENTS + i{use routes pattern}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Middlewares::Helper

#use

Constructor Details

#initializeDefinitions



33
34
35
# File 'lib/walle/robot/definitions.rb', line 33

def initialize
  @all_handlers = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#all_handlersObject (readonly)

Returns the value of attribute all_handlers.



31
32
33
# File 'lib/walle/robot/definitions.rb', line 31

def all_handlers
  @all_handlers
end

Instance Method Details

#apply_to(client) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/walle/robot/definitions.rb', line 48

def apply_to(client)
  all_handlers.each do |event, event_handlers|
    event_handlers.each do |handler|
      client.on(event) do |data|
        handle_event(environment(event, client, data), handler)
      end
    end
  end
end

#handlers(event = nil) ⇒ Object



58
59
60
61
# File 'lib/walle/robot/definitions.rb', line 58

def handlers(event = nil)
  return all_handlers[event] if event
  all_handlers.values.flatten.uniq
end

#routes(&block) ⇒ Object



43
44
45
46
# File 'lib/walle/robot/definitions.rb', line 43

def routes(&block)
  router = Router.new(&block)
  message { |env| router.call(env) }
end