Sha256: 5ed1d51dc5836c3b297310180a5de64db250a67f8bdab92c117612a8292a8176

Contents?: true

Size: 1015 Bytes

Versions: 8

Compression:

Stored size: 1015 Bytes

Contents

module Propono
  class QueueSubscription

    include Sns
    include Sqs

    attr_reader :topic_arn, :queue

    def self.create(topic_id)
      new(topic_id).tap do |subscription|
        subscription.create
      end
    end

    def initialize(topic_id)
      @topic_id = topic_id
    end

    def create
      @topic = TopicCreator.find_or_create(@topic_id)
      @queue = QueueCreator.find_or_create(queue_name)
      sns.subscribe(@topic.arn, @queue.arn, 'sqs')
      sqs.set_queue_attributes(@queue.url, "Policy", generate_policy)
    end

    def queue_name
      @queue_name ||= "#{Propono.config.application_name.gsub(" ", "_")}-#{@topic_id}"
    end

    private

    def generate_policy
      <<-EOS
{
  "Version": "2008-10-17",
  "Id": "#{@queue.arn}/SQSDefaultPolicy",
  "Statement": [
    {
      "Sid": "#{@queue.arn}-Sid",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "SQS:*",
      "Resource": "#{@queue.arn}"
    }
  ]
}
      EOS
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
propono-0.9.1 lib/propono/components/queue_subscription.rb
propono-0.9.0 lib/propono/components/queue_subscription.rb
propono-0.8.2 lib/propono/components/queue_subscription.rb
propono-0.8.0 lib/propono/components/queue_subscription.rb
propono-0.7.0 lib/propono/components/queue_subscription.rb
propono-0.6.3 lib/propono/components/queue_subscription.rb
propono-0.6.1 lib/propono/components/queue_subscription.rb
propono-0.6.0 lib/propono/components/queue_subscription.rb