Sha256: c6adde64d90a1430adb634c8b05873784294caebcd92a78b68efdee058438ee0

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'roo_on_rails/config'
require 'routemaster/client'

module RooOnRails
  module Routemaster
    class Publisher
      attr_reader :model, :event

      def initialize(model, event, client: ::Routemaster::Client)
        @model = model
        @event = event
        @client = client
      end

      def publish?
        noop? || @model.new_record? || @model.previous_changes.any?
      end

      def will_publish?
        Config.routemaster_publishing_enabled? && publish?
      end

      def publish!
        return unless will_publish?
        @client.send(@event, topic, url, async: async?, data: stringify_keys(data))
      end

      def topic
        @model.class.name.tableize
      end

      def url
        raise NotImplementedError
      end

      def async?
        false
      end

      def data
        nil
      end

      %i(created updated deleted noop).each do |event_type|
        define_method :"#{event_type}?" do
          @event.to_sym == event_type
        end
      end

      private

      def stringify_keys(hash)
        return hash if hash.nil? || hash.empty?

        hash.each_with_object({}) do |(k, v), h|
          h[k.to_s] = v.is_a?(Hash) ? stringify_keys(v) : v
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roo_on_rails-1.10.0 lib/roo_on_rails/routemaster/publisher.rb