diff --git a/lib/shoryuken/queue.rb b/lib/shoryuken/queue.rb index 9c0ea711..04bc3531 100644 --- a/lib/shoryuken/queue.rb +++ b/lib/shoryuken/queue.rb @@ -8,9 +8,9 @@ class Queue attr_accessor :name, :client, :url - def initialize(client, name_or_url) + def initialize(client, name_or_url_or_arn) self.client = client - set_name_and_url(name_or_url) + set_name_and_url(name_or_url_or_arn) end def visibility_timeout @@ -65,17 +65,34 @@ def set_by_url(url) self.url = url end - def set_name_and_url(name_or_url) - if name_or_url.include?('://') - set_by_url(name_or_url) + def arn_to_url(arn_str) + _, _, _, region, account_id, resource = arn_str.split(':') + + required = [region, account_id, resource] + valid = required.none?(&:empty?) + + raise "please pass a shoryuken queue ARN containing, account_id, and resource values (#{arn_str})" unless valid + + "https://sqs.#{region}.amazonaws.com/#{account_id}/#{resource}" + end + + def set_name_and_url(name_or_url_or_arn) + if name_or_url_or_arn.include?('://') + set_by_url(name_or_url_or_arn) # anticipate the fifo? checker for validating the queue URL return fifo? end - set_by_name(name_or_url) + if name_or_url_or_arn.include?('arn:') + url = arn_to_url(name_or_url_or_arn) + set_by_url(url) + return + end + + set_by_name(name_or_url_or_arn) rescue Aws::Errors::NoSuchEndpointError, Aws::SQS::Errors::NonExistentQueue => ex - raise ex, "The specified queue #{name_or_url} does not exist." + raise ex, "The specified queue #{name_or_url_or_arn} does not exist." end def queue_attributes diff --git a/spec/shoryuken/queue_spec.rb b/spec/shoryuken/queue_spec.rb index e514d34a..099046c0 100644 --- a/spec/shoryuken/queue_spec.rb +++ b/spec/shoryuken/queue_spec.rb @@ -39,6 +39,27 @@ end end + context 'when queue ARN supplied' do + let(:queue_arn) { "arn:aws:sqs:ap-southeast-2:000000000000:queue-name" } + + it 'instantiates by URL and validate the URL' do + subject = described_class.new(sqs, queue_arn) + + expect(subject.name).to eq("queue-name") + expect(subject.url).to eq("https://sqs.ap-southeast-2.amazonaws.com/000000000000/queue-name") + end + end + + context 'when inadequate queue ARN supplied' do + let(:queue_arn) { "arn:aws:sqs::000000000000:queue-name" } + + it 'raises an error' do + expect do + described_class.new(sqs, queue_arn) + end.to raise_error("please pass a shoryuken queue ARN containing, account_id, and resource values (arn:aws:sqs::000000000000:queue-name)") + end + end + context 'when queue name supplied' do subject { described_class.new(sqs, queue_name) }