cotton-tail 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 162f785a8c6f03379f26cc784a5c4a8e28a831d1f1b1145452addb16532f04c2
4
- data.tar.gz: b5336ab860aafca2a2a15d9c6bc75aff74155bf43ee47948b2ea46688f805cdf
3
+ metadata.gz: e850fbee254829b5680d9ef8b653f7112d839340070c63133e1615d02a2717d8
4
+ data.tar.gz: bcf0010028eb6b98a227764f951b5a9b2d50a0b1cc0beface95838295dfbee48
5
5
  SHA512:
6
- metadata.gz: 1e95e2b691fbfe5ebdbc39b857fd26912015b7b8b74a0a7820540a2b5ab957ad437c06c235149cf7e8bbdadd4634007ce4ce83fc07c7e193c8996d0f71ea3e0f
7
- data.tar.gz: 21e79d79373570d5d1371d29de75e0e918060f63c0877439bdf9da23ef030ccc85009d68d0f148c585e253a09a95d6dbd6f1a620b6cb67d94bbde1e0dd16df6e
6
+ metadata.gz: 9fff4b3fec0934022cd1255e95557f9fd7e0be9011a91affcb30be960b8e5ff84c149f83ab7bc047a13909999292f57f51b32b31271c888820176e0f22ea854a
7
+ data.tar.gz: f5a658bbb3f3473fc9aac6331f76c435aa0cc297a5be6dbe783972ae895761b54eac3c5577b2d23ab6f7e4d46c6bdb62baf6fd8a57113cbf29f9022ad32420aa
checksums.yaml.gz.sig CHANGED
Binary file
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cotton-tail (0.3.0)
4
+ cotton-tail (0.4.0)
5
5
  bunny (~> 2.12)
6
6
  ibsciss-middleware (~> 0.4.2)
7
7
 
data/examples/app.rb CHANGED
@@ -20,30 +20,22 @@ app.routes.draw do
20
20
  puts 'Goodbye cruel world!'
21
21
  end
22
22
 
23
- handle 'inspect.message' do |env, routing_key, delivery_info, properties, payload|
23
+ handle 'inspect.message' do |env, request, response|
24
24
  puts env: env
25
- puts routing_key: routing_key
26
- puts delivery_info: delivery_info
27
- puts properties: properties
28
- puts payload: payload
25
+ puts request: request
26
+ puts response: response
29
27
  end
30
28
  end
31
29
 
32
30
  queue 'require_ack_queue', exclusive: true, manual_ack: true do
33
- handle 'get.acked' do |_env, _routing_key, delivery_info, _properties, _message|
34
- delivery_tag = delivery_info[:delivery_tag]
35
- puts "acking with #{delivery_tag}"
36
-
37
- ch = delivery_info[:channel]
38
- ch.ack(delivery_tag)
31
+ handle 'get.acked' do |_env, request, _response|
32
+ puts "acking with #{request.delivery_tag}"
33
+ request.channel.ack(request.delivery_tag)
39
34
  end
40
35
 
41
- handle 'get.nacked' do |_env, _routing_key, delivery_info, _properties, _message|
42
- delivery_tag = delivery_info[:delivery_tag]
43
- puts "nacking with #{delivery_tag}"
44
-
45
- ch = delivery_info[:channel]
46
- ch.nack(delivery_tag)
36
+ handle 'get.nacked' do |_env, request, _response|
37
+ puts "nacking with #{request.delivery_tag}"
38
+ request.channel.nack(request.delivery_tag)
47
39
  end
48
40
  end
49
41
  end
@@ -6,10 +6,20 @@ require 'cotton_tail'
6
6
 
7
7
  app = CottonTail::App.new
8
8
 
9
- app.config.middleware do |_b|
9
+ upcase = lambda { |(env, req, res)|
10
+ [env, req, CottonTail::Response.new(res.body.upcase)]
11
+ }
12
+
13
+ print = lambda { |(env, req, res)|
14
+ puts res.body
15
+ [env, req, res]
16
+ }
17
+
18
+ app.config.middleware do |d|
10
19
  # This is added to the end of the middleware stack
11
20
  # 'message' is the return value of the handlers defined below
12
- d.use ->(message) { puts message.upcase }
21
+ d.use upcase
22
+ d.use print
13
23
  end
14
24
 
15
25
  app.routes.draw do
@@ -3,15 +3,18 @@
3
3
  module CottonTail
4
4
  # App is the main class for a CottonTail server
5
5
  class App
6
- attr_reader :env, :config
6
+ attr_reader :env
7
7
 
8
- def initialize(queue_strategy: Queue::Bunny, env: {}, connection: Bunny.new, config: Configuration.new)
8
+ def initialize(queue_strategy: Queue::Bunny, env: {}, connection: Bunny.new)
9
9
  @dependencies = { queue_strategy: queue_strategy, connection: connection }
10
10
  @env = env
11
- @config = config
12
11
  @connection = connection.start
13
12
  end
14
13
 
14
+ def config
15
+ @config ||= Configuration.new(middleware: Middleware.default_stack(self))
16
+ end
17
+
15
18
  def queues
16
19
  routes.queues
17
20
  end
@@ -7,9 +7,9 @@ module CottonTail
7
7
  class Configuration
8
8
  attr_reader :connection_args
9
9
 
10
- def initialize
10
+ def initialize(middleware: nil)
11
11
  @connection_args = nil
