Sha256: 6302c898a6cb654232d354fe52908f3f33463accbb2e4130805a5d65cb94ae26

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

module RisingDragon
  module SNS
    class Publisher
      attr_accessor :use_cache
      def initialize(sns_client)
        @sns_client = sns_client
        @topic_cache = {}
        @use_cache = true # default true :)
      end

      def publish(topic_name, event_type, data)
        event = create_event(event_type, data)

        topic = get_topic(topic_name)
        @sns_client.publish(topic_arn: topic.topic_arn, message: event.to_json)
      end

      private

        def create_event(type, data)
          ::RisingDragon::Event.new(id: uuid, timestamp: unixtime, type: type, data: data)
        end

        def uuid
          SecureRandom.uuid
        end

        def unixtime
          (Time.now.to_f * 1000).to_i
        end

        def get_topic(topic_name)
          return create_topic(topic_name) unless @use_cache

          @topic_cache[topic_name] ||= create_topic(topic_name)
        end

        def create_topic(topic_name)
          @sns_client.create_topic(name: topic_name)
        end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rising_dragon-0.5.0 lib/rising_dragon/sns/publisher.rb
rising_dragon-0.3.4 lib/rising_dragon/sns/publisher.rb
rising_dragon-0.3.3 lib/rising_dragon/sns/publisher.rb
rising_dragon-0.3.2 lib/rising_dragon/sns/publisher.rb