Sha256: 39a149c17e69d15c53b4dff81590840bbb097544d7cbe5ed4786c1177e5917a8

Contents?: true

Size: 1.73 KB

Versions: 15

Compression:

Stored size: 1.73 KB

Contents

# coding: utf-8
# frozen_string_literal: true

module Stealth
  module Services
    module Facebook

      class MessageEvent

        attr_reader :service_message, :params

        def initialize(service_message:, params:)
          @service_message = service_message
          @params = params
        end

        def process
          fetch_message
          fetch_location
          fetch_attachments
        end

        private

          def fetch_message
            if params['message']['quick_reply'].present?
              service_message.message = params['message']['quick_reply']['payload']
            elsif params['message']['text'].present?
              service_message.message = params['message']['text']
            end
          end

          def fetch_location
            if params['message']['attachments'].present? && params['message']['attachments'].is_a?(Array)
              params['message']['attachments'].each do |attachment|
                next unless attachment['type'] == 'location'

                lat = attachment['payload']['coordinates']['lat']
                lng = attachment['payload']['coordinates']['long']

                service_message.location = {
                  lat: lat,
                  lng: lng
                }
              end
            end
          end

          def fetch_attachments
            if params['message']['attachments'].present? && params['message']['attachments'].is_a?(Array)
              params['message']['attachments'].each do |attachment|
                service_message.attachments << {
                  type: attachment['type'],
                  url: attachment['payload']['url']
                }
              end
            end
          end

      end

    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
stealth-facebook-0.14.0 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.13.2 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.13.1 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.13.0 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.12.0 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.11.4 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.11.3 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.11.2 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.11.1 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.11.0 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.10.3 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.10.2 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.10.1 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.10.0 lib/stealth/services/facebook/events/message_event.rb
stealth-facebook-0.9.2 lib/stealth/services/facebook/events/message_event.rb