Sha256: 3db8ee897ba6e57cb53f52ea339fd3856cfce0623638b95ad2ac0082877a3095

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module Propono
  class PublisherError < Exception
  end

  class Publisher
    include Sns

    def self.publish(topic_id, message, options = {})
      new(topic_id, message, options).publish
    end

    attr_reader :topic_id, :message, :protocol

    def initialize(topic_id, message, options = {})
      raise PublisherError.new("Topic is nil") if topic_id.nil?
      raise PublisherError.new("Message is nil") if message.nil?

      @topic_id = topic_id
      @message = message
      @protocol = options.fetch(:protocol, :sns).to_sym
    end

    def publish
      send("publish_via_#{protocol}")
    end

    private

    def publish_via_sns
      topic = TopicCreator.find_or_create(topic_id)
      sns.publish(topic.arn, message)
    end

    def publish_via_udp
      payload = {topic: topic_id, message: message}.to_json
      UDPSocket.new.send(payload, 0, config.udp_host, config.udp_port)
    rescue SocketError => e
      config.logger.puts "Udp2sqs failed to send : #{e}"
    end

    def config
      Configuration.instance
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
propono-0.5.6 lib/propono/services/publisher.rb
propono-0.5.5 lib/propono/services/publisher.rb