Sha256: 71d800ed772ba77bf5a69caa587aa37c9889177da5093b091b0350516135bac0

Contents?: true

Size: 1.53 KB

Versions: 5

Compression:

Stored size: 1.53 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),
          t: timestamp && timestamp.to_i
        )
      end

      def topic
        @model.class.name.tableize
      end

      def url
        raise NotImplementedError
      end

      def async?
        false
      end

      def data
        nil
      end

      def timestamp
        return @model.created_at if created? && @model.respond_to?(:created_at)
        return @model.updated_at if (updated? || created?) && @model.respond_to?(:updated_at)
        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

5 entries across 5 versions & 1 rubygems

Version Path
roo_on_rails-1.13.1 lib/roo_on_rails/routemaster/publisher.rb
roo_on_rails-1.13.0 lib/roo_on_rails/routemaster/publisher.rb
roo_on_rails-1.12.0 lib/roo_on_rails/routemaster/publisher.rb
roo_on_rails-1.11.1 lib/roo_on_rails/routemaster/publisher.rb
roo_on_rails-1.11.0 lib/roo_on_rails/routemaster/publisher.rb