From 156364e743ca543d1d899c2884d47e2480fd1c56 Mon Sep 17 00:00:00 2001 From: Ahmad Elassuty Date: Fri, 21 Aug 2020 00:49:07 +0200 Subject: [PATCH] add rubocop as development dependency (#4) * add rubocop and fix offenses --- .github/workflows/quality.yaml | 16 +++++++ .rubocop.yml | 16 +++++++ .ruby-gemset | 1 + .ruby-version | 1 + .travis.yml | 6 --- Gemfile | 7 ++- Gemfile.lock | 24 +++++++++- Rakefile | 8 ++-- bin/console | 8 ++-- event_router.gemspec | 37 ++++++++------- lib/event_router.rb | 45 +++++++------------ lib/event_router/configuration.rb | 20 ++++----- lib/event_router/deliver_event_job.rb | 10 +++-- lib/event_router/destination.rb | 4 +- lib/event_router/error.rb | 1 - .../errors/unsupported_option_error.rb | 5 +-- lib/event_router/event.rb | 3 +- lib/event_router/event_serializer.rb | 12 ++--- lib/event_router/version.rb | 4 +- lib/examples/event_store/order_placed.rb | 3 +- lib/examples/notifications.rb | 8 ++-- lib/examples/order_placed.rb | 10 ++--- lib/examples/payment_received.rb | 2 +- spec/event_router/configuration_spec.rb | 20 ++++----- spec/event_router_spec.rb | 10 ++--- spec/spec_helper.rb | 6 +-- 26 files changed, 164 insertions(+), 123 deletions(-) create mode 100644 .github/workflows/quality.yaml create mode 100644 .rubocop.yml create mode 100644 .ruby-gemset create mode 100644 .ruby-version delete mode 100644 .travis.yml diff --git a/.github/workflows/quality.yaml b/.github/workflows/quality.yaml new file mode 100644 index 0000000..608dca5 --- /dev/null +++ b/.github/workflows/quality.yaml @@ -0,0 +1,16 @@ +name: Quality +on: push + +jobs: + rubocop: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - uses: ruby/setup-ruby@v1 + with: + bundler-cache: true + + - run: bundle + - run: bundle exec rubocop diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..7952ac2 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,16 @@ +AllCops: + NewCops: enable + +Style/FrozenStringLiteralComment: + Exclude: + - spec/**/* + - bin/**/* + +Metrics/BlockLength: + Exclude: + - spec/**/* + +# [TODO] Turn on documentation rule +Style/Documentation: + Enabled: false + diff --git a/.ruby-gemset b/.ruby-gemset new file mode 100644 index 0000000..ac58625 --- /dev/null +++ b/.ruby-gemset @@ -0,0 +1 @@ +event_router diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..bff6ce5 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +ruby-2.7.1 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 774944c..0000000 --- a/.travis.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- -language: ruby -cache: bundler -rvm: - - 2.6.3 -before_install: gem install bundler -v 2.1.4 diff --git a/Gemfile b/Gemfile index b73877d..3f9d986 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,6 @@ -source "https://rubygems.org" +# frozen_string_literal: true + +source 'https://rubygems.org' # Specify your gem's dependencies in event_router.gemspec gemspec - -gem "rake", "~> 12.0" -gem "rspec", "~> 3.0" diff --git a/Gemfile.lock b/Gemfile.lock index c5f3e68..3c10252 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -16,6 +16,7 @@ GEM minitest (~> 5.1) tzinfo (~> 1.1) zeitwerk (~> 2.2, >= 2.2.2) + ast (2.4.1) byebug (11.1.3) coderay (1.1.3) concurrent-ruby (1.1.7) @@ -27,6 +28,9 @@ GEM concurrent-ruby (~> 1.0) method_source (1.0.0) minitest (5.14.1) + parallel (1.19.2) + parser (2.7.1.4) + ast (~> 2.4.1) pry (0.13.1) coderay (~> 1.1) method_source (~> 1.0) @@ -34,8 +38,10 @@ GEM byebug (~> 11.0) pry (~> 0.13.0) rack (2.2.3) - rake (12.3.3) + rainbow (3.0.0) redis (4.2.1) + regexp_parser (1.7.1) + rexml (3.2.4) rspec (3.9.0) rspec-core (~> 3.9.0) rspec-expectations (~> 3.9.0) @@ -49,6 +55,18 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.9.0) rspec-support (3.9.1) + rubocop (0.89.1) + parallel (~> 1.10) + parser (>= 2.7.1.1) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.7) + rexml + rubocop-ast (>= 0.3.0, < 1.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 2.0) + rubocop-ast (0.3.0) + parser (>= 2.7.1.4) + ruby-progressbar (1.10.1) sidekiq (6.1.1) connection_pool (>= 2.2.2) rack (~> 2.0) @@ -56,6 +74,7 @@ GEM thread_safe (0.3.6) tzinfo (1.2.7) thread_safe (~> 0.1) + unicode-display_width (1.7.0) zeitwerk (2.4.0) PLATFORMS @@ -63,9 +82,10 @@ PLATFORMS DEPENDENCIES event_router! + pry pry-byebug - rake (~> 12.0) rspec (~> 3.0) + rubocop sidekiq BUNDLED WITH diff --git a/Rakefile b/Rakefile index b7e9ed5..82bb534 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,8 @@ -require "bundler/gem_tasks" -require "rspec/core/rake_task" +# frozen_string_literal: true + +require 'bundler/gem_tasks' +require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) -task :default => :spec +task default: :spec diff --git a/bin/console b/bin/console index fae91f0..87a0117 100755 --- a/bin/console +++ b/bin/console @@ -1,7 +1,7 @@ #!/usr/bin/env ruby -require "bundler/setup" -require "event_router" +require 'bundler/setup' +require 'event_router' # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. @@ -10,5 +10,5 @@ require "event_router" # require "pry" # Pry.start -require "irb" -IRB.start(__FILE__) +require 'pry' +Pry.start(__FILE__) diff --git a/event_router.gemspec b/event_router.gemspec index e582c97..e9c13da 100644 --- a/event_router.gemspec +++ b/event_router.gemspec @@ -1,34 +1,39 @@ +# frozen_string_literal: true + require_relative 'lib/event_router/version' Gem::Specification.new do |spec| - spec.name = "event_router" + spec.name = 'event_router' spec.version = EventRouter::VERSION - spec.authors = ["Ahmad Elassuty"] - spec.email = ["ahmad.elassuty@gmail.com"] + spec.authors = ['Ahmad Elassuty'] + spec.email = ['ahmad.elassuty@gmail.com'] - spec.summary = "A simple event router gem" - spec.homepage = "https://github.com/ahmad-elassuty/event_router" - spec.license = "MIT" - spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0") + spec.summary = 'A simple event router gem' + spec.homepage = 'https://github.com/ahmad-elassuty/event_router' + spec.license = 'MIT' + spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0') - spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'" + spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'" - spec.metadata["homepage_uri"] = spec.homepage - spec.metadata["source_code_uri"] = spec.homepage + spec.metadata['homepage_uri'] = spec.homepage + spec.metadata['source_code_uri'] = spec.homepage # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. - spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do + spec.files = Dir.chdir(File.expand_path(__dir__)) do `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } end - spec.bindir = "exe" + spec.bindir = 'exe' spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } - spec.require_paths = ["lib"] + spec.require_paths = ['lib'] # Dependencies - spec.add_dependency "activejob" + spec.add_dependency 'activejob' # Development dependencies - spec.add_development_dependency "sidekiq" - spec.add_development_dependency "pry-byebug" + spec.add_development_dependency 'pry' + spec.add_development_dependency 'pry-byebug' + spec.add_development_dependency 'rspec', '~> 3.0' + spec.add_development_dependency 'rubocop' + spec.add_development_dependency 'sidekiq' end diff --git a/lib/event_router.rb b/lib/event_router.rb index bf3dad6..1ff77aa 100644 --- a/lib/event_router.rb +++ b/lib/event_router.rb @@ -1,36 +1,23 @@ -require "event_router/version" -require "event_router/error" -require "active_job" -require "event_router/configuration" -require "event_router/destination" -require "event_router/event" -require "event_router/event_serializer" -require "event_router/deliver_event_job" - -require "examples/notifications" -require "examples/event_store/order_placed" -require "examples/order_placed" -require "examples/payment_received" - -require "pry" +# frozen_string_literal: true -module EventRouter - module_function - - def schedule_single_event - ActiveJob::Serializers.add_serializers EventSerializer - - Examples::OrderPlaced.publish(order_id: 1) - end +require 'event_router/version' +require 'event_router/error' +require 'active_job' +require 'event_router/configuration' +require 'event_router/destination' +require 'event_router/event' +require 'event_router/event_serializer' +require 'event_router/deliver_event_job' - def schedule_multiple_events - ActiveJob::Serializers.add_serializers EventSerializer +require 'examples/notifications' +require 'examples/event_store/order_placed' +require 'examples/order_placed' +require 'examples/payment_received' - event_1 = Examples::OrderPlaced.new(order_id: 1) - event_2 = Examples::PaymentReceived.new(order_id: 2) +require 'pry' - publish(event_1, event_2) - end +module EventRouter + module_function def publish(*events) correlation_id = events.first.correlation_id diff --git a/lib/event_router/configuration.rb b/lib/event_router/configuration.rb index e2b76a3..50040b5 100644 --- a/lib/event_router/configuration.rb +++ b/lib/event_router/configuration.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require_relative "errors/unsupported_option_error" +require_relative 'errors/unsupported_option_error' module EventRouter class Configuration @@ -27,25 +27,25 @@ def initialize end def delivery_adapter=(adapter) - validate_inclusion!(:delivery_adapter, adapter, DELIVERY_ADAPTERS) + validate_inclusion(:delivery_adapter, adapter, DELIVERY_ADAPTERS) @delivery_adapter = adapter end def delivery_strategy=(strategy) - validate_inclusion!(:delivery_strategy, strategy, DELIVERY_STRATEGIES) + validate_inclusion(:delivery_strategy, strategy, DELIVERY_STRATEGIES) @delivery_strategy = strategy end - private + private - def validate_inclusion!(config, option, supported_options) - unless supported_options.include?(option) - raise Errors::UnsupportedOptionError.new( - config: config, option: option, supported_options: supported_options - ) - end + def validate_inclusion(config, option, supported_options) + return if supported_options.include?(option) + + raise Errors::UnsupportedOptionError.new( + config: config, option: option, supported_options: supported_options + ) end end end diff --git a/lib/event_router/deliver_event_job.rb b/lib/event_router/deliver_event_job.rb index e13fae6..ab71910 100644 --- a/lib/event_router/deliver_event_job.rb +++ b/lib/event_router/deliver_event_job.rb @@ -1,14 +1,18 @@ # frozen_string_literal: true -require "pry" +require 'pry' module EventRouter class DeliverEventJob < ActiveJob::Base self.queue_adapter = :sidekiq def perform(destination, event, payload) - binding.pry - event.destinations[destination]&.process(event, payload) + destination = event.destinations[destination] + + return if destination.blank? + + payload ||= destination.payload_for(event) + destination.process(event, payload) end end end diff --git a/lib/event_router/destination.rb b/lib/event_router/destination.rb index 4b2bddb..518ef01 100644 --- a/lib/event_router/destination.rb +++ b/lib/event_router/destination.rb @@ -4,7 +4,7 @@ module EventRouter class Destination # Attributes attr_reader :name, :handler, :handler_method, - :prefetch_payload, :payload_method + :prefetch_payload, :payload_method # Constants DEFAULT_ATTRIBUTES = { @@ -31,7 +31,7 @@ def process(event, payload) handler.send( handler_method || event.name, event: event, - payload: payload || payload_for(event) + payload: payload ) end diff --git a/lib/event_router/error.rb b/lib/event_router/error.rb index 02a0446..91dba3b 100644 --- a/lib/event_router/error.rb +++ b/lib/event_router/error.rb @@ -3,4 +3,3 @@ module EventRouter class Error < StandardError; end end - diff --git a/lib/event_router/errors/unsupported_option_error.rb b/lib/event_router/errors/unsupported_option_error.rb index f8c29b5..3f29340 100644 --- a/lib/event_router/errors/unsupported_option_error.rb +++ b/lib/event_router/errors/unsupported_option_error.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require_relative "../error" +require_relative '../error' module EventRouter module Errors @@ -14,8 +14,7 @@ def initialize(message: nil, config:, option:, supported_options:) end def message - "Unsupported #{@option} for #{@config} configuration." + - " Currently supports #{@supported_options}" + "Unsupported #{@option} for #{@config} configuration. Currently supports #{@supported_options}" end end end diff --git a/lib/event_router/event.rb b/lib/event_router/event.rb index 0d50370..e21692a 100644 --- a/lib/event_router/event.rb +++ b/lib/event_router/event.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "securerandom" +require 'securerandom' module EventRouter class Event @@ -46,5 +46,4 @@ def publish(**attrs) end end end - end diff --git a/lib/event_router/event_serializer.rb b/lib/event_router/event_serializer.rb index a1879d6..7b008f2 100644 --- a/lib/event_router/event_serializer.rb +++ b/lib/event_router/event_serializer.rb @@ -12,15 +12,15 @@ def serialize(event) end def deserialize(hash) - hash["_er_klass"].constantize.new( - uid: hash["uid"], - correlation_id: hash["correlation_id"], - created_at: Time.parse(hash["created_at"]), - **ActiveJob::Arguments.deserialize(hash["payload"]).to_h + hash['_er_klass'].constantize.new( + uid: hash['uid'], + correlation_id: hash['correlation_id'], + created_at: Time.parse(hash['created_at']), + **ActiveJob::Arguments.deserialize(hash['payload']).to_h ) end - private + private def klass Event diff --git a/lib/event_router/version.rb b/lib/event_router/version.rb index d7532e0..e7af1cc 100644 --- a/lib/event_router/version.rb +++ b/lib/event_router/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module EventRouter - VERSION = "0.1.0" + VERSION = '0.1.0' end diff --git a/lib/examples/event_store/order_placed.rb b/lib/examples/event_store/order_placed.rb index 6505a52..de62a00 100644 --- a/lib/examples/event_store/order_placed.rb +++ b/lib/examples/event_store/order_placed.rb @@ -4,8 +4,7 @@ module EventRouter module Examples module EventStore class OrderPlaced - def self.handle(event) - binding.pry + def self.handle(_event) true end end diff --git a/lib/examples/notifications.rb b/lib/examples/notifications.rb index db34c9c..3c77daa 100644 --- a/lib/examples/notifications.rb +++ b/lib/examples/notifications.rb @@ -3,15 +3,13 @@ module EventRouter module Examples module Notifications - module_function + module_function - def order_placed(event:, payload:) - binding.pry + def order_placed(_args) true end - def payment_received(event) - binding.pry + def payment_received(_event) true end end diff --git a/lib/examples/order_placed.rb b/lib/examples/order_placed.rb index 1fc976d..0dde787 100644 --- a/lib/examples/order_placed.rb +++ b/lib/examples/order_placed.rb @@ -4,13 +4,13 @@ module EventRouter module Examples class OrderPlaced < EventRouter::Event deliver_to :notifications, - handler: EventRouter::Examples::Notifications + handler: EventRouter::Examples::Notifications deliver_to :event_store, - handler: EventRouter::Examples::EventStore::OrderPlaced, - handler_method: :handle, - prefetch_payload: true, - payload_method: :store_payload + handler: EventRouter::Examples::EventStore::OrderPlaced, + handler_method: :handle, + prefetch_payload: true, + payload_method: :store_payload # Custom payload methods def notifications_payload diff --git a/lib/examples/payment_received.rb b/lib/examples/payment_received.rb index 91d3f29..95d06eb 100644 --- a/lib/examples/payment_received.rb +++ b/lib/examples/payment_received.rb @@ -4,7 +4,7 @@ module EventRouter module Examples class PaymentReceived < EventRouter::Event deliver_to :notifications, - handler: EventRouter::Examples::Notifications + handler: EventRouter::Examples::Notifications end end end diff --git a/spec/event_router/configuration_spec.rb b/spec/event_router/configuration_spec.rb index 30d1032..0fe4ff0 100644 --- a/spec/event_router/configuration_spec.rb +++ b/spec/event_router/configuration_spec.rb @@ -1,37 +1,37 @@ RSpec.describe EventRouter::Configuration do subject(:config) { described_class.new } - describe ".new" do - it "initializes with memory delivery adapter by default" do + describe '.new' do + it 'initializes with memory delivery adapter by default' do expect(subject.delivery_adapter).to eq(:memory) end - it "initializes with async delivery strategy by default" do + it 'initializes with async delivery strategy by default' do expect(subject.delivery_strategy).to eq(:async) end end - describe "#delivery_adapter=" do + describe '#delivery_adapter=' do EventRouter::Configuration::DELIVERY_ADAPTERS.each do |adapter| it "supports #{adapter} adapter" do expect { config.delivery_adapter = adapter }.to_not raise_error end - it "updates the configuration" do + it 'updates the configuration' do config.delivery_adapter = adapter expect(config.delivery_adapter).to eq(adapter) end end - context "when given unsupported option" do - it "raises unsupported option error" do + context 'when given unsupported option' do + it 'raises unsupported option error' do expect { config.delivery_adapter = :invalid }.to raise_error(EventRouter::Errors::UnsupportedOptionError) end end end - describe "#delivery_strategy=" do + describe '#delivery_strategy=' do EventRouter::Configuration::DELIVERY_STRATEGIES.each do |strategy| it "supports #{strategy} strategy" do expect { config.delivery_strategy = strategy }.to_not raise_error @@ -44,8 +44,8 @@ end end - context "when given unsupported option" do - it "raises unsupported option error" do + context 'when given unsupported option' do + it 'raises unsupported option error' do expect { config.delivery_strategy = :invalid }.to raise_error(EventRouter::Errors::UnsupportedOptionError) end end diff --git a/spec/event_router_spec.rb b/spec/event_router_spec.rb index aabafd5..34237da 100644 --- a/spec/event_router_spec.rb +++ b/spec/event_router_spec.rb @@ -1,18 +1,18 @@ RSpec.describe EventRouter do - it "has a version number" do + it 'has a version number' do expect(EventRouter::VERSION).not_to be nil end - describe ".configuration" do + describe '.configuration' do it { expect(EventRouter.configuration).to be_instance_of(EventRouter::Configuration) } end - describe ".configure" do - it "yields the configuration" do + describe '.configure' do + it 'yields the configuration' do expect { |b| EventRouter.configure(&b) }.to yield_with_args(EventRouter.configuration) end - it "memoizes the configurations" do + it 'memoizes the configurations' do expect do EventRouter.configure { |c| c.delivery_strategy = :sync } end.to change { EventRouter.configuration.delivery_strategy }.to(:sync) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a95cf47..95935db 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,9 +1,9 @@ -require "bundler/setup" -require "event_router" +require 'bundler/setup' +require 'event_router' RSpec.configure do |config| # Enable flags like --only-failures and --next-failure - config.example_status_persistence_file_path = ".rspec_status" + config.example_status_persistence_file_path = '.rspec_status' # Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching!