strum-esb 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 360c4b7cd86f34d2dd3628104474c6948cb926b852c493880b931d0f7a1730e6
4
- data.tar.gz: 963fc92e530b52200a58e205247ded0728e1247aa8c865c69f3aa5d56fe729fa
3
+ metadata.gz: 2caca6a995f15c20737aa3ea31ee69be5660b058cf4ab632740225d31797c272
4
+ data.tar.gz: 81d1fd921e9e610b8fe65f275200485d2476a0761e205b229c0a09f790c65da7
5
5
  SHA512:
6
- metadata.gz: 9ffa2333d5dbbd263b32c7944acbfe2f82aa9233c8f7e6f2337f2444f134fb88b33ec260e377c1d4126609ad1194f62a24acce30a0cdc34f919f027da62db9a1
7
- data.tar.gz: 740715b0c16c4c2f270f94fd2fca43aea88194f9c0352860b31fdb74758ffafa92cbaefb755f783b6de540473779d48a81a4b45ed50f8bb6270e7e350d3c8126
6
+ metadata.gz: 4391409756c8fbc6c346ce9124d72052d849846f9b88eb7f26885d1a1405b66fe21db5129a644811361c6d77a35071b3887cdcb00cce30b68ee6593ed3ea380d
7
+ data.tar.gz: 92b3f5be2af199ec1c803cda251667e11206c1eca20d88efdb33bdfc2a72eb5dc8c55760d767aa22be7bba5e453c631b47f087825cf6426f38043f0a3cedb38c
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,3 @@
1
+ include:
2
+ - project: 'strum-rb/ci-cd'
3
+ file: '/strum-rb/.gitlab-ci.yml'
data/Gemfile CHANGED
@@ -1,12 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
5
  # Specify your gem's dependencies in strum-esb.gemspec
4
6
  gemspec
5
7
 
6
8
  gem "bundler", "~> 2.1.4"
7
- gem "rake", "~> 12"
8
9
  gem "rspec", "~> 3"
9
- gem "rubocop", "~> 0.62.0"
10
+ gem "rubocop", "~> 0.90.0"
10
11
 
11
12
  gem "debase", "~> 0.2.4"
12
- gem "ruby-debug-ide", "~> 0.7.0"
13
+ gem "ruby-debug-ide", "~> 0.7.2"
14
+
15
+ gem "dry-configurable", "~> 0.11"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- strum-esb (0.0.1)
4
+ strum-esb (0.1.0)
5
5
  bunny (~> 2.15)
6
6
  connection_pool (~> 2.2.2)
7
7
  json (~> 2.3)
@@ -10,46 +10,56 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- amq-protocol (2.3.1)
14
- ast (2.4.0)
15
- bunny (2.15.0)
13
+ amq-protocol (2.3.2)
14
+ ast (2.4.1)
15
+ bunny (2.17.0)
16
16
  amq-protocol (~> 2.3, >= 2.3.1)
17
- concurrent-ruby (1.1.6)
18
- connection_pool (2.2.2)
17
+ concurrent-ruby (1.1.7)
18
+ connection_pool (2.2.5)
19
19
  debase (0.2.4.1)
20
20
  debase-ruby_core_source (>= 0.10.2)
21
- debase-ruby_core_source (0.10.9)
22
- diff-lcs (1.3)
23
- jaro_winkler (1.5.4)
24
- json (2.3.0)
25
- parallel (1.19.1)
26
- parser (2.7.1.1)
27
- ast (~> 2.4.0)
28
- powerpack (0.1.2)
21
+ debase-ruby_core_source (0.10.11)
22
+ diff-lcs (1.4.4)
23
+ dry-configurable (0.11.6)
24
+ concurrent-ruby (~> 1.0)
25
+ dry-core (~> 0.4, >= 0.4.7)
26
+ dry-equalizer (~> 0.2)
27
+ dry-core (0.5.0)
28
+ concurrent-ruby (~> 1.0)
29
+ dry-equalizer (0.3.0)
30
+ json (2.5.1)
31
+ parallel (1.20.1)
32
+ parser (2.7.2.0)
33
+ ast (~> 2.4.1)
29
34
  rainbow (3.0.0)
30
35
  rake (12.3.3)
31
- rspec (3.9.0)
32
- rspec-core (~> 3.9.0)
33
- rspec-expectations (~> 3.9.0)
34
- rspec-mocks (~> 3.9.0)
35
- rspec-core (3.9.1)
36
- rspec-support (~> 3.9.1)
37
- rspec-expectations (3.9.1)
36
+ regexp_parser (2.0.0)
37
+ rexml (3.2.4)
38
+ rspec (3.10.0)
39
+ rspec-core (~> 3.10.0)
40
+ rspec-expectations (~> 3.10.0)
41
+ rspec-mocks (~> 3.10.0)
42
+ rspec-core (3.10.0)
43
+ rspec-support (~> 3.10.0)
44
+ rspec-expectations (3.10.0)
38
45
  diff-lcs (>= 1.2.0, < 2.0)
39
- rspec-support (~> 3.9.0)
40
- rspec-mocks (3.9.1)
46
+ rspec-support (~> 3.10.0)
47
+ rspec-mocks (3.10.0)
41
48
  diff-lcs (>= 1.2.0, < 2.0)
42
- rspec-support (~> 3.9.0)
43
- rspec-support (3.9.2)
44
- rubocop (0.62.0)
45
- jaro_winkler (~> 1.5.1)
49
+ rspec-support (~> 3.10.0)
50
+ rspec-support (3.10.0)
51
+ rubocop (0.90.0)
46
52
  parallel (~> 1.10)
47
- parser (>= 2.5, != 2.5.1.1)
48
- powerpack (~> 0.1)
53
+ parser (>= 2.7.1.1)
49
54
  rainbow (>= 2.2.2, < 4.0)
55
+ regexp_parser (>= 1.7)
56
+ rexml
57
+ rubocop-ast (>= 0.3.0, < 1.0)
50
58
  ruby-progressbar (~> 1.7)
51
- unicode-display_width (~> 1.4.0)
52
- ruby-debug-ide (0.7.0)
59
+ unicode-display_width (>= 1.4.0, < 2.0)
60
+ rubocop-ast (0.8.0)
61
+ parser (>= 2.7.1.5)
62
+ ruby-debug-ide (0.7.2)
53
63
  rake (>= 0.8.1)
54
64
  ruby-progressbar (1.10.1)
55
65
  serverengine (2.1.1)
@@ -61,8 +71,8 @@ GEM
61
71
  rake (~> 12.3)
62
72
  serverengine (~> 2.1.0)
63
73
  thor
64
- thor (1.0.1)
65
- unicode-display_width (1.4.1)
74
+ thor (1.1.0)
75
+ unicode-display_width (1.7.0)
66
76
 
67
77
  PLATFORMS
68
78
  ruby
@@ -70,10 +80,10 @@ PLATFORMS
70
80
  DEPENDENCIES
71
81
  bundler (~> 2.1.4)
72
82
  debase (~> 0.2.4)
73
- rake (~> 12)
83
+ dry-configurable (~> 0.11)
74
84
  rspec (~> 3)
75
- rubocop (~> 0.62.0)
76
- ruby-debug-ide (~> 0.7.0)
85
+ rubocop (~> 0.90.0)
86
+ ruby-debug-ide (~> 0.7.2)
77
87
  strum-esb!
78
88
 
