amqp_directory_broadcaster 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT LICENSE
2
+
3
+ Copyright (c) 2010 Nathan Stults
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,3 @@
1
+ USAGE:
2
+
3
+ broadcast_directory --source "~/source_dir" --broker "amqp://ganga" --exchange "my.exchange" --routing-key "my.event.name"
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ dir = File.expand_path(File.dirname(__FILE__))
4
+
5
+ require File.join(dir,"../lib/amqp_directory_broadcaster")
6
+
7
+ AmqpDirectoryBroadcaster.broadcast
@@ -0,0 +1,73 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+
4
+ require 'trollop'
5
+ require 'bunny'
6
+
7
+ $:.unshift(File.expand_path(File.dirname(__FILE__)))
8
+
9
+ require "amqp_directory_broadcaster/rabbit"
10
+
11
+ module AmqpDirectoryBroadcaster
12
+
13
+ def self.parse_options
14
+ opts = Trollop::options do
15
+ opt :source, "The directory you want to publish JSON messages from", :type => String, :default=>"."
16
+ opt :filter, "The file filter to use to select which files to send", :default => "*.json"
17
+ opt :broker, "The uri of the AMQP broker you want to publish to", :type => String, :default=>"localhost"
18
+ opt :exchange, "The name of the exchange you want to publish to", :type => String
19
+ opt :type, "The type of the exchange you want to publish to", :type => String, :default => "topic"
20
+ opt :durable, "Whether or not the exchange is durable", :default => true
21
+ opt :routing_key, "The routing key to use when publishing messages", :type=>String, :short => '-k'
22
+ opt :verbose, :default => false
23
+ end
24
+ Trollop::die :exchange, "must be specified" unless opts[:exchange]
25
+ opts
26
+ end
27
+
28
+ def self.broadcast(options = nil)
29
+ options ||= parse_options
30
+ dir = File.expand_path(options[:source])
31
+ filter = File.join(dir,options[:filter])
32
+ exchange_name = options[:exchange]
33
+ broker_uri = options[:broker]
34
+ broker = AmqpDirectoryBroadcaster::Rabbit.new(broker_uri)
35
+
36
+ puts "Loading messages from #{filter}"
37
+ puts "Sending messages to #{exchange_name} @ #{broker_uri}"
38
+
39
+ bunny = breed(broker.to_h)
40
+
41
+ begin
42
+ exchange_options = {
43
+ :durable => options[:durable],
44
+ :type => options[:type].to_sym
45
+ }
46
+
47
+ exchange = bunny.exchange(exchange_name,exchange_options)
48
+ Dir[filter].each do |message|
49
+ puts "Processing message file: #{message}" if options[:verbose]
50
+ message_text = File.read(message)
51
+ if routing_key = options[:routing_key]
52
+ puts "Publishing to key: #{routing_key}, message: #{message_text}" if options[:verbose]
53
+ exchange.publish(message_text, :key => routing_key)
54
+ else
55
+ puts "Publishing message:#{message_text}" if options[:verbose]
56
+ exchange.publish(message_text)
57
+ end
58
+ end
59
+ ensure
60
+ bunny.stop
61
+ end
62
+ end
63
+
64
+ def self.breed(options)
65
+ b = Bunny.new(options)
66
+ b.start
67
+ b
68
+ end
69
+
70
+ end
71
+
72
+
73
+
@@ -0,0 +1,37 @@
1
+ require 'uri'
2
+
3
+ module AmqpDirectoryBroadcaster
4
+ class Rabbit
5
+ attr_accessor :host, :username, :password, :vhost, :port
6
+
7
+ def initialize(constr=nil)
8
+ @host = "localhost"
9
+ @username = "guest"
10
+ @password = "guest"
11
+ @vhost = "/"
12
+ @port = 5672
13
+
14
+ if (constr =~ /amqp:\/\//)
15
+ uri = URI.parse(constr)
16
+ host = uri.host
17
+ port = uri.port || 5672
18
+ username = uri.user || "guest"
19
+ password = uri.password || "guest"
20
+ vhost = uri.path.strip.length < 1 ? "/" : uri.path
21
+ else
22
+ host = constr
23
+ end
24
+ end
25
+
26
+ def to_h
27
+ {
28
+ :host=>host,
29
+ :port=>port,
30
+ :username=>username,
31
+ :password=>password,
32
+ :vhost=>vhost
33
+ }
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module AmqpDirectoryBroadcaster
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amqp_directory_broadcaster
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Nathan Stults
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-23 00:00:00 -08:00
19
+ default_executable: broadcast_directory
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bunny
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: trollop
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rake
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ description:
64
+ email:
65
+ - hereiam@sonic.net
66
+ executables:
67
+ - broadcast_directory
68
+ extensions: []
69
+
70
+ extra_rdoc_files: []
71
+
72
+ files:
73
+ - lib/amqp_directory_broadcaster/rabbit.rb
74
+ - lib/amqp_directory_broadcaster/version.rb
75
+ - lib/amqp_directory_broadcaster.rb
76
+ - bin/broadcast_directory
77
+ - LICENSE.txt
78
+ - README
79
+ has_rdoc: true
80
+ homepage: http://github.com/PlasticLizard/amqp_directory_broadcaster
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options: []
85
+
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ requirements: []
107
+
108
+ rubyforge_project:
109
+ rubygems_version: 1.3.7
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: Read messages from a directory and send them to an AMQP exchange
113
+ test_files: []
114
+