Sha256: cdd581cc6776640f2aa9c207bace7a3b9c27a469065ec25c34637bbc75dff315

Contents?: true

Size: 1.08 KB

Versions: 17

Compression:

Stored size: 1.08 KB

Contents

require 'bobot/events/common'
require 'bobot/events/message'
require 'bobot/events/message_echo'
require 'bobot/events/delivery'
require 'bobot/events/postback'
require 'bobot/events/optin'
require 'bobot/events/read'
require 'bobot/events/account_linking'
require 'bobot/events/referral'
require 'bobot/events/policy_enforcement'

module Bobot
  module Event
    EVENTS = {
      'message' => Message,
      'delivery' => Delivery,
      'postback' => Postback,
      'optin' => Optin,
      'read' => Read,
      'account_linking' => AccountLinking,
      'referral' => Referral,
      'message_echo' => MessageEcho,
      'policy-enforcement' => PolicyEnforcement,
    }.freeze

    def self.parse(payload)
      return MessageEcho.new(payload) if payload_is_echo?(payload)

      EVENTS.each do |event, klass|
        return klass.new(payload) if payload.key?(event)
      end

      raise UnknownPayload.new(payload)
    end

    def self.payload_is_echo?(payload)
      payload.key?('message') && payload['message']['is_echo'] == true
    end

    class UnknownPayload < Bobot::Error; end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
bobot-4.11.0 lib/bobot/event.rb
bobot-4.10.0 lib/bobot/event.rb
bobot-4.9.0 lib/bobot/event.rb
bobot-4.8.0 lib/bobot/event.rb
bobot-4.7.0 lib/bobot/event.rb
bobot-4.6.0 lib/bobot/event.rb
bobot-4.5.0 lib/bobot/event.rb
bobot-4.4.1 lib/bobot/event.rb
bobot-4.4.0 lib/bobot/event.rb
bobot-4.3.1 lib/bobot/event.rb
bobot-4.3.0 lib/bobot/event.rb
bobot-4.2.0 lib/bobot/event.rb
bobot-4.1.0 lib/bobot/event.rb
bobot-4.0.0 lib/bobot/event.rb
bobot-3.7.9 lib/bobot/event.rb
bobot-3.7.8 lib/bobot/event.rb
bobot-3.7.6 lib/bobot/event.rb