Created
January 25, 2012 01:53
-
-
Save scizo/1674124 to your computer and use it in GitHub Desktop.
AMQP Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem 'bunny' | |
gem 'json' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bunny' | |
require 'json' | |
require 'erb' | |
class Madlib | |
MADLIBS = [ | |
ERB.new( | |
"Be kind to your <%= @msg['noun_1'] %>-footed <%= @msg['noun_plural'] %> | |
For a duck may be somebody`s <%= @msg['noun_2'] %>, | |
Be kind to your <%= @msg['noun_plural'] %> in <%= @msg['place'] %> | |
Where the weather is always <%= @msg['adjective'] %>. | |
You may think that this is the <%= @msg['noun_3'] %>, | |
Well it is."), | |
ERB.new( | |
"<%= @msg['adjective'] %> Macdonald had a <%= @msg['noun'] %>, E-I-E-I-O | |
and on that <%= @msg['noun'] %> he had an <%= @msg['animal'] %>, E-I-E-I-O | |
with a <%= @msg['noise'] %> <%= @msg['noise'] %> here | |
and a <%= @msg['noise'] %> <%= @msg['noise'] %> there, | |
here a <%= @msg['noise'] %>, there a <%= @msg['noise'] %>, | |
everywhere a <%= @msg['noise'] %> <%= @msg['noise'] %>, | |
<%= @msg['adjective'] %> Macdonald had a <%= @msg['noun'] %>, E-I-E-I-O. | |
"), | |
ERB.new( | |
"Two <%= @msg['noun_plural_1'] %>, both alike in dignity, | |
In fair <%= @msg['place'] %>, where we lay our scene, | |
From ancient <%= @msg['noun_1'] %> break to new mutiny, | |
Where civil blood makes civil hands unclean. | |
From forth the fatal loins of these two foes | |
A pair of star-cross`d <%= @msg['noun_plural_2'] %> take their life; | |
Whole misadventured piteous overthrows | |
Do with their <%= @msg['noun_2'] %> bury their parents` strife. | |
The fearful passage of their <%= @msg['adjective_1'] %> love, | |
And the continuance of their parents` rage, | |
Which, but their children`s end, nought could <%= @msg['verb_1'] %>, | |
Is now the <%= @msg['number'] %> hours` traffic of our stage; | |
The which if you with <%= @msg['adjective_2'] %> <%= @msg['body_part'] %> attend, | |
What here shall <%= @msg['verb_2'] %>, our toil shall strive to mend. | |
"), | |
] | |
def initialize(msg) | |
@msg = msg | |
end | |
def to_s | |
MADLIBS[@msg['madlib']].result(binding) | |
end | |
end | |
host = 'localhost' | |
b = Bunny.new(:host => host) | |
b.start | |
exchange = b.exchange('madlib_publisher', :type => :fanout) | |
b.queue('madlib_creator').subscribe do |msg| | |
begin | |
puts "Received Message: #{msg[:payload]}" | |
new_message = Madlib.new(JSON.load(msg[:payload])).to_s | |
puts "Publishing Message: #{new_message}" | |
exchange.publish(new_message) | |
rescue Exception => e | |
end | |
end | |
b.stop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bunny' | |
require 'json' | |
your_name_here = '' | |
host = 'localhost' | |
b = Bunny.new(:host => host) | |
b.start | |
direct_exchange = b.exchange('') | |
# Uncomment and fill in one or more madlibs | |
# | |
#message = JSON.dump( | |
# :name => your_name_here, | |
# :madlib => 0, | |
# :noun_1 => '', | |
# :noun_plural => '', | |
# :noun_2 => '', | |
# :place => '', | |
# :adjective => '', | |
# :noun_3 => '' | |
#) | |
# | |
#message = JSON.dump( | |
# :name => your_name_here, | |
# :madlib => 1, | |
# :adjective => '', | |
# :noun => '', | |
# :animal => '', | |
# :noise => '' | |
#) | |
# | |
#message = JSON.dump( | |
# :name => your_name_here, | |
# :madlib => 2, | |
# :noun_plural_1 => '', | |
# :place => '', | |
# :noun_1 => '', | |
# :noun_plural_2 => '', | |
# :noun_2 => '', | |
# :adjective_1 => '', | |
# :verb_1 => '', | |
# :number => '', | |
# :adjective_2 => '', | |
# :body_part => '', | |
# :verb_2 => '' | |
#) | |
puts "Published Message: #{message}" | |
direct_exchange.publish(message, :key => 'madlib_creator') | |
b.stop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bunny' | |
something_unique = '' | |
host = 'localhost' | |
b = Bunny.new(:host => host) | |
b.start | |
exchange = b.exchange('madlib_publisher', :type => :fanout) | |
queue = b.queue(something_unique) | |
queue.bind(exchange) | |
queue.subscribe do |msg| | |
puts '-' * 80 | |
puts msg[:payload] | |
end | |
b.stop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment