rmessage 0.1.1 → 0.1.4
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 +4 -4
- data/lib/rmessage/message.rb +10 -9
- data/lib/rmessage/subscriber.rb +9 -9
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c69292182b658aa175401a67815c59696c47a43f4b27b7987f27fd5f7d509e64
|
4
|
+
data.tar.gz: a89eb21e1f6759e7d283184d18f0cf08c9b15cad06abe0c96b98fd9fc1d3879a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d6d75dce9f23ca9b114e44b10c45336e469b9afea34c3187a271ce561ac2d16fd58314f8540fa24be731decc93a3099f82a0c29fb5662a7b9dd04a1d2036e16
|
7
|
+
data.tar.gz: 2b08ff7367d060e9b1878ac2296e6c666089b236d15ea66bf92e61a4f505ac5a785965f239fed313c84efc1ffdc165dbeeded1864ac45bb28aaf7ae261736146
|
data/lib/rmessage/message.rb
CHANGED
@@ -2,9 +2,9 @@ module RMessage
|
|
2
2
|
# A Message encapsulates a JSON payload that is sent to or received from Redis.
|
3
3
|
class Message
|
4
4
|
|
5
|
-
# @!attribute [r]
|
5
|
+
# @!attribute [r] type
|
6
6
|
# @return [Symbol, String] the label.
|
7
|
-
attr :
|
7
|
+
attr :type
|
8
8
|
|
9
9
|
# @!attribute [r] signature
|
10
10
|
# @return [Integer, String] the signature.
|
@@ -19,34 +19,35 @@ module RMessage
|
|
19
19
|
# * label [Symbol, String] the Message label
|
20
20
|
# * signature [Integer, String] the Message signature
|
21
21
|
# * payload [Hash, String] the Message data
|
22
|
-
|
23
|
-
|
24
|
-
@label = opts[:label] || :UNTITLED
|
22
|
+
def initialize(opts = {})
|
23
|
+
@type = opts[:type] || :UNTITLED
|
25
24
|
@signature = opts[:signature] || Druuid.gen
|
26
|
-
@payload =
|
25
|
+
@payload = opts[:payload] || opts
|
27
26
|
end
|
28
27
|
|
29
28
|
# Read and parse data. Defaults to the Message#payload.
|
30
29
|
# @param data [Hash, String] the data.
|
30
|
+
# @return [RMessage::Message]
|
31
31
|
def read(data = @payload)
|
32
32
|
case data
|
33
33
|
when Hash
|
34
|
-
@
|
34
|
+
@type ||= data[:type]
|
35
35
|
@signature ||= data[:signature]
|
36
36
|
@payload ||= data[:payload]
|
37
37
|
when String
|
38
38
|
parsed = Oj.load(data)
|
39
|
-
@
|
39
|
+
@type ||= parsed['type']
|
40
40
|
@signature ||= parsed['signature']
|
41
41
|
@payload ||= parsed['payload']
|
42
42
|
else raise TypeError, 'Invalid type for Message#payload'
|
43
43
|
end
|
44
|
+
self
|
44
45
|
end
|
45
46
|
|
46
47
|
# Build a JSON string using data in this message
|
47
48
|
# @param data [Hash, String] the payload.
|
48
49
|
def build(data = @payload)
|
49
|
-
Oj.dump({ '
|
50
|
+
Oj.dump({ 'type' => @type, 'signature' => @signature, 'payload' => data })
|
50
51
|
end
|
51
52
|
end
|
52
53
|
end
|
data/lib/rmessage/subscriber.rb
CHANGED
@@ -15,14 +15,16 @@ module RMessage::Subscriber
|
|
15
15
|
# @param auto_parse [Boolean] should messages be parsed automatically?
|
16
16
|
# @param connection [Redis] optional argument to provide the redis connection.
|
17
17
|
# @param connection_index [Integer] the index of the redis connection in {RMessage::CONNECTION_POOL}
|
18
|
-
def setup_subscription(channel: @channel, auto_parse: false, connection: nil, connection_index: 0, &_)
|
18
|
+
def setup_subscription(channel: @channel, auto_parse: false, logger: nil, connection: nil, connection_index: 0, &_)
|
19
19
|
redis_connection = connection || RMessage::CONNECTION_POOL[connection_index]
|
20
20
|
redis_connection.subscribe(channel) do |event|
|
21
|
-
event.subscribe
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
event.subscribe do
|
22
|
+
logger&.info("Subscribed to channel [channel: #{channel}]")
|
23
|
+
end
|
24
|
+
|
25
|
+
event.message do |_, message|
|
26
|
+
auto_parse ? yield(parse_message(message)) : yield(message)
|
27
|
+
logger&.info("Received Message: #{message}")
|
26
28
|
end
|
27
29
|
end
|
28
30
|
end
|
@@ -30,9 +32,7 @@ module RMessage::Subscriber
|
|
30
32
|
private
|
31
33
|
|
32
34
|
def parse_message(message_data)
|
33
|
-
|
34
|
-
msg.read
|
35
|
-
@events[msg.label].new(msg)
|
35
|
+
RMessage::Message.new.read(message_data)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rmessage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick W.
|
@@ -109,6 +109,7 @@ post_install_message:
|
|
109
109
|
rdoc_options: []
|
110
110
|
require_paths:
|
111
111
|
- lib
|
112
|
+
- lib/rmessage
|
112
113
|
required_ruby_version: !ruby/object:Gem::Requirement
|
113
114
|
requirements:
|
114
115
|
- - ">="
|