smart_message 0.0.2 → 0.0.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/CHANGELOG.md +31 -1
- data/Gemfile.lock +6 -1
- data/README.md +92 -14
- data/docs/README.md +1 -0
- data/docs/architecture.md +41 -8
- data/docs/dispatcher.md +52 -16
- data/docs/getting-started.md +64 -2
- data/docs/logging.md +452 -0
- data/docs/message_processing.md +423 -0
- data/docs/proc_handlers_summary.md +247 -0
- data/docs/transports.md +202 -8
- data/examples/.gitignore +2 -0
- data/examples/04_redis_smart_home_iot.rb +649 -0
- data/examples/05_proc_handlers.rb +181 -0
- data/examples/06_custom_logger_example.rb +620 -0
- data/examples/README.md +118 -3
- data/examples/smart_home_iot_dataflow.md +257 -0
- data/lib/smart_message/base.rb +94 -4
- data/lib/smart_message/dispatcher.rb +22 -6
- data/lib/smart_message/logger/default.rb +217 -0
- data/lib/smart_message/logger.rb +9 -1
- data/lib/smart_message/transport/redis_transport.rb +190 -0
- data/lib/smart_message/transport/registry.rb +1 -0
- data/lib/smart_message/transport.rb +1 -0
- data/lib/smart_message/version.rb +1 -1
- data/smart_message.gemspec +1 -0
- metadata +25 -1
@@ -0,0 +1,181 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# examples/05_proc_handlers.rb
|
3
|
+
#
|
4
|
+
# Proc and Block Handler Example
|
5
|
+
#
|
6
|
+
# This example demonstrates the new proc and block handler functionality
|
7
|
+
# in SmartMessage, showing different ways to subscribe to messages beyond
|
8
|
+
# the traditional self.process method.
|
9
|
+
|
10
|
+
require_relative '../lib/smart_message'
|
11
|
+
|
12
|
+
puts "=== SmartMessage Proc and Block Handler Example ==="
|
13
|
+
puts
|
14
|
+
|
15
|
+
# Define a simple notification message
|
16
|
+
class NotificationMessage < SmartMessage::Base
|
17
|
+
property :type # 'info', 'warning', 'error'
|
18
|
+
property :title
|
19
|
+
property :message
|
20
|
+
property :user_id
|
21
|
+
property :timestamp
|
22
|
+
|
23
|
+
config do
|
24
|
+
transport SmartMessage::Transport::StdoutTransport.new(loopback: true)
|
25
|
+
serializer SmartMessage::Serializer::JSON.new
|
26
|
+
end
|
27
|
+
|
28
|
+
# Default handler
|
29
|
+
def self.process(message_header, message_payload)
|
30
|
+
data = JSON.parse(message_payload)
|
31
|
+
icon = case data['type']
|
32
|
+
when 'info' then 'ℹ️'
|
33
|
+
when 'warning' then '⚠️'
|
34
|
+
when 'error' then '🚨'
|
35
|
+
else '📢'
|
36
|
+
end
|
37
|
+
puts "#{icon} [DEFAULT] #{data['title']}: #{data['message']}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
puts "🚀 Setting up different types of message handlers"
|
42
|
+
puts
|
43
|
+
|
44
|
+
# 1. Default handler (traditional way)
|
45
|
+
puts "1️⃣ Default handler subscription:"
|
46
|
+
default_id = NotificationMessage.subscribe
|
47
|
+
puts " Subscribed with ID: #{default_id}"
|
48
|
+
puts
|
49
|
+
|
50
|
+
# 2. Block handler
|
51
|
+
puts "2️⃣ Block handler subscription:"
|
52
|
+
block_id = NotificationMessage.subscribe do |header, payload|
|
53
|
+
data = JSON.parse(payload)
|
54
|
+
if data['type'] == 'error'
|
55
|
+
puts "🔥 [BLOCK] Critical error logged: #{data['title']}"
|
56
|
+
# Could send to error tracking service here
|
57
|
+
end
|
58
|
+
end
|
59
|
+
puts " Subscribed with ID: #{block_id}"
|
60
|
+
puts
|
61
|
+
|
62
|
+
# 3. Proc handler
|
63
|
+
puts "3️⃣ Proc handler subscription:"
|
64
|
+
audit_logger = proc do |header, payload|
|
65
|
+
data = JSON.parse(payload)
|
66
|
+
timestamp = header.published_at.strftime('%Y-%m-%d %H:%M:%S')
|
67
|
+
puts "📝 [AUDIT] #{timestamp} - User #{data['user_id']}: #{data['type'].upcase}"
|
68
|
+
end
|
69
|
+
|
70
|
+
proc_id = NotificationMessage.subscribe(audit_logger)
|
71
|
+
puts " Subscribed with ID: #{proc_id}"
|
72
|
+
puts
|
73
|
+
|
74
|
+
# 4. Lambda handler
|
75
|
+
puts "4️⃣ Lambda handler subscription:"
|
76
|
+
warning_filter = lambda do |header, payload|
|
77
|
+
data = JSON.parse(payload)
|
78
|
+
if data['type'] == 'warning'
|
79
|
+
puts "⚡ [LAMBDA] Warning for user #{data['user_id']}: #{data['message']}"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
lambda_id = NotificationMessage.subscribe(warning_filter)
|
84
|
+
puts " Subscribed with ID: #{lambda_id}"
|
85
|
+
puts
|
86
|
+
|
87
|
+
# 5. Method handler (traditional, but shown for comparison)
|
88
|
+
puts "5️⃣ Method handler subscription:"
|
89
|
+
class NotificationService
|
90
|
+
def self.handle_notifications(header, payload)
|
91
|
+
data = JSON.parse(payload)
|
92
|
+
puts "🏢 [SERVICE] Processing #{data['type']} notification for user #{data['user_id']}"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
method_id = NotificationMessage.subscribe("NotificationService.handle_notifications")
|
97
|
+
puts " Subscribed with ID: #{method_id}"
|
98
|
+
puts
|
99
|
+
|
100
|
+
puts "=" * 60
|
101
|
+
puts "📡 Publishing test notifications (watch the different handlers respond!)"
|
102
|
+
puts "=" * 60
|
103
|
+
puts
|
104
|
+
|
105
|
+
# Test the handlers with different notification types
|
106
|
+
notifications = [
|
107
|
+
{
|
108
|
+
type: 'info',
|
109
|
+
title: 'Welcome',
|
110
|
+
message: 'Welcome to the system!',
|
111
|
+
user_id: 'user123',
|
112
|
+
timestamp: Time.now.iso8601
|
113
|
+
},
|
114
|
+
{
|
115
|
+
type: 'warning',
|
116
|
+
title: 'Low Disk Space',
|
117
|
+
message: 'Your disk space is running low',
|
118
|
+
user_id: 'user456',
|
119
|
+
timestamp: Time.now.iso8601
|
120
|
+
},
|
121
|
+
{
|
122
|
+
type: 'error',
|
123
|
+
title: 'Database Connection Failed',
|
124
|
+
message: 'Unable to connect to the database',
|
125
|
+
user_id: 'system',
|
126
|
+
timestamp: Time.now.iso8601
|
127
|
+
}
|
128
|
+
]
|
129
|
+
|
130
|
+
notifications.each_with_index do |notification_data, index|
|
131
|
+
puts "\n📤 Publishing notification #{index + 1}: #{notification_data[:title]}"
|
132
|
+
|
133
|
+
notification = NotificationMessage.new(**notification_data)
|
134
|
+
notification.publish
|
135
|
+
|
136
|
+
# Give time for all handlers to process
|
137
|
+
sleep(0.5)
|
138
|
+
|
139
|
+
puts " ✅ All handlers processed the #{notification_data[:type]} notification"
|
140
|
+
end
|
141
|
+
|
142
|
+
puts "\n" + "=" * 60
|
143
|
+
puts "🔧 Demonstrating handler management"
|
144
|
+
puts "=" * 60
|
145
|
+
|
146
|
+
# Show how to unsubscribe handlers
|
147
|
+
puts "\n🗑️ Unsubscribing the block handler..."
|
148
|
+
NotificationMessage.unsubscribe(block_id)
|
149
|
+
puts " Block handler removed"
|
150
|
+
|
151
|
+
puts "\n📤 Publishing another error notification (block handler won't respond):"
|
152
|
+
error_notification = NotificationMessage.new(
|
153
|
+
type: 'error',
|
154
|
+
title: 'Another Error',
|
155
|
+
message: 'This error won\'t trigger the block handler',
|
156
|
+
user_id: 'test_user',
|
157
|
+
timestamp: Time.now.iso8601
|
158
|
+
)
|
159
|
+
|
160
|
+
error_notification.publish
|
161
|
+
sleep(0.5)
|
162
|
+
|
163
|
+
puts "\n" + "=" * 60
|
164
|
+
puts "✨ Example completed!"
|
165
|
+
puts "=" * 60
|
166
|
+
|
167
|
+
puts "\nThis example demonstrated:"
|
168
|
+
puts "• ✅ Default self.process method (traditional)"
|
169
|
+
puts "• ✅ Block handlers with subscribe { |h,p| ... } (NEW!)"
|
170
|
+
puts "• ✅ Proc handlers with subscribe(proc { ... }) (NEW!)"
|
171
|
+
puts "• ✅ Lambda handlers with subscribe(lambda { ... }) (NEW!)"
|
172
|
+
puts "• ✅ Method handlers with subscribe('Class.method') (traditional)"
|
173
|
+
puts "• ✅ Handler unsubscription and management"
|
174
|
+
puts "\nEach handler type has its own use cases:"
|
175
|
+
puts "• 🎯 Default: Simple built-in processing"
|
176
|
+
puts "• 🔧 Blocks: Inline logic for specific subscriptions"
|
177
|
+
puts "• 🔄 Procs: Reusable handlers across message types"
|
178
|
+
puts "• ⚡ Lambdas: Strict argument checking and functional style"
|
179
|
+
puts "• 🏢 Methods: Organized, testable business logic"
|
180
|
+
|
181
|
+
puts "\n🎉 Choose the handler type that best fits your needs!"
|