burrow 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 16010405f0c88acaf6ad8debb6f7b8ef41c78bbd
4
+ data.tar.gz: 30cc5337ac3e94ed1522323c74d8c2c8e0eb98b2
5
+ SHA512:
6
+ metadata.gz: 7f2dee63036cce37584b02b871a43567c9e39dac55e4f76ce9ffbf1148d755a3c9f47a2e5c3591a0022f4d6242b5f996419e26249f790c142a40cd1d31ce7f0c
7
+ data.tar.gz: 0163ca4d96c1d5969aa5619796a1c7f7ed52bad9cccfd31244f2645ce29b24b446c39aeea611c65eebd5ef05c9af5d7e4d0ec6215f0c7aee805ac50e4481e102
@@ -0,0 +1,24 @@
1
+ .ruby-version
2
+ .ruby-gemset
3
+ *.gem
4
+ *.rbc
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+ *.bundle
21
+ *.so
22
+ *.o
23
+ *.a
24
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in burrow.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Christopher Dell
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.
@@ -0,0 +1,48 @@
1
+ # Burrow
2
+
3
+ This gem builds on top of the `bunny` gem and aims to remove as much of
4
+ the boilerplate code as possible.
5
+
6
+ **This gem is what it is, do not expect amazing support here!**
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'burrow'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install burrow
21
+
22
+ ## Usage
23
+
24
+ Set up a server and handle incoming messages :
25
+
26
+ ```ruby
27
+ server = Burrow::Server.new('my_queue')
28
+ server.subscribe do |method, params|
29
+ if method == 'my_method'
30
+ {foo: 'bar', baz: 'biz'}
31
+ end
32
+ end
33
+ ```
34
+
35
+ Set up a client and send messages :
36
+
37
+ ```ruby
38
+ client = Burrow::Client.new('my_queue')
39
+ json = client.publish('my_method', first_param: 'one', second_param: 'two')
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( https://github.com/[my-github-username]/burrow/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'burrow/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "burrow"
8
+ spec.version = Burrow::VERSION
9
+ spec.authors = ["Christopher Dell"]
10
+ spec.email = ["chris@tigrish.com"]
11
+ spec.summary = %q{A wrapper to the bunny gem that removes the need for a lot of boilerplate code}
12
+ spec.description = %q{A wrapper to the bunny gem that removes the need for a lot of boilerplate code}
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 "bunny"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,5 @@
1
+ require 'json'
2
+ require 'bunny'
3
+
4
+ project_root = File.dirname(File.absolute_path(__FILE__))
5
+ Dir.glob(project_root + '/burrow/*', &method(:require))
@@ -0,0 +1,36 @@
1
+ class Burrow::Client
2
+ attr_reader :connection, :request
3
+
4
+ def initialize(queue)
5
+ @connection = Burrow::Connection.new(queue)
6
+ end
7
+
8
+ def publish(method, params={})
9
+ @request = Burrow::Request.new(method, params)
10
+ publish_request
11
+ subscribe_response
12
+ end
13
+
14
+ protected
15
+
16
+ def publish_request
17
+ connection.exchange.publish(
18
+ request.json, {
19
+ correlation_id: request.id,
20
+ reply_to: connection.return_queue.name,
21
+ routing_key: connection.queue.name
22
+ }
23
+ )
24
+ end
25
+
26
+ def subscribe_response
27
+ response = nil
28
+ connection.return_queue.subscribe(block: true) do |delivery_info, properties, payload|
29
+ if properties[:correlation_id] == request.id
30
+ response = payload
31
+ delivery_info.consumer.cancel
32
+ end
33
+ end
34
+ JSON.parse(response)
35
+ end
36
+ end
@@ -0,0 +1,31 @@
1
+ class Burrow::Connection
2
+ attr_reader :queue_name
3
+
4
+ def initialize(queue_name)
5
+ @queue_name = queue_name
6
+ end
7
+
8
+ def connection
9
+ @connection ||= begin
10
+ c = Bunny.new
11
+ c.start
12
+ c
13
+ end
14
+ end
15
+
16
+ def channel
17
+ @channel ||= connection.create_channel
18
+ end
19
+
20
+ def queue
21
+ @queue ||= channel.queue(queue_name, auto_delete: false)
22
+ end
23
+
24
+ def exchange
25
+ @exchange ||= channel.default_exchange
26
+ end
27
+
28
+ def return_queue
29
+ @return_queue ||= channel.queue('', exclusive: true)
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ class Burrow::Request
2
+ attr_reader :method, :params
3
+
4
+ def initialize(method, params={})
5
+ @method = method
6
+ @params = params
7
+ end
8
+
9
+ def id
10
+ @id ||= SecureRandom.hex
11
+ end
12
+
13
+ def attributes
14
+ { jsonrpc: '2.0',
15
+ id: id,
16
+ method: method,
17
+ params: params
18
+ }
19
+ end
20
+
21
+ def json
22
+ JSON.generate(attributes)
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ class Burrow::Response
2
+ attr_reader :id, :params
3
+
4
+ def initialize(id, params)
5
+ @id = id
6
+ @params = params
7
+ end
8
+
9
+ def json
10
+ JSON.generate(attributes)
11
+ end
12
+
13
+ def attributes
14
+ {
15
+ id: id,
16
+ result: params,
17
+ jsonrpc: '2.0'
18
+ }
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ class Burrow::Server
2
+ attr_reader :connection
3
+
4
+ def initialize(queue)
5
+ @connection = Burrow::Connection.new(queue)
6
+ end
7
+
8
+ def subscribe
9
+ connection.queue.subscribe(block: true) do |delivery_info, properties, payload|
10
+ request = JSON.parse(payload)
11
+ result = yield [request['method'], request['params']]
12
+ response = Burrow::Response.new(request['id'], result)
13
+ publish_response(response, properties)
14
+ end
15
+ end
16
+
17
+ protected
18
+
19
+ def publish_response(response, properties)
20
+ connection.exchange.publish(
21
+ response.json, {
22
+ routing_key: properties.reply_to,
23
+ correlation_id: properties.correlation_id
24
+ }
25
+ )
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module Burrow
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: burrow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Dell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunny
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A wrapper to the bunny gem that removes the need for a lot of boilerplate
56
+ code
57
+ email:
58
+ - chris@tigrish.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - burrow.gemspec
69
+ - lib/burrow.rb
70
+ - lib/burrow/client.rb
71
+ - lib/burrow/connection.rb
72
+ - lib/burrow/request.rb
73
+ - lib/burrow/response.rb
74
+ - lib/burrow/server.rb
75
+ - lib/burrow/version.rb
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A wrapper to the bunny gem that removes the need for a lot of boilerplate
100
+ code
101
+ test_files: []