amqp_helpers 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/README.md +37 -0
- data/VERSION +1 -1
- data/amqp_helpers.gemspec +1 -0
- data/examples/daemon.rb +34 -0
- data/examples/publisher.rb +36 -0
- data/lib/amqp_helpers/publisher.rb +28 -0
- data/spec/daemon_spec.rb +1 -0
- data/spec/publisher_spec.rb +48 -0
- metadata +24 -15
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MjllYWI0ZTQwOThlZDU3NTA1YmI2MzBmNzU3ZmQ4NDBjNTZkYTI1OQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MmNmYzBiNzgwNTI1OWJiOTU1OTQ2Y2ZhOWE4NmZiOGY2MTYyMzUxOA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
OTRkZWI4MGM0ODFmNDRjZTQ5ODdjODAxMWZkZjUxZWI2NTIyYzNmNzJiYTRl
|
10
|
+
NzEyZTg2YWE4ODc5ZmNjYWNkZTgwMGI5MWI2MmMxYjg4MjM4NjZhOTZmOTQw
|
11
|
+
YWQ3YzRjYzBiNDVkOTYxMzhkZjg4NzYxNmQ0OTM4YzViZjRiNTk=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
N2NmN2VjMGQ5MWM1YTY3YTVlODRmN2Q1OTQ3OTVkZWMzZDk3NWY1NGY5ODNk
|
14
|
+
N2JkNTdjZTY5Y2RkZmIzNTNjZWJkNjBmNGE2YmNhYjNhNDE2OWU4YzAyZGU0
|
15
|
+
NjI3ZTczMzE3YTY5OTY0Nzg2ZDJiNTRiZGYwNDNmMjQ2OTFlNjM=
|
data/README.md
CHANGED
@@ -34,3 +34,40 @@ AMQPHelpers::Daemon.new({
|
|
34
34
|
puts "AMQP Incoming message #{payload}"
|
35
35
|
end
|
36
36
|
```
|
37
|
+
|
38
|
+
## Publisher
|
39
|
+
|
40
|
+
`AMQPHelpers::Publisher` allows you to publish an AMQP message in an easy way.
|
41
|
+
|
42
|
+
### Example
|
43
|
+
|
44
|
+
It takes the same configuration hash as the daemon does.
|
45
|
+
* First argument of publish is the payload which will be JSON encoded.
|
46
|
+
* Second arugment specifies the exchange configuration which should be used.
|
47
|
+
* Third argument defines the routing key.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
publisher = AMQPHelpers::Publisher.new({
|
51
|
+
name: 'nba-scores-daemon',
|
52
|
+
environment: 'production',
|
53
|
+
logger: PXEConfGen.logger,
|
54
|
+
connection_params: {
|
55
|
+
host: 'localhost',
|
56
|
+
port: 5672
|
57
|
+
},
|
58
|
+
queue_params: { durable: false },
|
59
|
+
exchanges: {
|
60
|
+
'nba.scores': {
|
61
|
+
params: {
|
62
|
+
type: :topic,
|
63
|
+
durable: false
|
64
|
+
},
|
65
|
+
bindings: [
|
66
|
+
{ routing_key: 'north.#' },
|
67
|
+
{ routing_key: 'south.#' }
|
68
|
+
]
|
69
|
+
}
|
70
|
+
})
|
71
|
+
|
72
|
+
publisher.publish('1:1', 'nba.scores', 'south.east') # => true
|
73
|
+
```
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/amqp_helpers.gemspec
CHANGED
data/examples/daemon.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
APP_ROOT = File.join(File.dirname(Pathname.new(__FILE__).realpath),'..')
|
5
|
+
|
6
|
+
$:.unshift File.join(APP_ROOT, 'lib')
|
7
|
+
|
8
|
+
require 'amqp_helpers/daemon'
|
9
|
+
|
10
|
+
AMQPHelpers::Daemon.new({
|
11
|
+
name: 'nba-scores-daemon',
|
12
|
+
environment: 'development',
|
13
|
+
connection_params: {
|
14
|
+
user: 'guest',
|
15
|
+
password: 'guest',
|
16
|
+
host: 'localhost',
|
17
|
+
port: 5672
|
18
|
+
},
|
19
|
+
queue_params: { durable: false },
|
20
|
+
exchanges: {
|
21
|
+
'nba.scores' => {
|
22
|
+
params: {
|
23
|
+
type: :topic,
|
24
|
+
durable: false
|
25
|
+
},
|
26
|
+
bindings: [
|
27
|
+
{ routing_key: 'north.#' },
|
28
|
+
{ routing_key: 'south.#' }
|
29
|
+
]
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}).start do |delivery_info, payload|
|
33
|
+
puts "AMQP Incoming message #{payload}"
|
34
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
APP_ROOT = File.join(File.dirname(Pathname.new(__FILE__).realpath),'..')
|
5
|
+
|
6
|
+
$:.unshift File.join(APP_ROOT, 'lib')
|
7
|
+
|
8
|
+
require 'amqp_helpers/publisher'
|
9
|
+
|
10
|
+
config = {
|
11
|
+
name: 'nba-scores-daemon',
|
12
|
+
environment: 'production',
|
13
|
+
connection_params: {
|
14
|
+
user: 'guest',
|
15
|
+
password: 'guest',
|
16
|
+
host: 'localhost',
|
17
|
+
port: 5672
|
18
|
+
},
|
19
|
+
queue_params: { durable: false },
|
20
|
+
exchanges: {
|
21
|
+
'nba.scores' => {
|
22
|
+
params: {
|
23
|
+
type: :topic,
|
24
|
+
durable: false
|
25
|
+
},
|
26
|
+
bindings: [
|
27
|
+
{ routing_key: 'north.#' },
|
28
|
+
{ routing_key: 'south.#' }
|
29
|
+
]
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
publisher = AMQPHelpers::Publisher.new(config)
|
35
|
+
|
36
|
+
publisher.publish('test', 'nba.scores', 'north.seattle')
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'bunny'
|
2
|
+
|
3
|
+
module AMQPHelpers
|
4
|
+
class Publisher
|
5
|
+
|
6
|
+
def initialize(config)
|
7
|
+
@config = config
|
8
|
+
end
|
9
|
+
|
10
|
+
def publish(message, exchange_name, routing_key)
|
11
|
+
Bunny.run(@config) do |connection|
|
12
|
+
exchange_config = @config[:exchanges][exchange_name]
|
13
|
+
return false unless exchange_config
|
14
|
+
|
15
|
+
exchange = connection.exchange(exchange_name, exchange_config[:params])
|
16
|
+
exchange.publish(message,
|
17
|
+
key: routing_key,
|
18
|
+
persistent: exchange_config[:params][:durable],
|
19
|
+
content_type: 'application/json')
|
20
|
+
end
|
21
|
+
true
|
22
|
+
rescue => error
|
23
|
+
Airbrake.notify_or_ignore(error) if defined?(Airbrake)
|
24
|
+
Rails.logger.error("#{error.class}: #{error}") if defined?(Rails)
|
25
|
+
raise error
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/spec/daemon_spec.rb
CHANGED
@@ -46,6 +46,7 @@ describe AMQPHelpers::Daemon do
|
|
46
46
|
connection.stub(:open?).and_return(true)
|
47
47
|
connection.stub(:channel_max).and_return(0)
|
48
48
|
connection.stub(:on_connection)
|
49
|
+
connection.stub(:on_open)
|
49
50
|
connection.stub(:on_recovery)
|
50
51
|
connection.stub(:on_error)
|
51
52
|
connection.stub(:on_tcp_connection_loss)
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'amqp_helpers/publisher'
|
4
|
+
|
5
|
+
describe AMQPHelpers::Publisher do
|
6
|
+
let(:config) do
|
7
|
+
{
|
8
|
+
name: 'lala',
|
9
|
+
queue_name: 'example-queue',
|
10
|
+
queue_params: { durable: false },
|
11
|
+
logger: Logger.new('/dev/null'),
|
12
|
+
connection_params: { host: 'localhost', port: 1337 },
|
13
|
+
exchanges: {
|
14
|
+
'test-topic' => {
|
15
|
+
params: { type: :topic, durable: false },
|
16
|
+
bindings: [ { routing_key: 'lala.#' } ]
|
17
|
+
}
|
18
|
+
}
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
let(:instance) { described_class.new(config) }
|
23
|
+
let(:bunny_double) { double(Bunny::Session) }
|
24
|
+
let(:exchange_double) { double(Bunny::Exchange) }
|
25
|
+
|
26
|
+
before do
|
27
|
+
allow(Bunny).to receive(:run).and_yield(bunny_double)
|
28
|
+
allow(bunny_double).to receive(:exchange).and_return(exchange_double)
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#publish' do
|
32
|
+
context 'exchange specified' do
|
33
|
+
it 'publishes the message' do
|
34
|
+
expect(exchange_double).to receive(:publish).
|
35
|
+
with('we are testing', key: 'lala.will.exist', persistent: false, content_type: 'application/json')
|
36
|
+
|
37
|
+
expect(instance.publish('we are testing', 'test-topic', 'lala.will.exist')).to eq(true)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'wrong exchange specified' do
|
42
|
+
it 'does not publish the message' do
|
43
|
+
expect(exchange_double).to_not receive(:publish)
|
44
|
+
expect(instance.publish('we are testing', 'undefined-topic', 'lala.will.exist')).to eq(false)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amqp_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Nils Caspar
|
@@ -11,12 +10,11 @@ authors:
|
|
11
10
|
autorequire:
|
12
11
|
bindir: bin
|
13
12
|
cert_chain: []
|
14
|
-
date: 2014-
|
13
|
+
date: 2014-10-06 00:00:00.000000000 Z
|
15
14
|
dependencies:
|
16
15
|
- !ruby/object:Gem::Dependency
|
17
16
|
name: rspec
|
18
17
|
requirement: !ruby/object:Gem::Requirement
|
19
|
-
none: false
|
20
18
|
requirements:
|
21
19
|
- - ~>
|
22
20
|
- !ruby/object:Gem::Version
|
@@ -24,7 +22,6 @@ dependencies:
|
|
24
22
|
type: :development
|
25
23
|
prerelease: false
|
26
24
|
version_requirements: !ruby/object:Gem::Requirement
|
27
|
-
none: false
|
28
25
|
requirements:
|
29
26
|
- - ~>
|
30
27
|
- !ruby/object:Gem::Version
|
@@ -32,7 +29,6 @@ dependencies:
|
|
32
29
|
- !ruby/object:Gem::Dependency
|
33
30
|
name: rake
|
34
31
|
requirement: !ruby/object:Gem::Requirement
|
35
|
-
none: false
|
36
32
|
requirements:
|
37
33
|
- - ~>
|
38
34
|
- !ruby/object:Gem::Version
|
@@ -40,7 +36,6 @@ dependencies:
|
|
40
36
|
type: :development
|
41
37
|
prerelease: false
|
42
38
|
version_requirements: !ruby/object:Gem::Requirement
|
43
|
-
none: false
|
44
39
|
requirements:
|
45
40
|
- - ~>
|
46
41
|
- !ruby/object:Gem::Version
|
@@ -48,7 +43,6 @@ dependencies:
|
|
48
43
|
- !ruby/object:Gem::Dependency
|
49
44
|
name: amqp
|
50
45
|
requirement: !ruby/object:Gem::Requirement
|
51
|
-
none: false
|
52
46
|
requirements:
|
53
47
|
- - ~>
|
54
48
|
- !ruby/object:Gem::Version
|
@@ -56,15 +50,27 @@ dependencies:
|
|
56
50
|
type: :runtime
|
57
51
|
prerelease: false
|
58
52
|
version_requirements: !ruby/object:Gem::Requirement
|
59
|
-
none: false
|
60
53
|
requirements:
|
61
54
|
- - ~>
|
62
55
|
- !ruby/object:Gem::Version
|
63
56
|
version: '1.3'
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: bunny
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ~>
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 1.4.1
|
64
|
+
type: :runtime
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ~>
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 1.4.1
|
64
71
|
- !ruby/object:Gem::Dependency
|
65
72
|
name: syslogger
|
66
73
|
requirement: !ruby/object:Gem::Requirement
|
67
|
-
none: false
|
68
74
|
requirements:
|
69
75
|
- - ~>
|
70
76
|
- !ruby/object:Gem::Version
|
@@ -72,7 +78,6 @@ dependencies:
|
|
72
78
|
type: :runtime
|
73
79
|
prerelease: false
|
74
80
|
version_requirements: !ruby/object:Gem::Requirement
|
75
|
-
none: false
|
76
81
|
requirements:
|
77
82
|
- - ~>
|
78
83
|
- !ruby/object:Gem::Version
|
@@ -93,35 +98,39 @@ files:
|
|
93
98
|
- Rakefile
|
94
99
|
- VERSION
|
95
100
|
- amqp_helpers.gemspec
|
101
|
+
- examples/daemon.rb
|
102
|
+
- examples/publisher.rb
|
96
103
|
- lib/amqp_helpers.rb
|
97
104
|
- lib/amqp_helpers/daemon.rb
|
105
|
+
- lib/amqp_helpers/publisher.rb
|
98
106
|
- spec/daemon_spec.rb
|
107
|
+
- spec/publisher_spec.rb
|
99
108
|
- spec/spec_helper.rb
|
100
109
|
homepage: https://github.com/ninech/amqp_helpers
|
101
110
|
licenses:
|
102
111
|
- MIT
|
112
|
+
metadata: {}
|
103
113
|
post_install_message:
|
104
114
|
rdoc_options: []
|
105
115
|
require_paths:
|
106
116
|
- lib
|
107
117
|
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
-
none: false
|
109
118
|
requirements:
|
110
119
|
- - ! '>='
|
111
120
|
- !ruby/object:Gem::Version
|
112
121
|
version: '0'
|
113
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
-
none: false
|
115
123
|
requirements:
|
116
124
|
- - ! '>='
|
117
125
|
- !ruby/object:Gem::Version
|
118
126
|
version: '0'
|
119
127
|
requirements: []
|
120
128
|
rubyforge_project:
|
121
|
-
rubygems_version: 1.
|
129
|
+
rubygems_version: 2.1.11
|
122
130
|
signing_key:
|
123
|
-
specification_version:
|
131
|
+
specification_version: 4
|
124
132
|
summary: Simple helpers to achieve various AMQP tasks.
|
125
133
|
test_files:
|
126
134
|
- spec/daemon_spec.rb
|
135
|
+
- spec/publisher_spec.rb
|
127
136
|
- spec/spec_helper.rb
|