Skip to content

Commit 5b33adc

Browse files
Merge pull request #156 from CaiqueMitsuoka/update-ruby-tutorials
Update ruby tutorials
2 parents a2da52f + 1f9dbc5 commit 5b33adc

12 files changed

+176
-195
lines changed

ruby/emit_log.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
#!/usr/bin/env ruby
2-
# encoding: utf-8
32

4-
require "bunny"
3+
require 'bunny'
54

6-
conn = Bunny.new(:automatically_recover => false)
7-
conn.start
5+
connection = Bunny.new
6+
connection.start
87

9-
ch = conn.create_channel
10-
x = ch.fanout("logs")
8+
channel = connection.create_channel
9+
exchange = channel.fanout('logs')
1110

12-
msg = ARGV.empty? ? "Hello World!" : ARGV.join(" ")
11+
message = ARGV.empty? ? 'Hello World!' : ARGV.join(' ')
1312

14-
x.publish(msg)
15-
puts " [x] Sent #{msg}"
13+
exchange.publish(message)
14+
puts " [x] Sent #{message}"
1615

17-
conn.close
16+
connection.close

ruby/emit_log_direct.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.direct('direct_logs')
9+
severity = ARGV.shift || 'info'
10+
message = ARGV.empty? ? 'Hello World!' : ARGV.join(' ')
811

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

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

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/new_task.rb

Lines changed: 9 additions & 11 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+
queue = channel.queue('task_queue', durable: true)
89

9-
ch = conn.create_channel
10-
q = ch.queue("task_queue", :durable => true)
10+
message = ARGV.empty? ? 'Hello World!' : ARGV.join(' ')
1111

12-
msg = ARGV.empty? ? "Hello World!" : ARGV.join(" ")
12+
queue.publish(message, persistent: true)
13+
puts " [x] Sent #{message}"
1314

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

ruby/receive.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
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
8-
9-
ch = conn.create_channel
10-
q = ch.queue("hello")
7+
channel = connection.create_channel
8+
queue = channel.queue('hello')
119

1210
begin
13-
puts " [*] Waiting for messages. To exit press CTRL+C"
14-
q.subscribe(:block => true) do |delivery_info, properties, body|
11+
puts ' [*] Waiting for messages. To exit press CTRL+C'
12+
queue.subscribe(block: true) do |_delivery_info, _properties, body|
1513
puts " [x] Received #{body}"
1614
end
1715
rescue Interrupt => _
18-
conn.close
16+
connection.close
1917

2018
exit(0)
2119
end

ruby/receive_logs.rb

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
#!/usr/bin/env ruby
2-
# encoding: utf-8
32

4-
require "bunny"
3+
require 'bunny'
54

6-
conn = Bunny.new(:automatically_recover => false)
7-
conn.start
5+
connection = Bunny.new
6+
connection.start
87

9-
ch = conn.create_channel
10-
x = ch.fanout("logs")
11-
q = ch.queue("", :exclusive => true)
8+
channel = connection.create_channel
9+
exchange = channel.fanout('logs')
10+
queue = channel.queue('', exclusive: true)
1211

13-
q.bind(x)
12+
queue.bind(exchange)
1413

15-
puts " [*] Waiting for logs. To exit press CTRL+C"
14+
puts ' [*] Waiting for logs. To exit press CTRL+C'
1615

1716
begin
18-
q.subscribe(:block => true) do |delivery_info, properties, body|
17+
queue.subscribe(block: true) do |_delivery_info, _properties, body|
1918
puts " [x] #{body}"
2019
end
2120
rescue Interrupt => _
22-
ch.close
23-
conn.close
24-
25-
exit(0)
21+
channel.close
22+
connection.close
2623
end

ruby/receive_logs_direct.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} [info] [warning] [error]" if ARGV.empty?
55

6-
if ARGV.empty?
7-
abort "Usage: #{$0} [info] [warning] [error]"
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.direct("direct_logs")
15-
q = ch.queue("", :exclusive => true)
9+
channel = connection.create_channel
10+
exchange = channel.direct('direct_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

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

ruby/rpc_client.rb

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,70 @@
11
#!/usr/bin/env ruby
2-
# encoding: utf-8
2+
require 'bunny'
3+
require 'thread'
34

4-
require "bunny"
5-
require "thread"
5+
class FibonacciClient
6+
attr_accessor :call_id, :response, :lock, :condition, :connection,
7+
:channel, :server_queue_name, :reply_queue, :exchange
68

7-
conn = Bunny.new(:automatically_recover => false)
8-
conn.start
9+
def initialize(server_queue_name)
10+
@connection = Bunny.new(automatically_recover: false)
11+
@connection.start
912

10-
ch = conn.create_channel
13+
@channel = connection.create_channel
14+
@exchange = channel.default_exchange
15+
@server_queue_name = server_queue_name
1116

17+
setup_reply_queue
18+
end
1219

13-
class FibonacciClient
14-
attr_reader :reply_queue
15-
attr_accessor :response, :call_id
16-
attr_reader :lock, :condition
20+
def call(n)
21+
@call_id = generate_uuid
22+
23+
exchange.publish(n.to_s,
24+
routing_key: server_queue_name,
25+
correlation_id: call_id,
26+
reply_to: reply_queue.name)
1727

18-
def initialize(ch, server_queue)
19-
@ch = ch
20-
@x = ch.default_exchange
28+
# wait for the signal to continue the execution
29+
lock.synchronize { condition.wait(lock) }
2130

22-
@server_queue = server_queue
23-
@reply_queue = ch.queue("", :exclusive => true)
31+
response
32+
end
33+
34+
def stop
35+
channel.close
36+
connection.close
37+
end
2438

39+
private
2540

26-
@lock = Mutex.new
41+
def setup_reply_queue
42+
@lock = Mutex.new
2743
@condition = ConditionVariable.new
28-
that = self
44+
that = self
45+
@reply_queue = channel.queue('', exclusive: true)
2946

30-
@reply_queue.subscribe do |delivery_info, properties, payload|
47+
reply_queue.subscribe do |_delivery_info, properties, payload|
3148
if properties[:correlation_id] == that.call_id
3249
that.response = payload.to_i
33-
that.lock.synchronize{that.condition.signal}
50+
51+
# sends the signal to continue the execution of #call
52+
that.lock.synchronize { that.condition.signal }
3453
end
3554
end
3655
end
3756

38-
def call(n)
39-
self.call_id = self.generate_uuid
40-
41-
@x.publish(n.to_s,
42-
:routing_key => @server_queue,
43-
:correlation_id => call_id,
44-
:reply_to => @reply_queue.name)
45-
46-
lock.synchronize{condition.wait(lock)}
47-
response
48-
end
49-
50-
protected
51-
5257
def generate_uuid
53-
# very naive but good enough for code
54-
# examples
58+
# very naive but good enough for code examples
5559
"#{rand}#{rand}#{rand}"
5660
end
5761
end
5862

63+
client = FibonacciClient.new('rpc_queue')
5964

60-
client = FibonacciClient.new(ch, "rpc_queue")
61-
puts " [x] Requesting fib(30)"
65+
puts ' [x] Requesting fib(30)'
6266
response = client.call(30)
67+
6368
puts " [.] Got #{response}"
6469

65-
ch.close
66-
conn.close
70+
client.stop

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