79
89
  BUNDLED WITH
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Strum::Esb
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/strum/esb`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Useful sender RabbitMQ messages and handle them
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,58 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ####Configuration
24
+
25
+ Exchanges can be customized by each message type:
26
+ ```ruby
27
+ Strum::Esb.config.info_exchange = `demo.info`
28
+ Strum::Esb.config.event_exchange = `demo.events`
29
+ Strum::Esb.config.action_exchange = `demo.actions`
30
+ Strum::Esb.config.notice_exchange = `demo.notice`
31
+ ```
32
+
33
+ ####Sending message:
34
+
35
+ ```ruby
36
+ Strum::Esb::Action.call(`payload`, `action`, `resource`)
37
+ ```
38
+
39
+ ```ruby
40
+ Strum::Esb::Event.success(`payload`, `resource`, `*events`)
41
+ Strum::Esb::Event.failure(`payload`, `resource`, `*events`)
42
+ ```
43
+
44
+ ```ruby
45
+ Strum::Esb::Info.call(`payload`, `resource`)
46
+ ```
47
+
48
+ ```ruby
49
+ Strum::Esb::Notice.call(`payload`, `notice`)
50
+ Strum::Esb::Notice.call(`payload`, `notice`, `resource`)
51
+ ```
52
+
53
+ #### Handling message:
54
+
55
+ ```ruby
56
+ require "strum/esb"
57
+
58
+ module Queues
59
+ class Resource
60
+ include Strum::Esb::Handler
61
+
62
+ from_queue "resource-queue",
63
+ bindings: {
64
+ events: %w[user/create user/update],
65
+
66
+ }
67
+
68
+ def handler(payload)
69
+ # code here ...
70
+ end
71
+ end
72
+ end
73
+ ```
74
+
26
75
 
27
76
  ## Development
28
77
 
@@ -32,5 +81,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
81
 
33
82
  ## Contributing
34
83
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/strum-esb.
84
+ Bug reports and pull requests are welcome on GitHub at https://code.qpard.com/srtrum/strum-esb.
36
85
 
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rspec/core/rake_task"
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
data/bin/console CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "bundler/setup"
4
5
  require "strum/esb"
@@ -10,11 +11,11 @@ require "strum/esb"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- Strum::Esb::Message.rabbit_connection ||= begin
14
- rabbit_connection = Bunny.new
15
- rabbit_connection.start
16
- rabbit_connection
17
- end
14
+ Strum::Esb.config.exchange = "demo.general"
15
+ Strum::Esb.config.info_exchange = "demo.info"
16
+ Strum::Esb.config.event_exchange = "demo.events"
17
+ Strum::Esb.config.action_exchange = "demo.actions"
18
+ Strum::Esb.config.notice_exchange = "demo.notice"
18
19
 
19
20
  require "irb"
20
21
  IRB.start(__FILE__)
data/lib/strum/esb.rb CHANGED
@@ -1,36 +1,57 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "strum/esb/version"
2
4
 
3
5
  require "json"
4
6
  require "bunny"
5
7
  require "sneakers"
6
- require_relative "patch/sneakers/queue_patch"
7
- require_relative "esb/handler"
8
- require_relative "esb/message"
9
- require_relative "esb/action"
10
- require_relative "esb/event"
11
- require_relative "esb/notice"
12
- require_relative "esb/info"
8
+ require "connection_pool"
9
+ require "etc"
10
+ require "strum/patch/sneakers/queue_patch"
11
+ require "strum/esb/handler"
12
+ require "strum/esb/message"
13
+ require "strum/esb/action"
14
+ require "strum/esb/event"
15
+ require "strum/esb/notice"
16
+ require "strum/esb/info"
17
+ require "dry/configurable"
13
18
 
14
19
  module Strum
15
20
  module Esb
21
+ extend Dry::Configurable
22
+
23
+ setting :exchange, "strum.general"
24
+ setting :info_exchange, "strum.info"
25
+ setting :event_exchange, "strum.events"
26
+ setting :action_exchange, "strum.actions"
27
+ setting :notice_exchange, "strum.notice"
28
+ setting :rabbit_channel_pool, begin
29
+ ConnectionPool.new(size: 5, timeout: 5) do
30
+ rabbit_connection = Bunny.new
31
+ rabbit_connection.start
32
+ rabbit_connection.create_channel
33
+ end
34
+ end
35
+ setting :before_fork_hooks, []
36
+ setting :after_fork_hooks, []
37
+
38
+ Strum::Esb.config.before_fork_hooks << proc { DB.disconnect } if defined?(DB)
39
+
40
+ Strum::Esb.config.after_fork_hooks << proc do
41
+ end
42
+
16
43
  class Error < StandardError; end
17
44
 
18
45
  Sneakers.configure(
19
- log: STDOUT,
20
- hooks: {
21
- before_fork: lambda { DB.disconnect if defined?(DB) },
22
- after_fork: lambda do
23
- Strum::Esb::Message.rabbit_connection ||= begin
24
- rabbit_connection = Bunny.new
25
- rabbit_connection.start
26
- rabbit_connection
27
- end
28
- end
29
- },
30
- exchange: "strum.general",
31
- exchange_type: "headers"
46
+ log: STDOUT,
47
+ workers: Etc.nprocessors,
48
+ hooks: {
49
+ before_fork: -> { Strum::Esb.config.before_fork_hooks.each(&:call) },
50
+ after_fork: -> { Strum::Esb.config.after_fork_hooks.each(&:call) }
51
+ },
52
+ exchange: Strum::Esb.config.exchange,
53
+ exchange_type: "headers"
32
54
  )
33
55
  Sneakers.logger.level = Logger::INFO
34
-
35
56
  end
36
57
  end
@@ -7,8 +7,8 @@ module Strum
7
7
  # Action message
8
8
  class Action < Message
9
9
  class << self
10
- def call(payload, action, resource, exchange: "strum.actions", chain: Thread.current[:chain])
11
- publish(headers: { resource: resource, action: action }, payload: payload, exchange: exchange, chain: chain)
10
+ def call(payload, action, resource, exchange: Strum::Esb.config.action_exchange, **opts)
11
+ publish(headers: { resource: resource, action: action }, payload: payload, exchange: exchange, **opts)
12
12
  end
13
13
  end
14
14
  end
@@ -7,13 +7,16 @@ module Strum
7
7
  # Event message
8
8
  class Event < Message
9
9
  class << self
10
- def call(payload, resource, event, state, exchange:, chain:)
11
- publish(headers: { resource: resource, event: event, state: state }, payload: payload, exchange: exchange, chain: chain)
12
- end
13
-
14
10
  %i[success failure].each do |method_name|
15
- define_method method_name do |payload, resource, *events, exchange: "strum.events", chain: Thread.current[:chain]|
16
- events.each { |event| call(payload, resource, event, method_name, exchange: exchange, chain: chain) }
11
+ define_method method_name do |payload, resource, event, exchange: Strum::Esb.config.event_exchange, **opts|
12
+ publish(headers: {
13
+ resource: resource,
14
+ event: event,
15
+ state: method_name
16
+ },
17
+ payload: payload,
18
+ exchange: exchange,
19
+ **opts)
17
20
  end
18
21
  end
19
22
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strum
4
+ module Esb
5
+ class Functions
6
+ class << self
7
+ def action_explain(source)
8
+ exchange, resource_event = source.include?(":") ? source.split(":") : [Strum::Esb.config.action_exchange, source]
9
+ action, resource = resource_event.split("/")
10
+ [exchange, action, resource]
11
+ end
12
+
13
+ def event_explain(source)
14
+ exchange, resource_event = source.include?(":") ? source.split(":") : [Strum::Esb.config.event_exchange, source]
15
+ resource, event, state = resource_event.split("/")
16
+ [exchange, resource, event, state || "success"]
17
+ end
18
+
19
+ def notice_explain(source)
20
+ source.include?(":") ? source.split(":") : [Strum::Esb.config.notice_exchange, source]
21
+ end
22
+
23
+ def info_explain(source)
24
+ source.include?(":") ? source.split(":") : [Strum::Esb.config.info_exchange, source]
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "json"
4
+ require "strum/esb/functions"
4
5
 
5
6
  module Strum
6
7
  module Esb
@@ -10,6 +11,24 @@ module Strum
10
11
  base.class_eval do
11
12
  include Sneakers::Worker
12
13
  end
14
+ base.extend ClassMethods
15
+ end
16
+
17
+ module ClassMethods
18
+ def bind_to(queue, message_type, message_binding, handler = nil)
19
+ bindings ||= (queue_opts && queue_opts[:bindings]) || {}
20
+ bindings[message_type] ||= []
21
+ bindings[message_type] << message_binding
22
+ from_queue queue, bindings: bindings
23
+ _, *msg = Strum::Esb::Functions.public_send("#{message_type}_explain", message_binding)
24
+ @handlers ||= {}
25
+ handler_key = ([message_type] + msg).join("-")
26
+ @handlers[handler_key] = handler.to_s if handler
27
+ end
28
+
29
+ def handlers
30
+ @handlers
31
+ end
13
32
  end
14
33
 
15
34
  def work_with_params(deserialized_msg, _delivery_info, metadata)
@@ -24,26 +43,21 @@ module Strum
24
43
  event = header.call("event")
25
44
  state = header.call("state")
26
45
  info = header.call("info")
27
- chain = header.call("chain")
28
46
  notice = header.call("notice")
29
- Thread.current[:chain] = chain
47
+ Thread.current[:pipeline] = header.call("pipeline")
48
+ Thread.current[:pipeline_id] = header.call("pipeline-id")
49
+
50
+ after_headers_hook
30
51
 
31
52
  methods_names = if action
32
- %W[action_#{action}_#{resource} action_handler]
53
+ action_handler_methods(action, resource)
33
54
  elsif event
34
- result = %W[event_#{resource}_#{event}_#{state}]
35
- result << "event_#{resource}_#{event}" if state.eql?("success")
36
- result << "event_handler"
37
- result
55
+ event_handler_methods(resource, event, state)
38
56
  elsif info
39
- %W[info_#{info} info_handler]
57
+ info_handler_methods(info)
40
58
  elsif notice
41
- result = []
42
- result << "notice_#{resource}_#{notice}" if resource
43
- result << "notice_#{notice}"
44
- result << "notice_handler"
45
- result
46
- end
59
+ notice_handler_methods(resource, notice)
60
+ end
47
61
 
48
62
  method_name = ([*methods_names] << "handler").find { |n| respond_to?(n, include_all: true) }
49
63
 
@@ -55,7 +69,7 @@ module Strum
55
69
  method_params = method(method_name)
56
70
  .parameters
57
71
  .map { |param| _, param_name = param; param_name }
58
- .then { |m| m & %I[action resource event state chain info] }
72
+ .then { |m| m & %I[action resource event state info] }
59
73
  handler_params = method_params.each_with_object({}) { |i, res| res[i] = eval(i.to_s); }
60
74
  logger.debug("Handler #{method_name} found. Payload: #{notice_data}")
61
75
  handler_params.count.positive? ? send(method_name, notice_data, handler_params) : send(method_name, notice_data)
@@ -65,6 +79,46 @@ module Strum
65
79
  logger.error e
66
80
  reject!
67
81
  end
82
+
83
+ def after_headers_hook; end
84
+
85
+ private
86
+
87
+ def action_handler_methods(action, resource)
88
+ if custom_handler = self.class.handlers[["action", action, resource].join("-")]
89
+ %W[#{custom_handler} action_#{action}_#{resource} action_handler]
90
+ else
91
+ %W[action_#{action}_#{resource} action_handler]
92
+ end
93
+ end
94
+
95
+ def event_handler_methods(resource, event, state)
96
+ result = []
97
+ custom_handler = self.class.handlers[["event", resource, event, state].join("-")]
98
+ result << custom_handler if custom_handler
99
+ result << "event_#{resource}_#{event}_#{state}"
100
+ result << "event_#{resource}_#{event}" if state.eql?("success")
101
+ result << "event_handler"
102
+ result
103
+ end
104
+
105
+ def info_handler_methods(info)
106
+ if custom_handler = self.class.handlers[["info", info].join("-")]
107
+ %W[#{custom_handler} info_#{info} info_handler]
108
+ else
109
+ %W[info_#{info} info_handler]
110
+ end
111
+ end
112
+
113
+ def notice_handler_methods(resource, notice)
114
+ custom_handler = self.class.handlers[["notice", resource, notice].join("-")]
115
+ result = []
116
+ result << custom_handler if custom_handler
117
+ result << "notice_#{resource}_#{notice}" if resource
118
+ result << "notice_#{notice}"
119
+ result << "notice_handler"
120
+ result
121
+ end
68
122
  end
69
123
  end
70
- end
124
+ end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "message"
3
+ require "strum/esb/message"
4
4
 
5
5
  module Strum
6
6
  module Esb
7
7
  # Action message
8
8
  class Info < Message
9
9
  class << self
10
- def call(payload, resource, exchange: "strum.info", chain: Thread.current[:chain])
11
- publish(headers: { info: resource }, payload: payload, exchange: exchange, chain: chain)
10
+ def call(payload, resource, exchange: Strum::Esb.config.info_exchange, **opts)
11
+ publish(headers: { info: resource }, payload: payload, exchange: exchange, **opts)
12
12
  end
13
13
  end
14
14
  end
@@ -5,25 +5,13 @@ module Strum
5
5
  # Strum Message
6
6
  class Message
7
7
  class << self
8
- attr_accessor :rabbit_connection
9
-
10
- def publish(exchange:, headers:, payload:, chain:)
11
- rabbit_exchange ||= Strum::Esb::Message.rabbit_channel.headers(exchange, durable: true)
12
- if chain
13
- payload["chain"] ||= chain
14
- headers["chain"] ||= chain
15
- end
8
+ def publish(exchange:, headers:, payload:)
9
+ rabbit_exchange ||= Strum::Esb.config.rabbit_channel_pool.with { |rabbit_channel| rabbit_channel.headers(exchange, durable: true) }
10
+ headers["pipeline"] ||= Thread.current[:pipeline] if Thread.current[:pipeline]
11
+ headers["pipeline-id"] ||= Thread.current[:pipeline_id] if Thread.current[:pipeline_id]
16
12
  rabbit_exchange.publish(payload.to_json, headers: headers, content_type: "application/json")
17
13
  end
18
-
19
- def rabbit_channel
20
- @rabbit_channel ||= begin
21
- raise "Strum::Esb::Message.rabbit_connection must be setup" unless rabbit_connection
22
- rabbit_connection.create_channel
23
- end
24
- end
25
14
  end
26
15
  end
27
16
  end
28
17
  end
29
-
@@ -7,10 +7,10 @@ module Strum
7
7
  # Notice message
8
8
  class Notice < Message
9
9
  class << self
10
- def call(payload, notice, resource = nil, exchange: "strum.notice", chain: Thread.current[:chain])
10
+ def call(payload, notice, resource = nil, exchange: Strum::Esb.config.notice_exchange, **opts)
11
11
  headers = { notice: notice }
12
12
  headers[:resource] = resource if resource
13
- publish(headers: headers, payload: payload, exchange: exchange, chain: chain)
13
+ publish(headers: headers, payload: payload, exchange: exchange, **opts)
14
14
  end
15
15
  end
16
16
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Strum
2
4
  module Esb
3
- VERSION = "0.0.1"
5
+ VERSION = "0.1.0"
4
6
  end
5
7
  end
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "sneakers/queue"
4
+ require "strum/esb/functions"
4
5
 
5
6
  module QueuePatch
6
7
  def subscribe(worker)
7
8
  if @opts[:bindings]
8
- qiwa_subscribe(worker)
9
+ strum_subscribe(worker)
9
10
  elsif @opts[:header_bindings]
10
11
  header_subscribe(worker)
11
12
  else
@@ -17,93 +18,88 @@ module QueuePatch
17
18
  # actions: "get/account",
18
19
  # events: %w[account/update account/delete],
19
20
  # infos: "account"
21
+ # pipeline: {
22
+ # name: "pipeline-name",
23
+ # actions: "get/account",
24
+ # events: %w[account/update account/delete],
25
+ # infos: "account"
26
+ # }
20
27
  # }
21
- def qiwa_subscribe(worker)
28
+
29
+ def strum_subscribe(worker)
22
30
  @opts[:header_bindings] = []
23
31
  actions_subscribes([*(@opts[:bindings][:actions] || @opts[:bindings][:action])])
24
32
  events_subscribes([*(@opts[:bindings][:events] || @opts[:bindings][:event])])
25
33
  infos_subscribes([*(@opts[:bindings][:infos] || @opts[:bindings][:info])])
26
34
  notices_subscribes([*(@opts[:bindings][:notices] || @opts[:bindings][:notice])])
35
+ pipeline_subscribes(@opts[:bindings][:pipeline] || @opts[:bindings][:pipelines])
27
36
  header_subscribe(worker)
28
37
  end
29
38
 
30
- def actions_subscribes(actions)
31
- actions.each do |_action|
32
- exchange, resource_action = _action.split(":")
33
- if resource_action.nil?
34
- resource_action = exchange
35
- exchange = "strum.actions"
36
- end
37
- raise StandardError "binding format must be a `exchange:action/resource`" unless exchange && resource_action
39
+ def pipeline_subscribes(pipelines)
40
+ return if pipelines.nil?
41
+
42
+ actions_subscribes([*(pipelines[:actions] || pipelines[:action])], pipeline: pipelines[:name])
43
+ events_subscribes([*(pipelines[:events] || pipelines[:event])], pipeline: pipelines[:name])
44
+ infos_subscribes([*(pipelines[:infos] || pipelines[:info])], pipeline: pipelines[:name])
45
+ notices_subscribes([*(pipelines[:notices] || pipelines[:notice])], pipeline: pipelines[:name])
46
+ end
38
47
 
39
- action, resource = resource_action.split("/")
40
- raise StandardError "binding format must be a `exchange:action/resource`" unless resource && action
48
+ def actions_subscribes(actions, **custom_headers)
49
+ actions.each do |_action|
50
+ exchange, action, resource = Strum::Esb::Functions.action_explain(_action)
51
+ raise StandardError "action binding format must be `exchange:action/resource`" unless resource && action
41
52
 
42
53
  @opts[:header_bindings] << {
43
- exchange: exchange,
44
- arguments: {
45
- action: action,
46
- resource: resource
47
- }
54
+ exchange: exchange,
55
+ arguments: {
56
+ action: action,
57
+ resource: resource
58
+ }.merge(custom_headers)
48
59
  }
49
60
  end
50
61
  end
51
62
 
52
- def events_subscribes(events)
63
+ def events_subscribes(events, **custom_headers)
53
64
  events.each do |_event|
54
- exchange, resource_event = _event.split(":")
55
- if resource_event.nil?
56
- resource_event = exchange
57
- exchange = "strum.events"
58
- end
59
- raise StandardError "binding format must be `exchange:resource/event{/state}`" unless exchange && resource_event
60
-
61
- resource, event, state = resource_event.split("/")
65
+ exchange, resource, event, state = Strum::Esb::Functions.event_explain(_event)
62
66
  raise StandardError "binding format must be `exchange:resource/event{/state}`" unless resource && event
63
67
 
64
68
  @opts[:header_bindings] << {
65
- exchange: exchange,
66
- arguments: {
67
- resource: resource,
68
- event: event,
69
- state: state || "success"
70
- }
69
+ exchange: exchange,
70
+ arguments: {
71
+ resource: resource,
72
+ event: event,
73
+ state: state
74
+ }.merge(custom_headers)
71
75
  }
72
76
  end
73
77
  end
74
78
 
75
- def infos_subscribes(infos)
79
+ def infos_subscribes(infos, **custom_headers)
76
80
  infos.each do |_info|
77
- exchange, resource = _info.split(":")
78
- if resource.nil?
79
- resource = exchange
80
- exchange = "strum.info"
81
- end
82
- raise StandardError "binding format must be a `exchange:resource`" unless exchange && resource
81
+ exchange, resource = Strum::Esb::Functions.info_explain(_info)
82
+ raise StandardError "info binding format must be a `exchange:resource`" unless resource
83
83
 
84
84
  @opts[:header_bindings] << {
85
- exchange: exchange,
86
- arguments: {
87
- info: resource
88
- }
85
+ exchange: exchange,
86
+ arguments: {
87
+ info: resource
88
+ }.merge(custom_headers)
89
89
  }
90
90
  end
91
91
  end
92
92
 
93
- def notices_subscribes(notices)
93
+ def notices_subscribes(notices, **custom_headers)
94
94
  notices.each do |_info|
95
- exchange, resource = _info.split(":")
96
- if resource.nil?
97
- resource = exchange
98
- exchange = "strum.info"
99
- end
100
- raise StandardError "binding format must be a `exchange:resource`" unless exchange && resource
95
+ exchange, resource = Strum::Esb::Functions.info_explain(_info)
96
+ raise StandardError "notice binding format must be a `exchange:resource`" unless resource
101
97
 
102
98
  @opts[:header_bindings] << {
103
- exchange: exchange,
104
- arguments: {
105
- notice: resource
106
- }
99
+ exchange: exchange,
100
+ arguments: {
101
+ notice: resource
102
+ }.merge(custom_headers)
107
103
  }
108
104
  end
109
105
  end
@@ -128,7 +124,11 @@ module QueuePatch
128
124
  header_bindings.each do |header_binding|
129
125
  exchange_name = header_binding[:exchange] || @opts[:exchange]
130
126
  exchange = @channel.exchange(exchange_name, @opts[:exchange_options].merge(type: :headers))
131
- queue.bind(exchange, arguments: { "x-match": :all }.merge(header_binding[:arguments]))
127
+ queue.bind(exchange,
128
+ arguments: {}.tap do |args|
129
+ args["x-match"] = :all
130
+ end
131
+ .merge(header_binding[:arguments]))
132
132
  end
133
133
 
134
134
  handler = handler_klass.new(@channel, queue, worker.opts)
@@ -140,6 +140,8 @@ module QueuePatch
140
140
  end
141
141
  end
142
142
 
143
- class Sneakers::Queue
144
- prepend QueuePatch
143
+ module Sneakers
144
+ class Queue
145
+ prepend QueuePatch
146
+ end
145
147
  end
data/strum-esb.gemspec CHANGED
@@ -1,4 +1,6 @@
1
- require_relative 'lib/strum/esb/version'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/strum/esb/version"
2
4
 
3
5
  Gem::Specification.new do |spec|
4
6
  spec.name = "strum-esb"
@@ -9,7 +11,7 @@ Gem::Specification.new do |spec|
9
11
  spec.summary = "Publish and subscribe rabbitMQ messages"
10
12
  spec.description = "Publish and subscribe rabbitMQ messages"
11
13
  spec.homepage = "https://code.qpard.com/strum/strum-esb"
12
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
13
15
 
14
16
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
15
17
 
@@ -19,7 +21,7 @@ Gem::Specification.new do |spec|
19
21
 
20
22
  # Specify which files should be added to the gem when it is released.
21
23
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
25
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
26
  end
25
27
  spec.bindir = "exe"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strum-esb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serhiy Nazarov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-16 00:00:00.000000000 Z
11
+ date: 2021-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -74,9 +74,9 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - ".gitignore"
77
+ - ".gitlab-ci.yml"
77
78
  - ".rspec"
78
79
  - ".rubocop.yml"
79
- - ".ruby-version"
80
80
  - ".travis.yml"
81
81
  - Gemfile
82
82
  - Gemfile.lock
@@ -87,6 +87,7 @@ files:
87
87
  - lib/strum/esb.rb
88
88
  - lib/strum/esb/action.rb
89
89
  - lib/strum/esb/event.rb
90
+ - lib/strum/esb/functions.rb
90
91
  - lib/strum/esb/handler.rb
91
92
  - lib/strum/esb/info.rb
92
93
  - lib/strum/esb/message.rb
@@ -109,14 +110,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
110
  requirements:
110
111
  - - ">="
111
112
  - !ruby/object:Gem::Version
112
- version: 2.3.0
113
+ version: 2.6.0
113
114
  required_rubygems_version: !ruby/object:Gem::Requirement
114
115
  requirements:
115
116
  - - ">="
116
117
  - !ruby/object:Gem::Version
117
118
  version: '0'
118
119
  requirements: []
119
- rubygems_version: 3.1.2
120
+ rubygems_version: 3.1.6
120
121
  signing_key:
121
122
  specification_version: 4
122
123
  summary: Publish and subscribe rabbitMQ messages
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.7.0