rack-amqp-client 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30238824d47466532279b8b590398aae97cb2509
4
+ data.tar.gz: ae448f75f2c128b8d763589a0ec9a34b82becfa5
5
+ SHA512:
6
+ metadata.gz: 5155f4e74042b4bab9bfa70535316e5e750e509a53f93e334df57890ea7694cbcd1050eb9cdc72655c584ee37312001b2d86565aa2bbe1072c9b499879365851
7
+ data.tar.gz: fddbb1638cb849b8db3fa95cf2838e47f9c0cc5d43e28cecbbaa5b5405f19fe69c6bd0d31c29d37b0f6683a3d0d7116d485218335b21c4e862bf9ca7abe77fe3
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require emoji-rspec
2
+ --format hearts
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - 1.9.2
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ - ruby-head
9
+ - jruby-head
10
+ - rbx
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # 0.0.2
2
+
3
+ * initial version
4
+ * Not 0.0.1 because Josh forgot to fill in the homepage gem attribute ;)
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ platforms :rbx do
6
+ gem 'racc'
7
+ gem 'rubysl', '~> 2.0'
8
+ gem 'psych'
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Optoro, Inc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Joshua Szmajda
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,37 @@
1
+ # Rack::Amqp::Client
2
+
3
+ An AMQP-HTTP ruby client that simplifies following convention
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-amqp-client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-amqp-client
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ client = Rack::AMQP::Client.client(host: 'localhost') # host is your AMQP broker, like RabbitMQ or whatever
23
+
24
+ # First param is the queue and http path, etc; second is options
25
+ response = client.request('test.simple/users.json', {http_method: 'GET', headers: {}})
26
+
27
+ response.payload
28
+ # => "[{\"id\":1,\"login\":\"someguy\",\"password\":\"awesomenesssss\",\"created_at\":\"2013-12-07T17:11:45.518Z\",\"updated_at\":\"2013-12-07T17:11:45.518Z\"},{\"id\":2,\"login\":\"Hi\",\"password\":\"There\",\"created_at\":\"2014-03-30T18:31:21.106Z\",\"updated_at\":\"2014-03-30T18:31:21.106Z\"}]"
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,37 @@
1
+ require 'bunny'
2
+ require 'singleton'
3
+
4
+ module Rack
5
+ module AMQP
6
+ module Client
7
+
8
+ def self.with_client(*args, &block)
9
+ yield client(*args)
10
+ end
11
+
12
+ def self.client(*args)
13
+ Synchronizer.instance.client(*args)
14
+ end
15
+
16
+ class Synchronizer
17
+ include Singleton
18
+ def initialize
19
+ @mutex = Mutex.new
20
+ super
21
+ end
22
+
23
+ def client(*args)
24
+ @mutex.synchronize do # TODO this probably doesn't help anything here
25
+ @mgr ||= Manager.new(*args)
26
+ end
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+
34
+ require "rack/amqp/client/request"
35
+ require "rack/amqp/client/response"
36
+ require "rack/amqp/client/manager"
37
+ require "rack/amqp/client/version"
@@ -0,0 +1,66 @@
1
+ module Rack
2
+ module AMQP
3
+ module Client
4
+ class Manager
5
+ attr_accessor :connection_options, :amqp_channel, :amqp_client
6
+
7
+ def initialize(broker_connection_options)
8
+ connect!(broker_connection_options)
9
+ @correlation_id = 0
10
+ @incomplete_requests = []
11
+ @mutex = Mutex.new
12
+ end
13
+
14
+ # TODO this method needs to be refactored
15
+ def request(uri, options={})
16
+ http_method = options[:http_method]
17
+ timeout = options[:timeout] || 5
18
+
19
+ body = options[:body] || ""
20
+ headers = {
21
+ 'Content-Type' => 'application/x-www-form-urlencoded',
22
+ 'Content-Length' => body.length
23
+ }.merge(options[:headers])
24
+
25
+ request = Request.new((@correlation_id += 1).to_s, http_method, uri, body, headers)
26
+ @mutex.synchronize { @incomplete_requests << request }
27
+ callback_queue = create_callback_queue
28
+ request.callback_queue = callback_queue
29
+
30
+ amqp_channel.direct('').publish(request.payload, request.publishing_options)
31
+
32
+ response = request.reply_wait(timeout)
33
+ response
34
+ end
35
+
36
+ private
37
+
38
+ def create_callback_queue
39
+ @callback_queue ||= begin
40
+ # build queue
41
+ queue = amqp_channel.queue("", auto_delete: true, exclusive: true)
42
+ queue.subscribe do |di, meta, payload|
43
+ request = nil
44
+ @mutex.synchronize do
45
+ request = @incomplete_requests.detect do |r|
46
+ r.request_id == meta[:correlation_id]
47
+ end
48
+ @incomplete_requests.delete(request)
49
+ end
50
+ request.callback(di, meta, payload)
51
+ end
52
+ # bind to an exchange, maybe later
53
+ queue
54
+ end
55
+ end
56
+
57
+ def connect!(broker_options)
58
+ self.amqp_client = Bunny.new(broker_options)
59
+ amqp_client.start
60
+ self.amqp_channel = amqp_client.create_channel
61
+ end
62
+
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,74 @@
1
+ #require 'thread'
2
+ module Rack
3
+ module AMQP
4
+ module Client
5
+ class Request
6
+
7
+ SLEEP_INCREMENT =
8
+
9
+ attr_accessor :routing_key, :request_path, :body, :request_id, :callback_queue, :http_method
10
+
11
+ def initialize(request_id, http_method, uri, body, headers={})
12
+ @callback_queue = callback_queue
13
+ @request_id = request_id
14
+ @http_method = http_method
15
+ @headers = headers
16
+ @body = body
17
+ @routing_key, @request_path = split_uri uri
18
+ @response = nil
19
+ @mutex = Mutex.new
20
+ @resource = ConditionVariable.new
21
+ end
22
+
23
+ def reply_wait(timeout)
24
+ @mutex.synchronize do
25
+ @resource.wait(@mutex, timeout)
26
+ end
27
+ resp = @response
28
+ @reponse = nil
29
+ resp
30
+ end
31
+
32
+ def callback(delivery_info, meta, payload)
33
+ @mutex.synchronize do
34
+ @resource.signal
35
+ @response = Response.new(meta, payload, delivery_info)
36
+ end
37
+ end
38
+
39
+
40
+ def publishing_options
41
+ {
42
+ mandatory: true, # receive an error on routing error
43
+ message_id: request_id,
44
+ reply_to: callback_queue.name,
45
+ type: http_method,
46
+ app_id: user_agent,
47
+ timestamp: Time.now.to_i,
48
+ headers: headers,
49
+ routing_key: routing_key
50
+ }
51
+ end
52
+
53
+ def payload
54
+ body
55
+ end
56
+
57
+ def headers
58
+ @headers.merge(path: request_path)
59
+ end
60
+
61
+ def user_agent
62
+ "rack-amqp-client-#{VERSION}"
63
+ end
64
+
65
+ def split_uri(uri)
66
+ # expecting target.queue.name/request/path?params=things&others=stuff
67
+ parts = uri.split('/', 2)
68
+ [parts[0].to_s, "/#{parts[1].to_s}"]
69
+ end
70
+
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,24 @@
1
+ module Rack
2
+ module AMQP
3
+ module Client
4
+ class Response
5
+
6
+ attr_accessor :meta, :payload, :delivery_info
7
+ def initialize(meta, payload, delivery_info)
8
+ @meta = meta
9
+ @payload = payload
10
+ @delivery_info = delivery_info
11
+ end
12
+
13
+ def headers
14
+ meta[:headers]
15
+ end
16
+
17
+ def response_code
18
+ headers['X-AMQP-HTTP-Status']
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,7 @@
1
+ module Rack
2
+ module AMQP
3
+ module Client
4
+ VERSION = "0.0.2"
5
+ end
6
+ end
7
+ 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 'rack/amqp/client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-amqp-client"
8
+ spec.version = Rack::AMQP::Client::VERSION
9
+ spec.authors = ["Joshua Szmajda", "John Nestoriak"]
10
+ spec.email = ["josh@optoro.com"]
11
+ spec.description = %q{An AMQP-HTTP ruby client}
12
+ spec.summary = %q{An AMQP-HTTP ruby client that relies on Bunny}
13
+ spec.homepage = "http://github.com/rack-amqp/rack-amqp-client"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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 "bunny"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "emoji-rspec"
27
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+
4
+ describe Rack::AMQP::Client do
5
+ before :each do
6
+ Rack::AMQP::Client::Synchronizer.instance.instance_variable_set('@mgr', nil)
7
+ end
8
+
9
+ describe "#client" do
10
+ it "returns a Manager" do
11
+ Rack::AMQP::Client::Manager.any_instance.stub(:connect!).and_return(nil)
12
+ expect(Rack::AMQP::Client.client(nil)).to be_instance_of(Rack::AMQP::Client::Manager)
13
+ end
14
+ end
15
+
16
+ describe '#with_client' do
17
+ it 'yields something' do
18
+ x = nil
19
+ Rack::AMQP::Client::Manager.any_instance.stub(:connect!).and_return(nil)
20
+ Rack::AMQP::Client.with_client({}) do |mgr|
21
+ x = mgr
22
+ end
23
+ expect(x).to_not be_nil
24
+ end
25
+
26
+ it "passes options it doesn't understand to Bunny" do
27
+ fake_bunny = double("Bunny", start: true, create_channel: true)
28
+ expect(Bunny).to receive(:new).with({foo: 'bar'}).and_return(fake_bunny)
29
+ Rack::AMQP::Client.with_client({foo: 'bar'}) {|m| }
30
+ end
31
+
32
+ it "integrates", brittle: true do
33
+ pending "Some better way to test integrations"
34
+ Timeout.timeout(3) do
35
+ Rack::AMQP::Client.with_client(host: 'localhost') do |c|
36
+ response = c.request('test.simple/users.json', {http_method: 'GET', headers: {}})
37
+ expect(response.payload).to eq("[{\"id\":1,\"login\":\"someguy\",\"password\":\"awesomenesssss\",\"created_at\":\"2013-12-07T17:11:45.518Z\",\"updated_at\":\"2013-12-07T17:11:45.518Z\"},{\"id\":2,\"login\":\"Hi\",\"password\":\"There\",\"created_at\":\"2014-03-30T18:31:21.106Z\",\"updated_at\":\"2014-03-30T18:31:21.106Z\"}]")
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require 'rack/amqp/client'
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-amqp-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Szmajda
8
+ - John Nestoriak
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-04-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bunny
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '1.3'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '1.3'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: emoji-rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: An AMQP-HTTP ruby client
99
+ email:
100
+ - josh@optoro.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .rspec
107
+ - .travis.yml
108
+ - CHANGELOG.md
109
+ - Gemfile
110
+ - LICENSE
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - lib/rack/amqp/client.rb
115
+ - lib/rack/amqp/client/manager.rb
116
+ - lib/rack/amqp/client/request.rb
117
+ - lib/rack/amqp/client/response.rb
118
+ - lib/rack/amqp/client/version.rb
119
+ - rack-amqp-client.gemspec
120
+ - spec/rack/amqp/client/client_spec.rb
121
+ - spec/spec_helper.rb
122
+ homepage: http://github.com/rack-amqp/rack-amqp-client
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.2.2
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: An AMQP-HTTP ruby client that relies on Bunny
146
+ test_files:
147
+ - spec/rack/amqp/client/client_spec.rb
148
+ - spec/spec_helper.rb