karl_malone 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -0
- data/lib/karl_malone.rb +0 -0
- data/lib/karl_malone/connection_broker.rb +36 -0
- data/lib/karl_malone/consumer.rb +41 -0
- data/lib/karl_malone/consumer_options.rb +70 -0
- metadata +81 -0
data/README.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
How to use:
|
2
|
+
|
3
|
+
consumer = KarlMalone::Consumer.new(callback_object, callback, exchange_opts, queue_opts)
|
4
|
+
consumers = [consumer]
|
5
|
+
broker = KarlMalone::ConnectionBroker.new(consumers, options={})
|
6
|
+
broker.start!
|
7
|
+
|
8
|
+
|
9
|
+
Maybe have a 'register_consumer(consumer_options)' function that wraps the consumer creation?
|
10
|
+
|
11
|
+
TODO:
|
12
|
+
publish
|
13
|
+
Guaranteed delivery on publish
|
14
|
+
Integration specs with rabbit, separate rake task
|
data/lib/karl_malone.rb
ADDED
File without changes
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'amqp'
|
2
|
+
|
3
|
+
module KarlMalone
|
4
|
+
class ConnectionBroker
|
5
|
+
def initialize(consumers, override_settings={})
|
6
|
+
@consumers = consumers
|
7
|
+
@override_settings = override_settings
|
8
|
+
end
|
9
|
+
|
10
|
+
def start!
|
11
|
+
AMQP.start(connection_settings) do |connection|
|
12
|
+
consumers.each do |consumer|
|
13
|
+
consumer.start!(connection)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
attr_reader :override_settings, :consumers
|
20
|
+
|
21
|
+
def connection_settings
|
22
|
+
{
|
23
|
+
:host => '127.0.0.1',
|
24
|
+
:port => 5672,
|
25
|
+
:user => 'guest',
|
26
|
+
:pass => 'guest',
|
27
|
+
:vhost => '/',
|
28
|
+
:ssl => false,
|
29
|
+
:heartbeat => 0,
|
30
|
+
:frame_max => 131072,
|
31
|
+
:on_tcp_connection_failure => Proc.new {|settings| raise('TCP connection error')},
|
32
|
+
:on_possible_authentication_failure => Proc.new {|settings| raise('Authnication failure')}
|
33
|
+
}.merge(override_settings)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'amqp'
|
2
|
+
require 'forwardable'
|
3
|
+
require 'karl_malone/consumer_options'
|
4
|
+
|
5
|
+
module KarlMalone
|
6
|
+
class Consumer
|
7
|
+
extend Forwardable
|
8
|
+
def_delegators :options, :routing_key, :exchange_type, :exchange_name,
|
9
|
+
:queue_name, :queue_configurations, :exchange_configurations
|
10
|
+
|
11
|
+
def initialize(callback_object, callback, exchange_options, queue_options)
|
12
|
+
@options = ConsumerOptions.new(exchange_options, queue_options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def start!(connection)
|
16
|
+
bind_callback(queue(channel(connection)))
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
attr_reader :options
|
21
|
+
|
22
|
+
def bind_callback(_queue)
|
23
|
+
_queue.subscribe(routing_key: routing_key) do |payload|
|
24
|
+
callback_object.send(callback, payload)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def channel(connection)
|
29
|
+
AMPQ::Channel.new(connection)
|
30
|
+
end
|
31
|
+
|
32
|
+
def queue(channel)
|
33
|
+
AMQP::Queue.new(channel, queue_name, queue_configurations).bind(exchange(channel))
|
34
|
+
end
|
35
|
+
|
36
|
+
def exchange(channel)
|
37
|
+
AMQP::Exchange.new(channel, exchange_type, exchange_name, exchange_configurations)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module KarlMalone
|
2
|
+
class ConsumerOptions
|
3
|
+
EXCHANGE_TYPES = [:direct, :fanout, :topic, :headers]
|
4
|
+
attr_reader :exchange_options, :queue_options
|
5
|
+
|
6
|
+
def initialize(exchange_options, queue_options)
|
7
|
+
@exchange_options = exchange_options
|
8
|
+
@queue_options = queue_options
|
9
|
+
validate_exchange_options!
|
10
|
+
validate_queue_options!
|
11
|
+
end
|
12
|
+
|
13
|
+
def exchange_type
|
14
|
+
exchange_options[:type]
|
15
|
+
end
|
16
|
+
|
17
|
+
def exchange_name
|
18
|
+
exchange_options[:name]
|
19
|
+
end
|
20
|
+
|
21
|
+
def queue_name
|
22
|
+
queue_options[:name]
|
23
|
+
end
|
24
|
+
|
25
|
+
def routing_key
|
26
|
+
queue_options[:routing_key]
|
27
|
+
end
|
28
|
+
|
29
|
+
def exchange_configurations
|
30
|
+
{
|
31
|
+
:passive => false,
|
32
|
+
:durable => false,
|
33
|
+
:auto_delete => false,
|
34
|
+
:internal => false,
|
35
|
+
:default_routing_key => nil,
|
36
|
+
:no_declare => true
|
37
|
+
}.merge(exchange_options)
|
38
|
+
end
|
39
|
+
|
40
|
+
def queue_configurations
|
41
|
+
{
|
42
|
+
:passive => false,
|
43
|
+
:durable => false,
|
44
|
+
:exclusive => false,
|
45
|
+
:auto_delete => false,
|
46
|
+
:no_wait => false
|
47
|
+
}.merge(queue_options)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def validate_exchange_options!
|
53
|
+
[:name, :type].each do |required_field|
|
54
|
+
raise "An exchange #{required_field} is required." if exchange_options[required_field].nil?
|
55
|
+
end
|
56
|
+
|
57
|
+
validate_type!
|
58
|
+
end
|
59
|
+
|
60
|
+
def validate_type!
|
61
|
+
unless EXCHANGE_TYPES.include?(exchange_type)
|
62
|
+
raise "#{exchange_type} is not a recognized exchange type."
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def validate_queue_options!
|
67
|
+
raise "A queue name is required." if queue_options[:name].nil?
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: karl_malone
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben Voss
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: amqp
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.13.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.13.0
|
46
|
+
description: A convenience wrapper around messaging queues, specifically Rabbit
|
47
|
+
email: bwvoss@gmail.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- lib/karl_malone/connection_broker.rb
|
53
|
+
- lib/karl_malone/consumer.rb
|
54
|
+
- lib/karl_malone/consumer_options.rb
|
55
|
+
- lib/karl_malone.rb
|
56
|
+
- README.md
|
57
|
+
homepage: https://github.com/bwvoss/Karl-Malone
|
58
|
+
licenses: []
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.8.24
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: The Mailman delivers his messages
|
81
|
+
test_files: []
|