Sha256: c45db3d96c9083ca67e637e53f570fe9136b8a4e64adc02bec79a57e63b09724

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 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 NoMethodError, 'method :listen_messages must be defined in service'
    end

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

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

    private

    # @param payload (Payload)
    # @return (String): Json Format
    def encode_payload(payload)
      data = payload.to_h
      not_important_keys = %i[ordering_key topic_name forced_ordering_key]
      reduce_payload_size = !config.debug
      data[:headers].except!(*not_important_keys) if reduce_payload_size
      data.to_json
    end

    # @param (String: Payload in json format)
    def process_message(payload_info)
      payload = decode_payload(payload_info)
      return payload.process unless same_app_message?(payload)

      log("Skipping message from same origin: #{[payload]}") if config.debug
    rescue => e
      error_payload = [payload, e.message, e.backtrace]
      log("Error while starting to process a message: #{error_payload}", :error)
    end

    # @return Payload
    def decode_payload(payload_info)
      payload = ::PubSubModelSync::Payload.from_payload_data(JSON.parse(payload_info))
      log("Received message: #{[payload]}") if config.debug
      payload
    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

3 entries across 3 versions & 1 rubygems

Version Path
pub_sub_model_sync-1.1.0 lib/pub_sub_model_sync/service_base.rb
pub_sub_model_sync-1.0.1 lib/pub_sub_model_sync/service_base.rb
pub_sub_model_sync-1.0 lib/pub_sub_model_sync/service_base.rb