Sha256: e4134a2f50922415fda5367489e450dbf783a98a5470762ff4c82bb4e785117f

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

module Propono
  class QueueSubscription

    include Sns
    include Sqs

    attr_reader :topic_arn, :queue_name, :queue

    def self.create(topic_id, options = {})
      new(topic_id, options).tap do |subscription|
        subscription.create
      end
    end

    def initialize(topic_id, options = {})
      @topic_id = topic_id
      @suffixed_topic_id = "#{topic_id}#{Propono.config.queue_suffix}"
      @queue_name = "#{Propono.config.application_name.gsub(" ", "_")}-#{@suffixed_topic_id}#{options[:queue_name_suffix]}"
    end

    def create
      raise ProponoError.new("topic_id is nil") unless @topic_id
      @topic = TopicCreator.find_or_create(@suffixed_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

    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

2 entries across 2 versions & 1 rubygems

Version Path
propono-1.0.0.rc2 lib/propono/components/queue_subscription.rb
propono-1.0.0.rc1 lib/propono/components/queue_subscription.rb