12
- @middleware = Middleware::DEFAULT_STACK
12
+ @middleware = middleware
13
13
  @user_configs = {}
14
14
  end
15
15
 
@@ -29,7 +29,7 @@ module CottonTail
29
29
  return @middleware unless block_given?
30
30
 
31
31
  @middleware = ::Middleware::Builder.new do |b|
32
- b.use @middleware
32
+ b.use @middleware if @middleware
33
33
  yield b
34
34
  end
35
35
  end
@@ -4,29 +4,26 @@ module CottonTail
4
4
  module Middleware
5
5
  # Router Middleware
6
6
  class Router
7
- def initialize(app)
7
+ attr_reader :handlers
8
+
9
+ def initialize(app, handlers:)
8
10
  @app = app
11
+ @handlers = handlers
9
12
  end
10
13
 
11
- def call(request)
12
- message = parse(request)
13
- request.shift
14
- request.unshift message.app.env
15
- @app.call handler(message.app, message.routing_key).call(request)
14
+ def call(message)
15
+ env, req, = message
16
+ @app.call [env, req, response(req.routing_key, message)]
16
17
  end
17
18
 
18
19
  private
19
20
 
20
- def handler(app, route)
21
- handlers(app).fetch(route) { raise UndefinedRouteError }
22
- end
23
-
24
- def parse(msg)
25
- Message.new(*msg)
21
+ def handler(route)
22
+ handlers.fetch(route) { raise UndefinedRouteError }
26
23
  end
27
24
 
28
- def handlers(app)
29
- app.routes.handlers
25
+ def response(routing_key, message)
26
+ CottonTail::Response.new handler(routing_key).call(message)
30
27
  end
31
28
  end
32
29
  end
@@ -8,8 +8,10 @@ module CottonTail
8
8
  module Middleware
9
9
  autoload :Router, 'cotton_tail/middleware/router'
10
10
 
11
- DEFAULT_STACK = ::Middleware::Builder.new do |b|
12
- b.use Router
11
+ def self.default_stack(app)
12
+ ::Middleware::Builder.new do |b|
13
+ b.use Router, handlers: app.routes.handlers
14
+ end
13
15
  end
14
16
  end
15
17
  end
@@ -24,15 +24,13 @@ module CottonTail
24
24
  watch_source manual_ack
25
25
  end
26
26
 
27
- def push(args)
28
- routing_key, message = args
29
- bind routing_key
30
- exchange.publish message, routing_key: routing_key
27
+ def push(request)
28
+ bind request.routing_key
29
+ exchange.publish request.payload, routing_key: request.routing_key
31
30
  end
32
31
 
33
32
  def pop
34
- delivery_info, *tail = super
35
- [delivery_info[:routing_key], delivery_info, *tail]
33
+ Request.new(*super)
36
34
  end
37
35
 
38
36
  def bind(routing_key)
@@ -28,13 +28,17 @@ module CottonTail
28
28
  private
29
29
 
30
30
  def call_next
31
- args = fiber.resume
32
- middleware.call([@app, *args]) if args
31
+ request = fiber.resume
32
+ middleware.call([env, request, Response.new]) if request
33
33
  end
34
34
 
35
35
  def middleware
36
36
  @app.config.middleware
37
37
  end
38
+
39
+ def env
40
+ @app.env
41
+ end
38
42
  end
39
43
  end
40
44
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CottonTail
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
data/lib/cotton_tail.rb CHANGED
@@ -12,10 +12,21 @@ module CottonTail
12
12
  autoload :Router, 'cotton_tail/router'
13
13
  autoload :Version, 'cotton_tail/version'
14
14
 
15
- # Message is a struct for working with the messages that are passed through
16
- # the middleware stack.
17
- Message = Struct.new(:app, :routing_key, :delivery_info, :properties,
18
- :payload)
15
+ Request = Struct.new(:delivery_info, :properties, :payload) do
16
+ def routing_key
17
+ delivery_info[:routing_key]
18
+ end
19
+
20
+ def delivery_tag
21
+ delivery_info[:delivery_tag]
22
+ end
23
+
24
+ def channel
25
+ delivery_info[:channel]
26
+ end
27
+ end
28
+
29
+ Response = Struct.new(:body)
19
30
 
20
31
  class UndefinedRouteError < StandardError
21
32
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cotton-tail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Brennan
@@ -30,7 +30,7 @@ cert_chain:
30
30
  fXe/xr/Sc+2wCjHPVE2J+auN5hk3KCp1I4s2fKqyLIwyhTEF3shuYfCpC8rt/YdN
31
31
  cy9/lg5LCI3OvakzxL4Xt1Sq4h/xJZ06ydTVJ1wxfk6BXHrg
32
32
  -----END CERTIFICATE-----
33
- date: 2018-11-21 00:00:00.000000000 Z
33
+ date: 2018-11-30 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: bunny
metadata.gz.sig CHANGED
@@ -1,2 +1,2 @@
1
- �E}Lvk2E��# ͬ
2
- ����Ԉ�vj%U�DRhW'��iP��uj��"t�P�()oO��ژ������ZL��t׳�Iɯ��B��c_D���x�u����*P�%����-j��vX40��������޷�hݷ�U�ǻao�Ń�% �
1
+ L���wT�>+�Xd��g��Sc�Gܤ̧��W�15��/��t���]DkZ�Of��
2
+ �=���`�XD���`�~Z-����x��b7C��`n0z]Q�'��=`V>Bi���x��_��q��>$*���Y"m