philotic 0.4.0 → 0.5.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
  SHA1:
3
- metadata.gz: 158377a2a7f0458ec444e07ab7aab54225911480
4
- data.tar.gz: 510ad20cb672b6d3294fbde7d71f4afd42a19500
3
+ metadata.gz: b72b8c6dc5ab1be2da45998cf70ebe908ad3099d
4
+ data.tar.gz: 8b6473eb588bdbe69d4b401b4b149cdcecb416b9
5
5
  SHA512:
6
- metadata.gz: 8fe5d2caff540d1eacfb224c5de104ed7bdf8769a13a8bc99afe6748e70368ce0c13b57bbb92d90efa7e38d4455b867a0c51d834cf3b4c79625fa6b0bf9cdd14
7
- data.tar.gz: 713a02825e3bdc0d8eb62f0a185508a8de8bd3df8a9c3b431ff4b5213e7177b8f4a1a40431c1e478c8edcb185801670c09cf33461c41ce57f0861054227c299d
6
+ metadata.gz: 5a95fc1ea9e1729aff8752d68b50168e41d8ed621271c2a00809ec3e714ef68813defcb137febb57b4978df085a22da3b1a39e67b38e878f2de99d41b72e52eb
7
+ data.tar.gz: 96fb09160928634ec14693512aadd19c99237993d7e608bcf850ced33006a75ca8216ed60da6e6571e2c29b2a6e3c9363b649979578f8584e082b4a2c1358596
data/README.md CHANGED
@@ -8,8 +8,35 @@ Lightweight, opinionated wrapper for using RabbitMQ headers exchanges
8
8
  [![Test Coverage](https://codeclimate.com/github/nkeyes/philotic/badges/coverage.svg)](https://codeclimate.com/github/nkeyes/philotic)
9
9
 
10
10
  Check out the [examples](https://github.com/nkeyes/philotic/tree/master/examples).
11
+ ## Examples
12
+ ### Using `Philotic` as a singleton
13
+ ```Ruby
14
+ require 'philotic'
15
+ require 'awesome_print'
16
+
17
+ # override the message return handler
18
+ Philotic.config.message_return_handler = lambda do |basic_return, metadata, message|
19
+ Philotic.logger.warn "Message returned. reply_text: #{basic_return.reply_text}"
20
+ end
21
+
22
+ Philotic.subscriber.subscribe(header_key: 'header_1') do |metadata, message|
23
+ ap message[:attributes]
24
+ end
25
+
26
+ # normally we'd do:
27
+ #
28
+ # Philotic.subscriber.endure
29
+ #
30
+ # to keep the parent thread alive while the subscribers do their thing
31
+ # but this infinite publish loop takes care of that
32
+ loop do
33
+ Philotic::Event.publish({header_key: "header_#{[1, 2].sample}"}, {payload_key: 'payload_value'})
34
+ # only send a message every two seconds so we can see whats going on
35
+ sleep 2
36
+ end
37
+ ```
11
38
 
12
- ## Simple Example
39
+ ### Using an instance of `Philotic::Connection`
13
40
  ```Ruby
14
41
  require 'philotic'
15
42
  require 'awesome_print'
@@ -4,13 +4,11 @@ $stdout.sync = true
4
4
 
5
5
  require 'philotic'
6
6
 
7
- philotic = Philotic::Connection.new
8
-
9
7
  # explicitly create named queues for this example
10
- # philotic.config.initialize_named_queues must be truthy to run Philotic.initialize_named_queue!
11
- philotic.config.initialize_named_queues = true
8
+ # Philotic.config.initialize_named_queues must be truthy to run Philotic.initialize_named_queue!
9
+ Philotic.config.initialize_named_queues = true
12
10
 
13
- philotic.initialize_named_queue!('male_queue', bindings: [{:'x-match' => 'all', gender: :M, available: true}])
14
- philotic.initialize_named_queue!('female_queue', bindings: [{:'x-match' => 'all', gender: :F, available: true}])
15
- philotic.initialize_named_queue!('test_queue', bindings: [{ :'x-match' => 'any', gender: :M, available: true }])
16
- philotic.initialize_named_queue!('flaky_queue', bindings: [{ :'x-match' => 'any', gender: :M, available: true }])
11
+ Philotic.initialize_named_queue!('male_queue', bindings: [{:'x-match' => 'all', gender: :M, available: true}])
12
+ Philotic.initialize_named_queue!('female_queue', bindings: [{:'x-match' => 'all', gender: :F, available: true}])
13
+ Philotic.initialize_named_queue!('test_queue', bindings: [{ :'x-match' => 'any', gender: :M, available: true }])
14
+ Philotic.initialize_named_queue!('flaky_queue', bindings: [{ :'x-match' => 'any', gender: :M, available: true }])
@@ -5,9 +5,8 @@ $stdout.sync = true
5
5
  require 'philotic'
6
6
  require 'philotic/dummy_event'
7
7
 
8
- @philotic = Philotic::Connection.new
9
8
 
10
- @philotic.publisher.logger.level = Logger::WARN
9
+ Philotic.logger.level = Logger::WARN
11
10
 
12
11
  @event = Philotic::DummyEvent.new
13
12
 
@@ -26,7 +25,7 @@ def send_message number
26
25
  @event.gender = [:F, :M].sample
27
26
  @event.message = "Message #{number}: Hey #{@event.gender == :M ? 'dude' : 'dudette'}"
28
27
 
29
- @philotic.publish @event
28
+ Philotic.publish @event
30
29
 
31
30
  end
32
31
 
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+ $stdout.sync = true
4
+
5
+ require 'philotic'
6
+ require 'awesome_print'
7
+
8
+ # override the message return handler
9
+ Philotic.config.message_return_handler = lambda do |basic_return, metadata, message|
10
+ Philotic.logger.warn "Message returned. reply_text: #{basic_return.reply_text}"
11
+ end
12
+
13
+ Philotic.subscriber.subscribe(header_key: 'header_1') do |metadata, message|
14
+ ap message[:attributes]
15
+ end
16
+
17
+ # normally we'd do:
18
+ #
19
+ # Philotic.subscriber.endure
20
+ #
21
+ # to keep the parent thread alive while the subscribers do their thing
22
+ # but this infinite publish loop takes care of that
23
+ loop do
24
+ Philotic::Event.publish({header_key: "header_#{[1, 2].sample}"}, {payload_key: 'payload_value'})
25
+ # only send a message every two seconds so we can see whats going on
26
+ sleep 2
27
+ end
@@ -5,24 +5,21 @@ $stdout.sync = true
5
5
  require 'philotic'
6
6
  require 'awesome_print'
7
7
 
8
- philotic = Philotic::Connection.new
9
-
10
-
11
8
  # sometimes ack
12
- philotic.subscriber.subscribe('flaky_queue', ack: true) do |metadata, message|
9
+ Philotic.subscriber.subscribe('flaky_queue', ack: true) do |metadata, message|
13
10
  ap message[:attributes]
14
- [true, false].sample ? philotic.subscriber.acknowledge(message) : philotic.subscriber.reject(message)
11
+ [true, false].sample ? Philotic.subscriber.acknowledge(message) : Philotic.subscriber.reject(message)
15
12
  end
16
13
 
17
14
  # always ack
18
- philotic.subscriber.subscribe('flaky_queue', ack: true) do |metadata, message|
15
+ Philotic.subscriber.subscribe('flaky_queue', ack: true) do |metadata, message|
19
16
  ap message[:attributes]
20
- philotic.subscriber.acknowledge(message, true)
17
+ Philotic.subscriber.acknowledge(message, true)
21
18
  end
22
19
 
23
20
  # always reject
24
- philotic.subscriber.subscribe('flaky_queue', ack: true) do |metadata, message|
21
+ Philotic.subscriber.subscribe('flaky_queue', ack: true) do |metadata, message|
25
22
  ap message[:attributes]
26
- philotic.subscriber.reject message
23
+ Philotic.subscriber.reject message
27
24
  end
28
- philotic.subscriber.endure
25
+ Philotic.subscriber.endure
@@ -5,13 +5,10 @@ $stdout.sync = true
5
5
  require 'philotic'
6
6
  require 'awesome_print'
7
7
 
8
- philotic = Philotic::Connection.new
8
+ Philotic.config.load_file(File.join(File.dirname(__FILE__), '../../', 'Philotic.yml.example'))
9
9
 
10
- philotic.config.load_file(File.join(File.dirname(__FILE__), '../../', 'philotic.yml.example'))
11
-
12
-
13
- philotic.subscriber.subscribe(philotic_firehose: true) do |metadata, message|
10
+ Philotic.subscriber.subscribe(philotic_firehose: true) do |metadata, message|
14
11
  ap message[:attributes]
15
12
  end
16
13
 
17
- philotic.subscriber.endure
14
+ Philotic.subscriber.endure
@@ -4,17 +4,15 @@ $stdout.sync = true
4
4
 
5
5
  require 'philotic'
6
6
  require 'awesome_print'
7
- philotic = Philotic::Connection.new
8
7
 
8
+ Philotic.config.load_file(File.join(File.dirname(__FILE__), '../../', 'Philotic.yml.example'))
9
9
 
10
- philotic.config.load_file(File.join(File.dirname(__FILE__), '../../', 'philotic.yml.example'))
11
-
12
- philotic.subscriber.subscribe('female_queue') do |metadata, message|
10
+ Philotic.subscriber.subscribe('female_queue') do |metadata, message|
13
11
  ap message[:attributes]
14
12
  end
15
13
 
16
- philotic.subscriber.subscribe('male_queue') do |metadata, message|
14
+ Philotic.subscriber.subscribe('male_queue') do |metadata, message|
17
15
  ap message[:attributes]
18
16
  end
19
17
 
20
- philotic.subscriber.endure
18
+ Philotic.subscriber.endure
@@ -5,10 +5,8 @@ $stdout.sync = true
5
5
  require 'philotic'
6
6
  require 'awesome_print'
7
7
 
8
- philotic = Philotic::Connection.new
9
-
10
- philotic.subscriber.subscribe('test_queue') do |metadata, message|
8
+ Philotic.subscriber.subscribe('test_queue') do |metadata, message|
11
9
  ap message[:attributes]
12
10
  end
13
11
 
14
- philotic.subscriber.endure
12
+ Philotic.subscriber.endure
data/lib/philotic.rb CHANGED
@@ -1,36 +1,19 @@
1
1
  require 'active_support/all'
2
2
  require 'pathname'
3
3
 
4
- require 'philotic/constants'
5
- require 'philotic/connection'
6
-
7
-
8
4
  module Philotic
9
5
  class << self
10
- extend Forwardable
11
-
12
6
  def root
13
7
  ::Pathname.new File.expand_path('../../', __FILE__)
14
8
  end
15
-
16
- def env
17
- ENV['SERVICE_ENV'] || 'development'
18
- end
19
-
20
- def connection
21
- @connection ||= Philotic::Connection.new
22
- end
23
-
24
- def method_missing(method, *args, &block)
25
- connection.send(method, *args, &block)
26
- end
27
-
28
- def_delegators :connection, *(Philotic::Connection.public_instance_methods(false) - [:connection])
29
-
30
9
  end
31
10
 
32
11
  end
33
12
 
13
+ require 'philotic/constants'
14
+ require 'philotic/singleton'
15
+
16
+ require 'philotic/connection'
34
17
  require 'philotic/version'
35
18
  require 'philotic/config'
36
19
  require 'philotic/routable'
@@ -2,6 +2,7 @@ require 'json'
2
2
  require 'bunny'
3
3
  require 'logger'
4
4
 
5
+ require 'philotic/constants'
5
6
  require 'philotic/config'
6
7
  require 'philotic/publisher'
7
8
  require 'philotic/subscriber'
@@ -119,7 +120,7 @@ module Philotic
119
120
  end
120
121
 
121
122
  def queue_from_config(queue_name, config)
122
- queue_options = DEFAULT_NAMED_QUEUE_OPTIONS.dup
123
+ queue_options = Philotic::DEFAULT_NAMED_QUEUE_OPTIONS.dup
123
124
  queue_options.merge!(config[:options] || {})
124
125
 
125
126
  channel.queue(queue_name, queue_options).tap do
@@ -1,5 +1,7 @@
1
+ require 'philotic/event'
2
+
1
3
  module Philotic
2
- class DummyEvent < Event
4
+ class DummyEvent < Philotic::Event
3
5
  attr_payload :subject
4
6
  attr_payload :message
5
7
  attr_routable :gender
@@ -1,7 +1,6 @@
1
- require 'philotic'
2
1
  require 'philotic/constants'
3
- require 'philotic/connection'
4
2
  require 'philotic/routable'
3
+ require 'philotic/singleton'
5
4
 
6
5
  module Philotic
7
6
  class Event
@@ -1,4 +1,4 @@
1
- require 'philotic/connection'
1
+ require 'philotic/constants'
2
2
 
3
3
  module Philotic
4
4
  class Publisher
@@ -0,0 +1,13 @@
1
+ require 'philotic/connection'
2
+
3
+ module Philotic
4
+ class << self
5
+ extend Forwardable
6
+
7
+ def connection
8
+ @connection ||= Philotic::Connection.new
9
+ end
10
+
11
+ def_delegators :connection, *(Philotic::Connection.public_instance_methods(false) - [:connection])
12
+ end
13
+ end
@@ -1,3 +1,5 @@
1
+ require 'philotic/constants'
2
+
1
3
  module Philotic
2
4
  class Subscriber
3
5
  class Metadata
@@ -5,17 +5,14 @@ namespace :philotic do
5
5
 
6
6
  require 'philotic'
7
7
 
8
- philotic = Philotic::Connection.new
9
-
10
8
  # philotic.config.initialize_named_queues must be truthy to run Philotic.initialize_named_queue!
11
- philotic.config.initialize_named_queues = true
9
+ Philotic.config.initialize_named_queues = true
12
10
 
13
11
 
14
12
  @filename = args[:filename]
15
13
  queues = YAML.load_file(@filename)
16
- philotic.connect!
17
14
  queues.each_pair do |queue_name, queue_options|
18
- philotic.initialize_named_queue!(queue_name, queue_options)
15
+ Philotic.initialize_named_queue!(queue_name, queue_options)
19
16
  end
20
17
  end
21
18
  end
@@ -1,3 +1,3 @@
1
1
  module Philotic
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -13,7 +13,6 @@ SimpleCov.start do
13
13
  end
14
14
  require 'bundler/setup'
15
15
  require 'rspec/its'
16
- require 'philotic'
17
16
  require 'timecop'
18
17
 
19
18
  Bundler.require(:default, :test)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philotic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Keyes
@@ -169,7 +169,8 @@ files:
169
169
  - examples/creating_named_queues/manually.rb
170
170
  - examples/creating_named_queues/with_rake.rb
171
171
  - examples/publishing/publish.rb
172
- - examples/simple_combined_example.rb
172
+ - examples/simple_instance.rb
173
+ - examples/simple_singleton.rb
173
174
  - examples/subscribing/acks.rb
174
175
  - examples/subscribing/anonymous_queue.rb
175
176
  - examples/subscribing/multiple_named_queues.rb
@@ -185,6 +186,7 @@ files:
185
186
  - lib/philotic/logging/logger.rb
186
187
  - lib/philotic/publisher.rb
187
188
  - lib/philotic/routable.rb
189
+ - lib/philotic/singleton.rb
188
190
  - lib/philotic/subscriber.rb
189
191
  - lib/philotic/tasks.rb
190
192
  - lib/philotic/tasks/init_queues.rb