Class: Fb::Messenger::Receiver

Inherits:
Object
  • Object
show all
Defined in:
lib/fb/messenger/receiver.rb

Overview

handles the incoming fb webhook data

Class Method Summary collapse

Class Method Details

.configObject



30
31
32
# File 'lib/fb/messenger/receiver.rb', line 30

def config
  @_config ||= {}
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



22
23
24
# File 'lib/fb/messenger/receiver.rb', line 22

def configure
  yield self
end

.event_type(msg_event) ⇒ Object



34
35
36
37
38
# File 'lib/fb/messenger/receiver.rb', line 34

def event_type(msg_event)
  return 'message' if msg_event[:message]
  return 'postback' if msg_event[:postback]
  return 'delivery' if msg_event[:delivery]
end

.handle_unsupported_event(msg_event) ⇒ Object

TODO: log this error better



41
42
43
# File 'lib/fb/messenger/receiver.rb', line 41

def handle_unsupported_event(msg_event)
  puts ">>> unsupported event: #{msg_event}"
end

.receive(entries, opts = {}) ⇒ Object

expecting params



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fb/messenger/receiver.rb', line 7

def receive(entries, opts = {})
  entries.each do |entry|
    entry[:messaging].each do |msg_event|
      event_type = event_type(msg_event)
      unless event_type
        handle_unsupported_event(msg_event)
        next
      end
      event_class = event_type.classify
      event = "Fb::Messenger::Event::#{event_class}".constantize.new(msg_event)
      config[event_type].call(event, opts)
    end
  end
end

.subscribe(event, subscriber) ⇒ Object



26
27
28
# File 'lib/fb/messenger/receiver.rb', line 26

def subscribe(event, subscriber)
  config[event] = subscriber
end