diff --git a/.rubocop.yml b/.rubocop.yml index 4a34c43..5a741f2 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -14,4 +14,8 @@ Layout/LineLength: Max: 120 Metrics/BlockLength: - IgnoredMethods: ['describe', 'context'] \ No newline at end of file + IgnoredMethods: ["describe", "context"] + +Lint/MissingSuper: + Exclude: + - "/**/*" diff --git a/lib/pipefy_message/providers/aws_broker.rb b/lib/pipefy_message/providers/aws_broker.rb index 46ad36c..c6230dc 100644 --- a/lib/pipefy_message/providers/aws_broker.rb +++ b/lib/pipefy_message/providers/aws_broker.rb @@ -4,30 +4,32 @@ module PipefyMessage module Providers class AwsBroker < Broker - @@default_options = { - access_key_id: (ENV["AWS_ACCESS_KEY_ID"] || "foo"), - secret_access_key: (ENV["AWS_SECRET_ACCESS_KEY"] || "bar"), - endpoint: (ENV["AWS_ENDPOINT"] || "http://localhost:4566"), - region: (ENV["AWS_REGION"] || "us-east-1"), - stub_responses: (ENV["AWS_CLI_STUB_RESPONSE"] || false) - } - def initialize(queue_name) - @config = AwsBroker.config_options + @config = build_options Aws.config.update(@config) @sqs = Aws::SQS::Client.new + # require 'pry'; binding.pry queue_url = @sqs.get_queue_url({ queue_name: queue_name }).queue_url @poller = Aws::SQS::QueuePoller.new(queue_url) @wait_time_seconds = 10 - super - rescue (Aws::SQS::Errors::NonExistentQueue) => e + rescue Aws::SQS::Errors::NonExistentQueue, Seahorse::Client::NetworkingError => e raise PipefyMessage::Providers::Errors::ResourceError, e.message end - def self.config_options - @@default_options + def default_options + { + access_key_id: (ENV["AWS_ACCESS_KEY_ID"] || "foo"), + secret_access_key: (ENV["AWS_SECRET_ACCESS_KEY"] || "bar"), + endpoint: (ENV["AWS_ENDPOINT"] || "http://localhost:4566"), + region: (ENV["AWS_REGION"] || "us-east-1"), + stub_responses: (ENV["AWS_CLI_STUB_RESPONSE"] == "true") + } + end + + def build_options + default_options.merge({}) end def poller diff --git a/lib/pipefy_message/providers/errors.rb b/lib/pipefy_message/providers/errors.rb index 623d182..7efaf90 100644 --- a/lib/pipefy_message/providers/errors.rb +++ b/lib/pipefy_message/providers/errors.rb @@ -1,11 +1,12 @@ +# frozen_string_literal: true module PipefyMessage - module Providers - module Errors - class ResourceError < StandardError - def initialize(msg="ResourceError") - super - end - end + module Providers + module Errors + class ResourceError < RuntimeError + def initialize(msg = "ResourceError") + super end + end end -end \ No newline at end of file + end +end diff --git a/lib/pipefy_message/worker.rb b/lib/pipefy_message/worker.rb index 0a5f44b..b03011b 100644 --- a/lib/pipefy_message/worker.rb +++ b/lib/pipefy_message/worker.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'singleton' +require "singleton" module PipefyMessage # ClassMethods @@ -8,35 +8,33 @@ module Worker def self.included(base) base.extend(ClassMethods) end - + # ClassMethods - module ClassMethods - def pipefymessage_options(opts = {}) + module ClassMethods + def pipefymessage_options(opts = {}) options_hash = PipefyMessage.default_worker_options.merge(opts.transform_keys(&:to_s)) options_hash.each do |k, v| singleton_class.class_eval { attr_accessor k } send("#{k}=", v) end end - - def perform_async() - begin - obj = self.new - build_instance_broker.poller do |message| - obj.perform(message) - end - rescue Exception => exception - # TODO: Implement retry - raise exception + + def perform_async + obj = new + build_instance_broker.poller do |message| + obj.perform(message) end + rescue Exception => e + # TODO: Implement retry + raise e end + end - def build_instance_broker() - map = {"aws" => "PipefyMessage::Providers::AwsBroker"} - require_relative "providers/#{self.broker}_broker" - - map[self.broker].constantize.new(self.queue) - end + def build_instance_broker + map = { "aws" => "PipefyMessage::Providers::AwsBroker" } + require_relative "providers/#{broker}_broker" + + map[broker].constantize.new(queue) end end end diff --git a/spec/providers/aws_broker_spec.rb b/spec/providers/aws_broker_spec.rb index d4d689b..68ca585 100644 --- a/spec/providers/aws_broker_spec.rb +++ b/spec/providers/aws_broker_spec.rb @@ -5,11 +5,20 @@ RSpec.describe PipefyMessage::Providers::AwsBroker do context "#AwsBroker" do before do - ENV["AWS_CLI_STUB_RESPONSE"] = "true" + stub_const("ENV", ENV.to_hash.merge("AWS_CLI_STUB_RESPONSE" => "true")) end - describe "should raise Errors" do it "QueueNonExistError" do + allow_any_instance_of(Aws::SQS::Client) + .to receive(:get_queue_url) + .with({ queue_name: "my_queue" }) + .and_raise( + Aws::SQS::Errors::NonExistentQueue.new( + double(Aws::SQS::Client), + "The specified queue my_queue does not exist for this wsdl version" + ) + ) + expect do PipefyMessage::Providers::AwsBroker.new("my_queue") end.to raise_error(PipefyMessage::Providers::Errors::ResourceError, diff --git a/spec/worker_spec.rb b/spec/worker_spec.rb index 5f2b9b5..b496bf2 100644 --- a/spec/worker_spec.rb +++ b/spec/worker_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true -RSpec.describe PipefyMessage::Worker do - +RSpec.describe PipefyMessage::Worker do $result =~ nil class MockBroker < PipefyMessage::Providers::Broker @@ -9,12 +8,13 @@ def poller yield("test") end end + class MockBrokerFail < PipefyMessage::Providers::Broker def poller raise PipefyMessage::Providers::Errors::ResourceError end end - + class TestWorker include PipefyMessage::Worker pipefymessage_options broker: "aws", queue: "pipefy-local-queue" @@ -28,14 +28,14 @@ def perform(message) describe "#perform" do it "should call #perform from child instance when call #perform_async with success" do allow(TestWorker).to receive(:build_instance_broker).and_return(MockBroker.new) - + TestWorker.perform_async expect($result).to eq "test" end it "should call #perform from child instance when call #perform_async with fail(raise a ResourceError)" do allow(TestWorker).to receive(:build_instance_broker).and_return(MockBrokerFail.new) - expect{ TestWorker.perform_async }.to raise_error(PipefyMessage::Providers::Errors::ResourceError) + expect { TestWorker.perform_async }.to raise_error(PipefyMessage::Providers::Errors::ResourceError) end end @@ -44,6 +44,4 @@ def perform(message) expect(TestWorker.broker).to eq "aws" end end - - end