rcom 0.0.2 → 0.0.3
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/README.md +5 -2
- data/lib/rcom/node.rb +6 -0
- data/lib/rcom/rpc.rb +18 -1
- data/lib/rcom/task.rb +10 -3
- data/lib/rcom/topic.rb +10 -3
- data/lib/rcom/version.rb +1 -1
- data/test/bin/topic_publisher.rb +1 -1
- data/test/bin/topic_subscriber.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4a59231e2507f9f5010d142bfeb28efeba2e8e6
|
4
|
+
data.tar.gz: a141a111f2e29d337ddcad3a60626885e88430bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7c512e707d70fb3520bcfab86146053b14949d9d28fb3ed3a73268d1ed5df04c337f7a4766d38d5d4b7480d7e68535ac31870868fd5bc42dff2548fc5288d28
|
7
|
+
data.tar.gz: cf8fe056685e4024d07f5e0829c9f5c911303e75ce5b1f7254796c3a755660041fd4971a60ea353e6c3a7c5b865a0e566d15e0fedeb70831133c091323927ed3
|
data/README.md
CHANGED
@@ -24,9 +24,12 @@ Rcom supports the request-response, publish-subscribe and task queues patterns f
|
|
24
24
|
|
25
25
|
A node represents a Redis connection to a server address specified with an ENV variable.
|
26
26
|
|
27
|
+
```sh
|
28
|
+
#.env
|
29
|
+
LOCAL=redis://localhost
|
30
|
+
```
|
31
|
+
|
27
32
|
```ruby
|
28
|
-
# Specify this in your .env file.
|
29
|
-
ENV['local'] = 'redis://localhost'
|
30
33
|
node = Rcom::Node.new('local').connect
|
31
34
|
```
|
32
35
|
|
data/lib/rcom/node.rb
CHANGED
@@ -1,12 +1,18 @@
|
|
1
1
|
module Rcom
|
2
|
+
# A node represents a Redis server and has an uri.
|
2
3
|
class Node
|
4
|
+
# @return [String]
|
3
5
|
attr_reader :uri
|
4
6
|
|
7
|
+
# @param uri [String] Example: 'local'
|
8
|
+
# @raise [ArgumentError] uri is not in .env.
|
5
9
|
def initialize(uri)
|
6
10
|
raise ArgumentError unless ENV[uri.upcase]
|
7
11
|
@uri = ENV[uri.upcase]
|
8
12
|
end
|
9
13
|
|
14
|
+
# Connects to Redis.
|
15
|
+
# @return [Redis]
|
10
16
|
def connect
|
11
17
|
Redis.new(url: uri)
|
12
18
|
end
|
data/lib/rcom/rpc.rb
CHANGED
@@ -1,12 +1,24 @@
|
|
1
1
|
module Rcom
|
2
|
+
# Rpc implements the request/response pattern.
|
2
3
|
class Rpc
|
3
|
-
|
4
|
+
# @return [Rcom::Node]
|
5
|
+
attr_reader :node
|
6
|
+
# @return [String]
|
7
|
+
attr_reader :service
|
4
8
|
|
9
|
+
# @param params [Hash]
|
10
|
+
# @option params :node [Rcom::Node] Example: Rcom::Node.new('local').connect
|
11
|
+
# @option params :service [String] Example: 'auth'
|
5
12
|
def initialize(params)
|
6
13
|
@node = params[:node]
|
7
14
|
@service = params[:service]
|
8
15
|
end
|
9
16
|
|
17
|
+
# @param params [Hash]
|
18
|
+
# @option params :route [String] Example: 'users.key'
|
19
|
+
# @option params :args Example: 1
|
20
|
+
# @return [reply, nil] Returns the reply or nil if the
|
21
|
+
# request can't be processed.
|
10
22
|
def request(params)
|
11
23
|
begin
|
12
24
|
request = {
|
@@ -27,6 +39,8 @@ module Rcom
|
|
27
39
|
end
|
28
40
|
end
|
29
41
|
|
42
|
+
# Subscribe to the service and listen to requests.
|
43
|
+
# @yieldparam router [Rcom::Router] A router to match the request.
|
30
44
|
def subscribe
|
31
45
|
begin
|
32
46
|
loop do
|
@@ -47,13 +61,16 @@ module Rcom
|
|
47
61
|
end
|
48
62
|
end
|
49
63
|
|
64
|
+
# A router can match on a particular route.
|
50
65
|
class Router
|
51
66
|
attr_accessor :message, :reply
|
52
67
|
|
68
|
+
# @param message [Hash] The message received with the request.
|
53
69
|
def initialize(message)
|
54
70
|
@message = message
|
55
71
|
end
|
56
72
|
|
73
|
+
# @yieldparam args
|
57
74
|
def on(route)
|
58
75
|
return nil unless message[:route] == route
|
59
76
|
yield message[:args]
|
data/lib/rcom/task.rb
CHANGED
@@ -1,12 +1,18 @@
|
|
1
1
|
module Rcom
|
2
|
+
# Implements tasks and queues.
|
2
3
|
class Task
|
3
4
|
attr_reader :node, :queue
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
# @param params [Hash]
|
7
|
+
# @option params :node [Rcom::Node]
|
8
|
+
# @option params :queue [String] Example: 'messages'
|
9
|
+
def initialize(params)
|
10
|
+
@node = params[:node]
|
11
|
+
@queue = params[:queue]
|
8
12
|
end
|
9
13
|
|
14
|
+
# @param message [Hash]
|
15
|
+
# @return [true, nil] True if the message can be queued, otherwise nil.
|
10
16
|
def publish(message)
|
11
17
|
begin
|
12
18
|
node.lpush(queue, message.to_msgpack)
|
@@ -16,6 +22,7 @@ module Rcom
|
|
16
22
|
end
|
17
23
|
end
|
18
24
|
|
25
|
+
# @yieldparam message [Hash] the message received.
|
19
26
|
def subscribe
|
20
27
|
begin
|
21
28
|
loop do
|
data/lib/rcom/topic.rb
CHANGED
@@ -1,12 +1,18 @@
|
|
1
1
|
module Rcom
|
2
|
+
# Implements pub/sub over topics.
|
2
3
|
class Topic
|
3
4
|
attr_reader :node, :key
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
# @param params [Hash]
|
7
|
+
# @option params :node [Rcom::Node]
|
8
|
+
# @option params :key [String] Example: 'services'
|
9
|
+
def initialize(params)
|
10
|
+
@node = params[:node]
|
11
|
+
@key = params[:key]
|
8
12
|
end
|
9
13
|
|
14
|
+
# @param message [Hash]
|
15
|
+
# @return [true, nil] true if it can be published, otherwise nil.
|
10
16
|
def publish(message)
|
11
17
|
begin
|
12
18
|
node.publish(key, message.to_msgpack)
|
@@ -16,6 +22,7 @@ module Rcom
|
|
16
22
|
end
|
17
23
|
end
|
18
24
|
|
25
|
+
# @yieldparam message [Hash] the message received.
|
19
26
|
def subscribe
|
20
27
|
begin
|
21
28
|
node.subscribe(key) do |on|
|
data/lib/rcom/version.rb
CHANGED
data/test/bin/topic_publisher.rb
CHANGED