postcard_rb 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c0c9ef85ecce7e4def474a858b5986f57ad37eacbcff630ceb856a8d7abbd539
4
+ data.tar.gz: 7c1175345e90501559519d30880b15b8359f5f5d2d3b5235a0f1a01be2b433fd
5
+ SHA512:
6
+ metadata.gz: 8724318c2adfe1df3b1d6142cb523fac544e247007812c94a1fbee2d26d2aa2444b5de3d13b909b8f02b4f3e02df63010de7c840308dc850f61656ee5bc84e91
7
+ data.tar.gz: a3c8c987e09ae388c740f0638422298b59c3e8fa42d9cdbb294c2b5e02268b35bb3a4f9959c1a779aadba93a5645034406bf1c18f8cbd9cbcdae97831d7a062d
@@ -0,0 +1,17 @@
1
+ class Routing
2
+ @@Wide = "Wide"
3
+ @@Explicit = "Explicit"
4
+ @@PatternMatching = "PatternMatching"
5
+
6
+ def self.Wide
7
+ @@Wide
8
+ end
9
+
10
+ def self.Explicit
11
+ @@Explicit
12
+ end
13
+
14
+ def self.PatternMatching
15
+ @@PatternMatching
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ require 'interface'
2
+
3
+ IDispatcher = interface {
4
+ required_methods :connect, :createTopic
5
+ }
@@ -0,0 +1,5 @@
1
+ require 'interface'
2
+
3
+ IRoom = interface {
4
+ required_methods :subscribe
5
+ }
@@ -0,0 +1,5 @@
1
+ require 'interface'
2
+
3
+ ITopic = interface {
4
+ required_methods :createRoom, :publish
5
+ }
@@ -0,0 +1,57 @@
1
+ require 'bunny'
2
+
3
+ require_relative '../IDispatcher'
4
+ require_relative '../../errors/DispatcherConnectionRefused'
5
+ require_relative './routings/Wide/WideRabbitMQTopic'
6
+ require_relative './routings/Explicit/ExplicitRabbitMQTopic'
7
+ require_relative './routings/PatternMatching/PatternMatchingRabbitMQTopic'
8
+
9
+ class RabbitMQDispatcher
10
+ def initialize host:
11
+ @connection = Bunny.new(host: host)
12
+ @channel = nil
13
+ @topics = []
14
+ end
15
+
16
+ def createChannel
17
+ @channel = @connection.create_channel
18
+ @channel.prefetch(1)
19
+ end
20
+
21
+ def connect connectionInterval:, connectionRetries:
22
+ begin
23
+ @connection.start
24
+
25
+ createChannel()
26
+ rescue Bunny::TCPConnectionFailedForAllHosts
27
+ sleep connectionInterval
28
+ connectionRetries -= 1
29
+
30
+ raise DispatcherConnectionRefused if connectionRetries == 0
31
+
32
+ connect(
33
+ connectionInterval: connectionInterval,
34
+ connectionRetries: connectionRetries
35
+ )
36
+ end
37
+ end
38
+
39
+ def createTopic name:, routing:
40
+ topic = nil
41
+
42
+ case routing
43
+ when Routing.Wide
44
+ topic = WideRabbitMQTopic.new(name: name, channel: @channel)
45
+ when Routing.Explicit
46
+ topic = ExplicitRabbitMQTopic.new(name: name, channel: @channel)
47
+ when Routing.PatternMatching
48
+ topic = PatternMatchingRabbitMQTopic.new(name: name, channel: @channel)
49
+ end
50
+
51
+ @topics.push(topic)
52
+
53
+ return topic
54
+ end
55
+
56
+ implements IDispatcher
57
+ end
@@ -0,0 +1,27 @@
1
+ require_relative '../../IRoom'
2
+
3
+ class BaseRabbitMQRoom
4
+ def initialize channel:, queue:, exclusive: false
5
+ @channel = channel
6
+ @queue = queue
7
+ @exclusive = exclusive
8
+ end
9
+
10
+ def subscribe block: true
11
+ begin
12
+ manual_ack = false
13
+
14
+ manual_ack = true if @exclusive
15
+
16
+ @queue.subscribe(manual_ack: manual_ack, block: block) do |delivery_info, properties, payload|
17
+ yield delivery_info, properties, payload
18
+
19
+ @channel.ack(delivery_info.delivery_tag) if @exclusive
20
+ end
21
+ rescue Interrupt => _
22
+
23
+ end
24
+ end
25
+
26
+ implements IRoom
27
+ end
@@ -0,0 +1,18 @@
1
+ require_relative '../../ITopic'
2
+
3
+ class BaseRabbitMQTopic
4
+ def initialize
5
+ @rooms = []
6
+ end
7
+
8
+ def addRoom room:
9
+ @rooms.append(room)
10
+ end
11
+
12
+ def createRoom; end
13
+ def publish; end
14
+
15
+ implements ITopic
16
+
17
+ private :addRoom
18
+ end
@@ -0,0 +1,13 @@
1
+ require_relative '../BaseRabbitMQRoom'
2
+
3
+ class ExplicitRabbitMQRoom < BaseRabbitMQRoom
4
+ def initialize name:, exclusive: false, channel:, exchange:
5
+ queueName = ""
6
+
7
+ queueName = name if exclusive
8
+
9
+ queue = channel.queue(queueName).bind(exchange, :routing_key => name, :durable => false)
10
+
11
+ super(channel: channel, queue: queue, exclusive: exclusive)
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ require_relative '../BaseRabbitMQTopic'
2
+ require_relative './ExplicitRabbitMQRoom'
3
+
4
+ class ExplicitRabbitMQTopic < BaseRabbitMQTopic
5
+ def initialize name:, channel:
6
+ super()
7
+
8
+ @channel = channel
9
+ @exchange = @channel.direct(name, :durable => false)
10
+ end
11
+
12
+ def createRoom name:, exclusive: false
13
+ room = ExplicitRabbitMQRoom.new(
14
+ name: name,
15
+ exclusive: exclusive,
16
+ channel: @channel,
17
+ exchange: @exchange
18
+ )
19
+
20
+ addRoom(room: room)
21
+
22
+ return room
23
+ end
24
+
25
+ def publish room:, payload:, correlationId: nil, replyTo: nil
26
+ @exchange.publish(
27
+ payload,
28
+ :routing_key => room,
29
+ :correlation_id => correlationId,
30
+ :reply_to => replyTo,
31
+ :persistent => false
32
+ )
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ require_relative '../BaseRabbitMQRoom'
2
+
3
+ class PatternMatchingRabbitMQRoom < BaseRabbitMQRoom
4
+ def initialize name:, channel:, exchange:
5
+ queue = channel.queue("").bind(exchange, :routing_key => name, :durable => false)
6
+
7
+ super(queue: queue)
8
+ end
9
+ end
@@ -0,0 +1,33 @@
1
+ require_relative '../BaseRabbitMQTopic'
2
+ require_relative './PatternMatchingRabbitMQRoom'
3
+
4
+ class PatternMatchingRabbitMQTopic < BaseRabbitMQTopic
5
+ def initialize name:, channel:
6
+ super()
7
+
8
+ @channel = channel
9
+ @exchange = @channel.topic(name, :durable => false)
10
+ end
11
+
12
+ def createRoom name:
13
+ room = PatternMatchingRabbitMQRoom.new(
14
+ name: name,
15
+ channel: @channel,
16
+ exchange: @exchange
17
+ )
18
+
19
+ addRoom(room: room)
20
+
21
+ return room
22
+ end
23
+
24
+ def publish room:, payload:, correlationId: nil, replyTo: nil
25
+ @exchange.publish(
26
+ payload,
27
+ :routing_key => room,
28
+ :correlation_id => correlationId,
29
+ :reply_to => replyTo,
30
+ :persistent => false
31
+ )
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ require_relative '../BaseRabbitMQRoom'
2
+
3
+ class WideRabbitMQRoom < BaseRabbitMQRoom
4
+ def initialize name:, channel:, exchange:
5
+ queue = channel.queue("").bind(exchange, :durable => false)
6
+
7
+ super(queue: queue)
8
+ end
9
+ end
@@ -0,0 +1,32 @@
1
+ require_relative '../BaseRabbitMQTopic'
2
+ require_relative './WideRabbitMQRoom'
3
+
4
+ class WideRabbitMQTopic < BaseRabbitMQTopic
5
+ def initialize name:, channel:
6
+ super()
7
+
8
+ @channel = channel
9
+ @exchange = @channel.fanout(name, :durable => false)
10
+ end
11
+
12
+ def createRoom name:
13
+ room = WideRabbitMQRoom.new(
14
+ name: name,
15
+ channel: @channel,
16
+ exchange: @exchange
17
+ )
18
+
19
+ addRoom(room: room)
20
+
21
+ return room
22
+ end
23
+
24
+ def publish payload:, correlationId: nil, replyTo: nil
25
+ @exchange.publish(
26
+ payload,
27
+ :correlation_id => correlationId,
28
+ :reply_to => replyTo,
29
+ :persistent => false
30
+ )
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ class DispatcherConnectionRefused < RuntimeError
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ class PostcardConnectionRefused < RuntimeError
2
+
3
+ end
@@ -0,0 +1,7 @@
1
+ require "securerandom"
2
+
3
+ class CorrelationID
4
+ def self.create
5
+ SecureRandom.random_number(36**12).to_s(36).rjust(16, "0")
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'postcard_rb/errors/PostcardConnectionRefused'
2
+
3
+ class PostcardRB
4
+ def initialize dispatcher:
5
+ @CONNECTION_RETRIES = 10
6
+ @CONNECTION_INTERVAL = 2
7
+
8
+ @dispatcher = dispatcher
9
+ end
10
+
11
+ def connect
12
+ begin
13
+ @dispatcher.connect(
14
+ connectionInterval: @CONNECTION_INTERVAL,
15
+ connectionRetries: @CONNECTION_RETRIES
16
+ )
17
+ rescue DispatcherConnectionRefused
18
+ raise PostcardConnectionRefused
19
+ end
20
+ end
21
+
22
+ def createTopic name:, routing:
23
+ topic = @dispatcher.createTopic(name: name, routing: routing)
24
+
25
+ return topic
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postcard_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Simone Adelchino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An abstraction layer over message brokers
14
+ email: seconddesire@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/postcard_rb.rb
20
+ - lib/postcard_rb/Routing.rb
21
+ - lib/postcard_rb/dispatchers/IDispatcher.rb
22
+ - lib/postcard_rb/dispatchers/IRoom.rb
23
+ - lib/postcard_rb/dispatchers/ITopic.rb
24
+ - lib/postcard_rb/dispatchers/RabbitMQ/RabbitMQDispatcher.rb
25
+ - lib/postcard_rb/dispatchers/RabbitMQ/routings/BaseRabbitMQRoom.rb
26
+ - lib/postcard_rb/dispatchers/RabbitMQ/routings/BaseRabbitMQTopic.rb
27
+ - lib/postcard_rb/dispatchers/RabbitMQ/routings/Explicit/ExplicitRabbitMQRoom.rb
28
+ - lib/postcard_rb/dispatchers/RabbitMQ/routings/Explicit/ExplicitRabbitMQTopic.rb
29
+ - lib/postcard_rb/dispatchers/RabbitMQ/routings/PatternMatching/PatternMatchingRabbitMQRoom.rb
30
+ - lib/postcard_rb/dispatchers/RabbitMQ/routings/PatternMatching/PatternMatchingRabbitMQTopic.rb
31
+ - lib/postcard_rb/dispatchers/RabbitMQ/routings/Wide/WideRabbitMQRoom.rb
32
+ - lib/postcard_rb/dispatchers/RabbitMQ/routings/Wide/WideRabbitMQTopic.rb
33
+ - lib/postcard_rb/errors/DispatcherConnectionRefused.rb
34
+ - lib/postcard_rb/errors/PostcardConnectionRefused.rb
35
+ - lib/postcard_rb/lib/CorrelationID.rb
36
+ homepage: http://rubygems.org/gems/postcard
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.7.7
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: PostcardRB
60
+ test_files: []