Class: Hare::Runner
- Inherits:
-
Object
- Object
- Hare::Runner
- Defined in:
- lib/hare/runner.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(argv) ⇒ Runner
constructor
A new instance of Runner.
- #parse! ⇒ Object
- #parser ⇒ Object
- #run! ⇒ Object
Constructor Details
#initialize(argv) ⇒ Runner
Returns a new instance of Runner.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/hare/runner.rb', line 11 def initialize(argv) @argv = argv @options = { :logging => false, :publish => false, :amqp => { :host => 'localhost', :port => '5672', :exchange => { :name => nil, :kind => :direct, }, :queue => '', :vhost => '/', :timeout => 0 } } parse! end |
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments.
9 10 11 |
# File 'lib/hare/runner.rb', line 9 def arguments @arguments end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/hare/runner.rb', line 8 def @options end |
Instance Method Details
#parse! ⇒ Object
98 99 100 101 |
# File 'lib/hare/runner.rb', line 98 def parse! parser.parse! @argv @arguments = @argv end |
#parser ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/hare/runner.rb', line 33 def parser @parser ||= OptionParser.new do |opts| opts. = "Usage: hare [options] [MSG]" opts.separator "" opts.separator "Common Options:" opts.on("-h", "--host HOST", "The AMQP server host") { |host| @options[:amqp][:host] = host } opts.on("-p", "--port PORT", "The AMQP server host port") { |port| @options[:amqp][:port] = port } opts.on("--vhost VHOST", "The AMQP vhost on which to connect") { |vhost| @options[:amqp][:vhost] = vhost } opts.on("--exchange_name EXCHANGE", "The name of the AMQP exchange on which to connect") { |exc| @options[:amqp][:exchange][:name] = exc } opts.on("--exchange_type TYPE", "The type of the AMQP exchange on which to connect") do |type| if ['topic', 'direct', 'fanout'].member? type @options[:amqp][:exchange][:type] = type.to_sym else puts "Exchange type must be (topic|direct|fanout), not #{type}." exit 1 end end opts.on("--username NAME", "The AMQP username.") { |u| @options[:amqp][:username] = u } opts.on("--password PSWD", "The AMQP password for the user given.") { |p| @options[:amqp][:password] = p } opts.on("--route_key KEY", "The key to route messages over.") { |k| @options[:amqp][:key] = k } opts.on("--logging", "Enable logging of AMQP interactions.") { @options[:logging] = true } opts.separator "" opts.separator "Consumer Options: " opts.on("--queue QUEUE", "The queue on which to listen.") { |q| @options[:amqp][:queue] = q } opts.on("--timeout TIME", "The time after which queue subscription will end.") { |t| @options[:amqp][:timeout] = t } opts.separator "" opts.separator "Producer Options: " opts.on("--producer", "Toggle to enable producing messages") { @options[:publish] = true } opts.separator "" opts.on_tail("--help", "Show this message.") do puts opts exit end opts.on_tail("-v", "--version", "Show version") { puts Hare::VERSION; exit } end end |
#run! ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/hare/runner.rb', line 103 def run! amqp = @options[:amqp] b = Bunny.new(:host => amqp[:host], :port => amqp[:port], :logging => amqp[:logging]) b.start eopts = amqp[:exchange] exch = b.exchange(eopts[:name], :type => eopts[:type]) if @options[:publish] exch.publish(@arguments[0], :key => amqp[:key]) else q = b.queue(amqp[:queue]) q.bind(exch, :key => amqp[:key]) q.subscribe(:timeout => amqp[:timeout]) do |msg| puts "#{msg[:payload]}" end end b.stop end |