Sha256: c0be124636c6a8462e3096815e2563de66ffbbfd088c0d0d1597d02d546044d6

Contents?: true

Size: 1.29 KB

Versions: 6

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require 'pub_sub_model_sync/payload'
module PubSubModelSync
  class ServiceBase < PubSubModelSync::Base
    SERVICE_KEY = 'service_model_sync'

    def listen_messages
      raise 'method :listen_messages must be defined in service'
    end

    # @param _payload (Payload)
    def publish(_payload)
      raise 'method :publish must be defined in service'
    end

    def stop
      raise 'method :stop must be defined in service'
    end

    private

    # @param (String: Payload in json format)
    def process_message(payload_info)
      payload = parse_payload(payload_info)
      log("Received message: #{[payload]}") if config.debug
      if same_app_message?(payload)
        log("Skip message from same origin: #{[payload]}") if config.debug
      else
        payload.process!
      end
    rescue => e
      error = [payload, e.message, e.backtrace]
      log("Error parsing received message: #{error}", :error)
    end

    def parse_payload(payload_info)
      info = JSON.parse(payload_info).deep_symbolize_keys
      ::PubSubModelSync::Payload.new(info[:data], info[:attributes], info[:headers])
    end

    # @param payload (Payload)
    def same_app_message?(payload)
      key = payload.headers[:app_key]
      key && key == config.subscription_key
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pub_sub_model_sync-0.5.3 lib/pub_sub_model_sync/service_base.rb
pub_sub_model_sync-0.5.2 lib/pub_sub_model_sync/service_base.rb
pub_sub_model_sync-0.5.1.1 lib/pub_sub_model_sync/service_base.rb
pub_sub_model_sync-0.5.1 lib/pub_sub_model_sync/service_base.rb
pub_sub_model_sync-0.5.0.1 lib/pub_sub_model_sync/service_base.rb
pub_sub_model_sync-0.5.0 lib/pub_sub_model_sync/service_base.rb