simple_messaging 0.0.1
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 +7 -0
- data/.gitignore +23 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/lib/simple_messaging.rb +7 -0
- data/lib/simple_messaging/message_queue.rb +65 -0
- data/lib/simple_messaging/message_queue/rabbit.rb +67 -0
- data/lib/simple_messaging/message_queue/sqs.rb +50 -0
- data/lib/simple_messaging/version.rb +3 -0
- data/simple_messaging.gemspec +27 -0
- data/spec/message_queue_spec.rb +45 -0
- data/spec/spec_helper.rb +8 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c42f9f5b324c4fa8978193e856ad49b8fcf62681
|
4
|
+
data.tar.gz: b5b1aab8dc6ebe074f6ca8b28115488ba9042f0f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2d3a05c535d6b5fb38aac16760f9ecbfe81e9df80817772977a239d37f92b68e4ee5f2fb7aac8f23c0822379adf76c61aafa7d76cf4a36b9e876b263225c24d
|
7
|
+
data.tar.gz: 16f9802ce4e7b93fa12b555f4e25d9827d27947e58b23d5ab86927d8514ee70180967aba7b51329e9248578c457958acedafbfa4718e2768ec26a509a1f8c499
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
config
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Maxime Santerre
|
2
|
+
|
3
|
+
MIT License
|
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.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# SimpleMessaging
|
2
|
+
|
3
|
+
Simple messaging abstracts your messaging needs so you can easily switch between
|
4
|
+
supported drivers.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'simple-messaging'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install simple-messaging
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Set the driver name manually
|
23
|
+
```ruby
|
24
|
+
SimpleMessaging::MessageQueue.driver_name = "sqs"
|
25
|
+
```
|
26
|
+
|
27
|
+
or set it in your config/messaging.yml file
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
instance = SimpleMessaging::MessageQueue.instance(queue_name)
|
31
|
+
instance.enqueue(message)
|
32
|
+
instance.poll do |message|
|
33
|
+
p message
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it ( https://github.com/[my-github-username]/simple-messaging/fork )
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
module SimpleMessaging
|
2
|
+
class MessageQueue
|
3
|
+
|
4
|
+
# Deriving classes need to implement the following methods
|
5
|
+
# def enqueue(message)
|
6
|
+
# def poll(&block)
|
7
|
+
# def pop(&block)
|
8
|
+
|
9
|
+
def self.unregister_queue(name)
|
10
|
+
@queues[name] = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.driver_name=(driver_name)
|
14
|
+
if driver_name != @driver_name
|
15
|
+
@driver_class = nil
|
16
|
+
@queues = nil
|
17
|
+
@driver_name = driver_name.to_s
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.instance(name)
|
22
|
+
@queues ||= {}
|
23
|
+
return @queues[name] if @queues[name]
|
24
|
+
|
25
|
+
@driver_class ||=
|
26
|
+
case driver_name
|
27
|
+
when 'sqs'
|
28
|
+
MessageQueue::SQS
|
29
|
+
when 'rabbit'
|
30
|
+
MessageQueue::Rabbit
|
31
|
+
else
|
32
|
+
raise 'Messaging driver "#{driver_name}" not implemented'
|
33
|
+
end
|
34
|
+
|
35
|
+
@queues[name] = @driver_class.new(name)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.driver_name
|
39
|
+
return @driver_name if @driver_name
|
40
|
+
messaging_config_file = Pathname.new(Dir.pwd).join('config', 'messaging.yml').to_s
|
41
|
+
|
42
|
+
if File.exist? messaging_config_file
|
43
|
+
messaging_configs = YAML.load(File.open(messaging_config_file).read)
|
44
|
+
messaging_configs[environment]['driver']
|
45
|
+
else
|
46
|
+
raise "Messaging driver isn't set"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
52
|
+
def environment
|
53
|
+
if defined? Rails
|
54
|
+
Rails.env
|
55
|
+
else
|
56
|
+
ENV["RUBY_ENV"] || "development"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def queue_name(name)
|
61
|
+
"#{queue_identifier}-#{name}"
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'amqp'
|
2
|
+
|
3
|
+
module SimpleMessaging
|
4
|
+
|
5
|
+
class MessageQueue::Rabbit < MessageQueue
|
6
|
+
|
7
|
+
attr_reader :channel, :queue, :channel, :name
|
8
|
+
|
9
|
+
def initialize(name)
|
10
|
+
@@em_thread ||= Thread.new { EventMachine.run }
|
11
|
+
@@connection ||= AMQP.connect(rabbit_configs)
|
12
|
+
@channel = AMQP::Channel.new(@@connection)
|
13
|
+
@exchange = @channel.default_exchange
|
14
|
+
@queue = @channel.queue(queue_name(name))
|
15
|
+
@name = name
|
16
|
+
end
|
17
|
+
|
18
|
+
def enqueue(message)
|
19
|
+
@exchange.publish message,
|
20
|
+
:routing_key => @queue.name,
|
21
|
+
:app_id => app_id
|
22
|
+
end
|
23
|
+
|
24
|
+
def poll(&block)
|
25
|
+
@queue.subscribe do |metadata, payload|
|
26
|
+
yield payload
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def pop(&block)
|
31
|
+
@queue.subscribe do |metadata, payload|
|
32
|
+
yield payload
|
33
|
+
break
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete
|
38
|
+
MessageQueue.unregister_queue(name)
|
39
|
+
@queue.delete
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def queue_identifier
|
45
|
+
environment
|
46
|
+
end
|
47
|
+
|
48
|
+
def rabbit_configs
|
49
|
+
rabbit_config_file = Pathname.new(Dir.pwd).join('config', 'rabbit.yml').to_s
|
50
|
+
if File.exist? rabbit_config_file
|
51
|
+
YAML.load(File.open(rabbit_config_file).read)[environment]
|
52
|
+
else
|
53
|
+
{ host: "127.0.0.1" }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def app_id
|
58
|
+
if defined? Rails
|
59
|
+
Rails.application.class.parent.to_s
|
60
|
+
else
|
61
|
+
"unknown"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'aws-sdk'
|
2
|
+
|
3
|
+
module SimpleMessaging
|
4
|
+
|
5
|
+
class MessageQueue::SQS < MessageQueue
|
6
|
+
|
7
|
+
attr_reader :name
|
8
|
+
|
9
|
+
def initialize(name)
|
10
|
+
@sqs = AWS::SQS.new
|
11
|
+
@queue = @sqs.queues.create(queue_name(name))
|
12
|
+
@name = name
|
13
|
+
end
|
14
|
+
|
15
|
+
def enqueue(message)
|
16
|
+
@queue.send_message(message)
|
17
|
+
end
|
18
|
+
|
19
|
+
def pop(&block)
|
20
|
+
@queue.receive_message do |message|
|
21
|
+
yield message.body
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def poll(&block)
|
26
|
+
@queue.poll do |message|
|
27
|
+
yield message.body
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def delete
|
32
|
+
MessageQueue.unregister_queue(name)
|
33
|
+
@queue.delete
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def queue_identifier
|
39
|
+
case environment
|
40
|
+
when "development"
|
41
|
+
prefix = (ENV["SQS_IDENTIFIER"] || `whoami`).strip
|
42
|
+
"#{prefix}-development"
|
43
|
+
else
|
44
|
+
environment
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_messaging/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple_messaging"
|
8
|
+
spec.version = SimpleMessaging::VERSION
|
9
|
+
spec.authors = ["Maxime Santerre"]
|
10
|
+
spec.email = ["mooxeh@gmail.com"]
|
11
|
+
spec.summary = %q{Abstraction layer for simple messaging operations}
|
12
|
+
spec.description = """Lets you easily change messaging adapters for simple queue and poll operations"""
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "amqp"
|
22
|
+
spec.add_dependency "aws-sdk"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe SimpleMessaging::MessageQueue, 'loads appropriate driver' do
|
4
|
+
|
5
|
+
context 'Using SQS driver' do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
SimpleMessaging::MessageQueue.driver_name = "sqs"
|
9
|
+
@queue = SimpleMessaging::MessageQueue.instance("test-queue")
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should load' do
|
13
|
+
expect(@queue.class.name.to_s).to eq "SimpleMessaging::MessageQueue::SQS"
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should publish messages' do
|
17
|
+
@queue.enqueue("Message")
|
18
|
+
@queue.pop do |message|
|
19
|
+
expect(message).to eq "Message"
|
20
|
+
expect(message).not_to eq "Potato"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'Using the RabbitMQ driver' do
|
26
|
+
|
27
|
+
before(:each) do
|
28
|
+
SimpleMessaging::MessageQueue.driver_name = "rabbit"
|
29
|
+
@queue = SimpleMessaging::MessageQueue.instance("test-queue")
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should load the rabbitmq driver' do
|
33
|
+
expect(@queue.class.name.to_s).to eq "SimpleMessaging::MessageQueue::Rabbit"
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should publish and receive messages' do
|
37
|
+
@queue.enqueue("Potato")
|
38
|
+
@queue.pop do |message|
|
39
|
+
expect(message).to eq "Message"
|
40
|
+
expect(message).not_to eq "Potato"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_messaging
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Maxime Santerre
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: amqp
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: aws-sdk
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.6'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Lets you easily change messaging adapters for simple queue and poll operations
|
84
|
+
email:
|
85
|
+
- mooxeh@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/simple_messaging.rb
|
98
|
+
- lib/simple_messaging/message_queue.rb
|
99
|
+
- lib/simple_messaging/message_queue/rabbit.rb
|
100
|
+
- lib/simple_messaging/message_queue/sqs.rb
|
101
|
+
- lib/simple_messaging/version.rb
|
102
|
+
- simple_messaging.gemspec
|
103
|
+
- spec/message_queue_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: ''
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.2.2
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Abstraction layer for simple messaging operations
|
129
|
+
test_files:
|
130
|
+
- spec/message_queue_spec.rb
|
131
|
+
- spec/spec_helper.rb
|