Sha256: e6631151e0a9588d9cf6291d1e35942108afcc89f79133bafb9cd070f5e57e08

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

module SalesforceStreamer
  # Models the PushTopic object for both Restforce and Streamer
  class PushTopic
    attr_accessor :id
    attr_reader :name, :description, :notify_for_fields, :query,
      :handler, :handler_constant, :api_version

    def initialize(data:)
      @handler           = data['handler']
      @static_replay     = data.dig('replay')&.to_i || -1
      @name              = data.dig('salesforce', 'name')
      @api_version       = data.dig('salesforce', 'api_version') || '41.0'
      @description       = data.dig('salesforce', 'description') || @name
      @notify_for_fields = data.dig('salesforce', 'notify_for_fields') || 'Referenced'
      @query             = strip_spaces(data.dig('salesforce', 'query'))
      validate!
    end

    def replay
      @replay ||= (ReplayPersistence.retrieve(name) || @static_replay).tap do |replay_id|
        Log.info "PushTopic name=#{name} starting at replayId=#{replay_id}"
      end
    end

    def to_s
      "PushTopic id=#{id} name=#{name} handler=#{handler} " \
        "replay=#{replay} notify_for_fields=#{notify_for_fields} " \
        "description=#{description} api_version=#{api_version} query=#{query}"
    end

    private

    def validate!
      fail(PushTopicNameTooLongError, @name) if @name.size > 25

      @handler_constant = Object.const_get(@handler)
      true
    rescue NameError, TypeError => e
      message = 'handler=' + @handler.to_s + ' exception=' + e.to_s
      raise(PushTopicHandlerMissingError, message)
    end

    def strip_spaces(str)
      fail(NilQueryError, @name) unless str

      str.gsub(/\s+/, ' ')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
salesforce_streamer-0.4.0 lib/salesforce_streamer/push_topic.rb