Skip to content

Commit d241706

Browse files
author
Caique Hitoshi Mitsuoka
committed
Update ruby tutorial five
1 parent 30f0e18 commit d241706

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

ruby/emit_log_topic.rb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
#!/usr/bin/env ruby
2-
# encoding: utf-8
2+
require 'bunny'
33

4-
require "bunny"
4+
connection = Bunny.new(automatically_recover: false)
5+
connection.start
56

6-
conn = Bunny.new(:automatically_recover => false)
7-
conn.start
7+
channel = connection.create_channel
8+
exchange = channel.topic('topic_logs')
9+
severity = ARGV.shift || 'anonymous.info'
10+
message = ARGV.empty? ? 'Hello World!' : ARGV.join(' ')
811

9-
ch = conn.create_channel
10-
x = ch.topic("topic_logs")
11-
severity = ARGV.shift || "anonymous.info"
12-
msg = ARGV.empty? ? "Hello World!" : ARGV.join(" ")
12+
exchange.publish(message, routing_key: severity)
13+
puts " [x] Sent #{severity}:#{message}"
1314

14-
x.publish(msg, :routing_key => severity)
15-
puts " [x] Sent #{severity}:#{msg}"
16-
17-
conn.close
15+
connection.close

ruby/receive_logs_topic.rb

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
#!/usr/bin/env ruby
2-
# encoding: utf-8
2+
require 'bunny'
33

4-
require "bunny"
4+
abort "Usage: #{$PROGRAM_NAME} [binding key]" if ARGV.empty?
55

6-
if ARGV.empty?
7-
abort "Usage: #{$0} [binding key]"
8-
end
9-
10-
conn = Bunny.new(:automatically_recover => false)
11-
conn.start
6+
connection = Bunny.new(automatically_recover: false)
7+
connection.start
128

13-
ch = conn.create_channel
14-
x = ch.topic("topic_logs")
15-
q = ch.queue("", :exclusive => true)
9+
channel = connection.create_channel
10+
exchange = channel.topic('topic_logs')
11+
queue = channel.queue('', exclusive: true)
1612

1713
ARGV.each do |severity|
18-
q.bind(x, :routing_key => severity)
14+
queue.bind(exchange, routing_key: severity)
1915
end
2016

21-
puts " [*] Waiting for logs. To exit press CTRL+C"
17+
puts ' [*] Waiting for logs. To exit press CTRL+C'
2218

2319
begin
24-
q.subscribe(:block => true) do |delivery_info, properties, body|
20+
queue.subscribe(block: true) do |delivery_info, _properties, body|
2521
puts " [x] #{delivery_info.routing_key}:#{body}"
2622
end
2723
rescue Interrupt => _
28-
ch.close
29-
conn.close
24+
channel.close
25+
connection.close
3026

3127
exit(0)
3228
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy