qsagi 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +27 -0
- data/Rakefile +6 -0
- data/lib/qsagi.rb +8 -0
- data/lib/qsagi/default_serializer.rb +11 -0
- data/lib/qsagi/json_serializer.rb +11 -0
- data/lib/qsagi/message.rb +18 -0
- data/lib/qsagi/queue.rb +89 -0
- data/lib/qsagi/version.rb +3 -0
- data/qsagi.gemspec +22 -0
- data/spec/qsagi/json_serializer_spec.rb +25 -0
- data/spec/qsagi/message_spec.rb +33 -0
- data/spec/qsagi/queue_spec.rb +74 -0
- data/spec/spec_helper.rb +27 -0
- metadata +94 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Braintree
|
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,27 @@
|
|
1
|
+
# Qsagi
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'qsagi'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install qsagi
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
## Contributing
|
22
|
+
|
23
|
+
1. Fork it
|
24
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
25
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
26
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
27
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/qsagi.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Qsagi
|
2
|
+
class Message
|
3
|
+
attr_reader :payload
|
4
|
+
|
5
|
+
def initialize(message, payload)
|
6
|
+
@delivery_details = message[:delivery_details]
|
7
|
+
@payload = payload
|
8
|
+
end
|
9
|
+
|
10
|
+
def delivery_tag
|
11
|
+
@delivery_details[:delivery_tag]
|
12
|
+
end
|
13
|
+
|
14
|
+
def exchange
|
15
|
+
@delivery_details[:exchange]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/qsagi/queue.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
module Qsagi
|
2
|
+
module Queue
|
3
|
+
def ack(message)
|
4
|
+
@queue.ack(:delivery_tag => message.delivery_tag)
|
5
|
+
end
|
6
|
+
|
7
|
+
def clear
|
8
|
+
loop do
|
9
|
+
message = @queue.pop
|
10
|
+
break if message[:payload] == :queue_empty
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def connect
|
15
|
+
@client = Bunny.new(:host => self.class.host, :port => self.class.port)
|
16
|
+
@client.start
|
17
|
+
@queue = @client.queue(self.class.queue_name, :durable => true, :arguments => {"x-ha-policy" => "all"})
|
18
|
+
@exchange = @client.exchange(self.class._exchange)
|
19
|
+
@queue.bind(@exchange, :key => self.class.queue_name) unless self.class._exchange.empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
def disconnect
|
23
|
+
@client.send(:close_socket) unless @client.nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
def pop(options = {})
|
27
|
+
auto_ack = options.fetch(:auto_ack, true)
|
28
|
+
message = @queue.pop(:ack => !auto_ack)
|
29
|
+
|
30
|
+
unless message[:payload] == :queue_empty
|
31
|
+
self.class._message_class.new(message, self.class._serializer.deserialize(message[:payload]))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def push(message)
|
36
|
+
#exchange = @client.exchange(self.class._exchange)
|
37
|
+
serialized_message = self.class._serializer.serialize(message)
|
38
|
+
# @exchange.publish(serialized_message, :key => @queue.name, :persistent => true)
|
39
|
+
@exchange.publish(serialized_message, :key => @queue.name, :persistent => true)
|
40
|
+
end
|
41
|
+
|
42
|
+
def reconnect
|
43
|
+
disconnect
|
44
|
+
connect
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.included(klass)
|
48
|
+
klass.extend ClassMethods
|
49
|
+
end
|
50
|
+
|
51
|
+
module ClassMethods
|
52
|
+
def connect(&block)
|
53
|
+
queue = new
|
54
|
+
|
55
|
+
begin
|
56
|
+
queue.connect
|
57
|
+
|
58
|
+
block.call(queue)
|
59
|
+
ensure
|
60
|
+
queue.disconnect
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def exchange(exchange)
|
65
|
+
@exchange = exchange
|
66
|
+
end
|
67
|
+
|
68
|
+
def message_class(message_class)
|
69
|
+
@message_class = message_class
|
70
|
+
end
|
71
|
+
|
72
|
+
def serializer(serializer)
|
73
|
+
@serializer = serializer
|
74
|
+
end
|
75
|
+
|
76
|
+
def _exchange
|
77
|
+
@exchange || ""
|
78
|
+
end
|
79
|
+
|
80
|
+
def _message_class
|
81
|
+
@message_class || Qsagi::Message
|
82
|
+
end
|
83
|
+
|
84
|
+
def _serializer
|
85
|
+
@serializer || Qsagi::DefaultSerializer
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/qsagi.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'qsagi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "qsagi"
|
8
|
+
gem.version = Qsagi::VERSION
|
9
|
+
gem.authors = ["Braintree"]
|
10
|
+
gem.email = ["code@getbraintree.com"]
|
11
|
+
gem.description = "A friendly way to talk to RabbitMQ"
|
12
|
+
gem.summary = "A friendly way to talk to RabbitMQ"
|
13
|
+
gem.homepage = "https://github.com/braintree/qsagi"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "bunny", "~> 0.8.0"
|
21
|
+
gem.add_dependency "json", "~> 1.7.0"
|
22
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Qsagi::JsonSerializer do
|
4
|
+
describe "self.deserialize" do
|
5
|
+
it "parses json" do
|
6
|
+
Qsagi::JsonSerializer.deserialize('{"a": "b"}').should == {"a" => "b"}
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "self.serialize" do
|
11
|
+
it "parses json" do
|
12
|
+
Qsagi::JsonSerializer.serialize({"a" => "b"}).should == '{"a":"b"}'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "serializes and deserializes correctly through a queue" do
|
17
|
+
json_queue = Class.new(ExampleQueue) do
|
18
|
+
serializer Qsagi::JsonSerializer
|
19
|
+
end
|
20
|
+
json_queue.connect do |queue|
|
21
|
+
queue.push :a => 1
|
22
|
+
queue.pop.payload.should == {'a' => 1}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Qsagi::Message do
|
4
|
+
describe "delivery_tag" do
|
5
|
+
it "returns the delivery_tag" do
|
6
|
+
data = {
|
7
|
+
:delivery_details => {:delivery_tag => "tag"},
|
8
|
+
:payload => "raw_payload"
|
9
|
+
}
|
10
|
+
Qsagi::Message.new(data, :parsed_payload).delivery_tag.should == "tag"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "exchange" do
|
15
|
+
it "returns the exchange" do
|
16
|
+
data = {
|
17
|
+
:delivery_details => {:exchange => "the_exchange"},
|
18
|
+
:payload => "raw_payload"
|
19
|
+
}
|
20
|
+
Qsagi::Message.new(data, :parsed_payload).exchange.should == "the_exchange"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "payload" do
|
25
|
+
it "returns the parsed payload" do
|
26
|
+
data = {
|
27
|
+
:delivery_details => {:delivery_tag => "tag"},
|
28
|
+
:payload => "raw_payload"
|
29
|
+
}
|
30
|
+
Qsagi::Message.new(data, :parsed_payload).payload.should == :parsed_payload
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Qsagi::Queue do
|
4
|
+
it "and push and pop from a queue" do
|
5
|
+
ExampleQueue.connect do |queue|
|
6
|
+
queue.push("message")
|
7
|
+
result = queue.pop
|
8
|
+
result.payload.should == "message"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "self.exchange" do
|
13
|
+
it "configures the exchange" do
|
14
|
+
queue_on_exchange1 = Class.new(ExampleQueue) do
|
15
|
+
exchange "exchange1"
|
16
|
+
end
|
17
|
+
queue_on_exchange2 = Class.new(ExampleQueue) do
|
18
|
+
exchange "exchange2"
|
19
|
+
end
|
20
|
+
queue_on_exchange1.connect do |queue|
|
21
|
+
queue.push "message1"
|
22
|
+
end
|
23
|
+
queue_on_exchange1.connect do |queue|
|
24
|
+
message = queue.pop
|
25
|
+
message.payload.should == "message1"
|
26
|
+
message.exchange.should == "exchange1"
|
27
|
+
end
|
28
|
+
queue_on_exchange2.connect do |queue|
|
29
|
+
queue.pop.should be_nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "clear" do
|
35
|
+
it "clears the queue" do
|
36
|
+
ExampleQueue.connect do |queue|
|
37
|
+
queue.push("message")
|
38
|
+
queue.clear
|
39
|
+
queue.pop.should == nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "pop" do
|
45
|
+
it "automatically acks if :auto_ack is not passed in" do
|
46
|
+
ExampleQueue.connect do |queue|
|
47
|
+
queue.push("message")
|
48
|
+
message = queue.pop
|
49
|
+
message.payload.should == "message"
|
50
|
+
end
|
51
|
+
ExampleQueue.connect do |queue|
|
52
|
+
message = queue.pop
|
53
|
+
message.should == nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it "will not automatically ack if :auto_ack is set to false" do
|
58
|
+
ExampleQueue.connect do |queue|
|
59
|
+
queue.push("message")
|
60
|
+
message = queue.pop(:auto_ack => false)
|
61
|
+
message.payload.should == "message"
|
62
|
+
end
|
63
|
+
ExampleQueue.connect do |queue|
|
64
|
+
message = queue.pop(:auto_ack => false)
|
65
|
+
message.payload.should == "message"
|
66
|
+
queue.ack(message)
|
67
|
+
end
|
68
|
+
ExampleQueue.connect do |queue|
|
69
|
+
message = queue.pop(:auto_ack => false)
|
70
|
+
message.should == nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "qsagi"
|
2
|
+
|
3
|
+
class ExampleQueue
|
4
|
+
include Qsagi::Queue
|
5
|
+
|
6
|
+
def self.host
|
7
|
+
"127.0.0.1"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.port
|
11
|
+
5672
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.queue_name
|
15
|
+
"qsagi_testing"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
RSpec.configure do |c|
|
20
|
+
c.before(:each) do
|
21
|
+
client = Bunny.new(:host => ExampleQueue.host, :port => ExampleQueue.port)
|
22
|
+
client.start
|
23
|
+
queue = client.queue(ExampleQueue.queue_name, :durable => true, :arguments => {"x-ha-policy" => "all"})
|
24
|
+
queue.delete rescue nil
|
25
|
+
client.send(:close_socket)
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: qsagi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Braintree
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-10-08 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bunny
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.8.0
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: json
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.7.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
description: A friendly way to talk to RabbitMQ
|
38
|
+
email:
|
39
|
+
- code@getbraintree.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- LICENSE.txt
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- lib/qsagi.rb
|
53
|
+
- lib/qsagi/default_serializer.rb
|
54
|
+
- lib/qsagi/json_serializer.rb
|
55
|
+
- lib/qsagi/message.rb
|
56
|
+
- lib/qsagi/queue.rb
|
57
|
+
- lib/qsagi/version.rb
|
58
|
+
- qsagi.gemspec
|
59
|
+
- spec/qsagi/json_serializer_spec.rb
|
60
|
+
- spec/qsagi/message_spec.rb
|
61
|
+
- spec/qsagi/queue_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
homepage: https://github.com/braintree/qsagi
|
64
|
+
licenses: []
|
65
|
+
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.24
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: A friendly way to talk to RabbitMQ
|
90
|
+
test_files:
|
91
|
+
- spec/qsagi/json_serializer_spec.rb
|
92
|
+
- spec/qsagi/message_spec.rb
|
93
|
+
- spec/qsagi/queue_spec.rb
|
94
|
+
- spec/spec_helper.rb
|