philotic 0.8.1 → 1.0.1
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 +7 -7
- data/.travis.yml +5 -6
- data/README.md +10 -13
- data/examples/creating_named_queues/manually.rb +4 -4
- data/examples/creating_named_queues/with_rake.rb +4 -1
- data/examples/publishing/publish.rb +14 -14
- data/examples/simple_instance.rb +4 -4
- data/examples/simple_singleton.rb +3 -3
- data/examples/subscribing/acks.rb +6 -6
- data/examples/subscribing/anonymous_queue.rb +2 -2
- data/examples/subscribing/consumer.rb +47 -0
- data/examples/subscribing/multiple_named_queues.rb +4 -4
- data/examples/subscribing/named_queue.rb +3 -3
- data/lib/philotic.rb +2 -3
- data/lib/philotic/config.rb +1 -1
- data/lib/philotic/connection.rb +1 -0
- data/lib/philotic/constants.rb +1 -1
- data/lib/philotic/consumer.rb +93 -0
- data/lib/philotic/{dummy_event.rb → dummy_message.rb} +3 -3
- data/lib/philotic/logging.rb +1 -1
- data/lib/philotic/logging/logger.rb +6 -6
- data/lib/philotic/logging/{event.rb → message.rb} +2 -2
- data/lib/philotic/message.rb +146 -0
- data/lib/philotic/publisher.rb +24 -24
- data/lib/philotic/subscriber.rb +9 -12
- data/lib/philotic/version.rb +1 -1
- data/philotic.gemspec +8 -11
- data/philotic_queues.yml.example +8 -8
- data/spec/philotic/connection_spec.rb +2 -0
- data/spec/philotic/consumer_spec.rb +186 -0
- data/spec/philotic/logging/logger_spec.rb +15 -15
- data/spec/philotic/message_spec.rb +147 -0
- data/spec/philotic/publisher_spec.rb +39 -38
- data/spec/philotic/subscriber_spec.rb +6 -3
- metadata +173 -144
- data/lib/philotic/event.rb +0 -100
- data/lib/philotic/routable.rb +0 -98
- data/spec/philotic/event_spec.rb +0 -109
- data/spec/philotic/routable_spec.rb +0 -54
data/lib/philotic/event.rb
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
require 'philotic/constants'
|
2
|
-
require 'philotic/routable'
|
3
|
-
require 'philotic/singleton'
|
4
|
-
|
5
|
-
module Philotic
|
6
|
-
class Event
|
7
|
-
include Philotic::Routable
|
8
|
-
|
9
|
-
attr_accessor :connection, :publish_error
|
10
|
-
attr_writer :published
|
11
|
-
|
12
|
-
def initialize(routables={}, payloads={}, connection=nil)
|
13
|
-
self.timestamp = Time.now.to_i
|
14
|
-
self.philotic_firehose = true
|
15
|
-
self.connection = connection
|
16
|
-
|
17
|
-
# dynamically insert any passed in routables into both attr_routable
|
18
|
-
# and attr_payload
|
19
|
-
# result: ability to arbitrarily send a easily routable hash
|
20
|
-
# over into the bus
|
21
|
-
_set_routables_or_payloads(:routable, routables)
|
22
|
-
_set_routables_or_payloads(:payload, payloads)
|
23
|
-
|
24
|
-
@published = false
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.inherited(sub)
|
28
|
-
Philotic::PHILOTIC_HEADERS.each do |header|
|
29
|
-
sub.attr_routable header
|
30
|
-
end
|
31
|
-
self.attr_routable_readers.dup.each do |routable|
|
32
|
-
sub.attr_routable routable
|
33
|
-
end
|
34
|
-
|
35
|
-
self.attr_payload_readers.dup.each do |payload|
|
36
|
-
sub.attr_payload payload
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
self.inherited(self)
|
41
|
-
|
42
|
-
Philotic::MESSAGE_OPTIONS.each do |message_option|
|
43
|
-
attr_reader message_option
|
44
|
-
define_method :"#{message_option}=" do |val|
|
45
|
-
instance_variable_set(:"@#{message_option}", val)
|
46
|
-
self.message_metadata[message_option] = val
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def connection
|
51
|
-
@connection ||= Philotic.connection
|
52
|
-
end
|
53
|
-
|
54
|
-
def published?
|
55
|
-
!!@published
|
56
|
-
end
|
57
|
-
|
58
|
-
def publish
|
59
|
-
connection.publish self
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.publish(*args)
|
63
|
-
self.new(*args).publish
|
64
|
-
end
|
65
|
-
|
66
|
-
private
|
67
|
-
|
68
|
-
def _set_routables_or_payloads(type, attrs)
|
69
|
-
attrs.each do |key, value|
|
70
|
-
if self.respond_to?(:"#{key}=")
|
71
|
-
send(:"#{key}=", value)
|
72
|
-
elsif self.class == Philotic::Event
|
73
|
-
_set_event_attribute(type, key, value)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def _set_event_attribute(type, key, value)
|
79
|
-
_set_event_attribute_setter(key, type, value)
|
80
|
-
_set_event_attribute_getter(key, type)
|
81
|
-
end
|
82
|
-
|
83
|
-
def _set_event_attribute_getter(key, type)
|
84
|
-
self.class.send("attr_#{type}_readers").concat([key])
|
85
|
-
getter = lambda do
|
86
|
-
instance_variable_get(:"@#{key}")
|
87
|
-
end
|
88
|
-
self.class.send :define_method, :"#{key}", getter
|
89
|
-
end
|
90
|
-
|
91
|
-
def _set_event_attribute_setter(key, type, value)
|
92
|
-
self.class.send("attr_#{type}_writers").concat([:"#{key}="])
|
93
|
-
setter = lambda do |v|
|
94
|
-
instance_variable_set(:"@#{key}", v)
|
95
|
-
end
|
96
|
-
self.class.send :define_method, :"#{key}=", setter
|
97
|
-
self.send(:"#{key}=", value)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
data/lib/philotic/routable.rb
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
require 'active_support/all'
|
2
|
-
require 'active_record'
|
3
|
-
|
4
|
-
module Philotic
|
5
|
-
module Routable
|
6
|
-
def self.included(base)
|
7
|
-
base.send :include, ActiveRecord::Validations
|
8
|
-
base.send :include, ActiveRecord::Callbacks
|
9
|
-
base.validates :philotic_firehose, :philotic_product, :philotic_component, :philotic_event_type, presence: true
|
10
|
-
|
11
|
-
base.extend ClassMethods
|
12
|
-
end
|
13
|
-
|
14
|
-
module ClassMethods
|
15
|
-
def attr_payload_reader *names
|
16
|
-
attr_payload_readers.concat(names)
|
17
|
-
attr_reader(*names)
|
18
|
-
end
|
19
|
-
|
20
|
-
def attr_payload_readers
|
21
|
-
@attr_payload_readers ||= []
|
22
|
-
end
|
23
|
-
|
24
|
-
def attr_payload_writer *names
|
25
|
-
attr_payload_writers.concat names
|
26
|
-
attr_writer(*names)
|
27
|
-
end
|
28
|
-
|
29
|
-
def attr_payload_writers
|
30
|
-
@attr_payload_writers ||= []
|
31
|
-
end
|
32
|
-
|
33
|
-
def attr_payload *names
|
34
|
-
names -= attr_payload_readers
|
35
|
-
attr_payload_readers.concat(names)
|
36
|
-
attr_payload_writers.concat(names)
|
37
|
-
attr_accessor(*names)
|
38
|
-
end
|
39
|
-
|
40
|
-
def attr_routable_reader *names
|
41
|
-
attr_routable_reader.concat(names)
|
42
|
-
attr_reader(*names)
|
43
|
-
end
|
44
|
-
|
45
|
-
def attr_routable_readers
|
46
|
-
@attr_routable_readers ||= []
|
47
|
-
end
|
48
|
-
|
49
|
-
def attr_routable_writers
|
50
|
-
@attr_routable_writers ||= []
|
51
|
-
end
|
52
|
-
|
53
|
-
def attr_routable_writer *names
|
54
|
-
attr_routable_writers.concat names
|
55
|
-
attr_writer(*names)
|
56
|
-
end
|
57
|
-
|
58
|
-
def attr_routable *names
|
59
|
-
names -= attr_routable_readers
|
60
|
-
attr_routable_readers.concat(names)
|
61
|
-
attr_routable_writers.concat(names)
|
62
|
-
attr_accessor(*names)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def payload
|
67
|
-
_payload_or_headers(:payload)
|
68
|
-
end
|
69
|
-
|
70
|
-
def headers
|
71
|
-
_payload_or_headers(:routable)
|
72
|
-
end
|
73
|
-
|
74
|
-
def attributes
|
75
|
-
payload.merge headers
|
76
|
-
end
|
77
|
-
|
78
|
-
def message_metadata
|
79
|
-
@message_metadata ||= {}
|
80
|
-
end
|
81
|
-
|
82
|
-
def message_metadata= options
|
83
|
-
@message_metadata ||= {}
|
84
|
-
@message_metadata.merge! options
|
85
|
-
end
|
86
|
-
|
87
|
-
private
|
88
|
-
|
89
|
-
def _payload_or_headers(payload_or_headers)
|
90
|
-
attribute_hash = {}
|
91
|
-
self.class.send("attr_#{payload_or_headers}_readers").each do |attr|
|
92
|
-
attr = attr.to_sym
|
93
|
-
attribute_hash[attr] = send(attr)
|
94
|
-
end
|
95
|
-
attribute_hash
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
data/spec/philotic/event_spec.rb
DELETED
@@ -1,109 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
# create 'deep' inheritance to test self.inherited
|
4
|
-
class TestEventParent < Philotic::Event
|
5
|
-
end
|
6
|
-
class TestEvent < TestEventParent
|
7
|
-
end
|
8
|
-
|
9
|
-
describe Philotic::Event do
|
10
|
-
let(:event) { TestEvent.new }
|
11
|
-
subject { event }
|
12
|
-
|
13
|
-
Philotic::Routable::ClassMethods.instance_methods.sort.each do |method_name|
|
14
|
-
specify { expect(subject.class.methods).to include method_name.to_sym }
|
15
|
-
end
|
16
|
-
|
17
|
-
Philotic::MESSAGE_OPTIONS.each do |method_name|
|
18
|
-
specify { expect(subject.methods).to include method_name.to_sym }
|
19
|
-
specify { expect(subject.methods).to include "#{method_name}=".to_sym }
|
20
|
-
end
|
21
|
-
|
22
|
-
Philotic::PHILOTIC_HEADERS.each do |method_name|
|
23
|
-
specify { expect(subject.methods).to include method_name.to_sym }
|
24
|
-
specify { expect(subject.methods).to include "#{method_name}=".to_sym }
|
25
|
-
end
|
26
|
-
|
27
|
-
describe 'message_metadata' do
|
28
|
-
it 'should have a timestamp' do
|
29
|
-
Timecop.freeze
|
30
|
-
expect(subject.message_metadata).to eq(timestamp: Time.now.to_i)
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'should reflect changes in the event properties' do
|
34
|
-
expect(subject.message_metadata[:app_id]).to eq nil
|
35
|
-
subject.app_id = 'ANSIBLE'
|
36
|
-
expect(subject.message_metadata[:app_id]).to eq 'ANSIBLE'
|
37
|
-
end
|
38
|
-
end
|
39
|
-
describe 'headers' do
|
40
|
-
it 'should include :philotic_product' do
|
41
|
-
expect(subject.headers.keys).to include :philotic_product
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context 'generic event' do
|
46
|
-
let(:headers) do
|
47
|
-
{
|
48
|
-
header1: 'h1',
|
49
|
-
header2: 'h2',
|
50
|
-
header3: 'h3',
|
51
|
-
}
|
52
|
-
end
|
53
|
-
|
54
|
-
let(:payloads) do
|
55
|
-
{
|
56
|
-
payload1: 'h1',
|
57
|
-
payload2: 'h2',
|
58
|
-
payload3: 'h3',
|
59
|
-
}
|
60
|
-
end
|
61
|
-
it 'builds an event with dynamic headers and payloads' do
|
62
|
-
event = Philotic::Event.new(headers, payloads)
|
63
|
-
|
64
|
-
expect(event.headers).to include(headers)
|
65
|
-
expect(event.payload).to eq payloads
|
66
|
-
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
describe '#publish' do
|
71
|
-
subject { Philotic::Event.new }
|
72
|
-
specify do
|
73
|
-
expect(subject.connection).to receive(:publish).with(subject)
|
74
|
-
|
75
|
-
subject.publish
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
describe '.publish' do
|
81
|
-
let (:connection) { double }
|
82
|
-
let(:headers) do
|
83
|
-
{
|
84
|
-
header1: 'h1',
|
85
|
-
header2: 'h2',
|
86
|
-
header3: 'h3',
|
87
|
-
}
|
88
|
-
end
|
89
|
-
|
90
|
-
let(:payloads) do
|
91
|
-
{
|
92
|
-
payload1: 'h1',
|
93
|
-
payload2: 'h2',
|
94
|
-
payload3: 'h3',
|
95
|
-
}
|
96
|
-
end
|
97
|
-
subject { Philotic::Event }
|
98
|
-
specify do
|
99
|
-
expect_any_instance_of(Philotic::Event).to receive(:connection).and_return(connection)
|
100
|
-
expect(connection).to receive(:publish) do |event|
|
101
|
-
expect(event.headers).to include(headers)
|
102
|
-
expect(event.payload).to eq payloads
|
103
|
-
end
|
104
|
-
|
105
|
-
subject.publish(headers, payloads)
|
106
|
-
end
|
107
|
-
|
108
|
-
end
|
109
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Philotic::Routable do
|
4
|
-
context 'including the module on class' do
|
5
|
-
let(:routable_event_class) {
|
6
|
-
Class.new do
|
7
|
-
include Philotic::Routable
|
8
|
-
attr_routable :routable_attr
|
9
|
-
attr_payload :payload_attr
|
10
|
-
end
|
11
|
-
}
|
12
|
-
subject { routable_event_class }
|
13
|
-
|
14
|
-
%w{ attr_payload_reader attr_payload_readers
|
15
|
-
attr_payload_writer attr_payload_writers
|
16
|
-
attr_payload
|
17
|
-
|
18
|
-
attr_routable_reader attr_routable_readers
|
19
|
-
attr_routable_writers attr_routable_writer
|
20
|
-
attr_routable }.each do |method_name|
|
21
|
-
specify { expect(subject.methods).to include method_name.to_sym }
|
22
|
-
end
|
23
|
-
|
24
|
-
context ' and then instantiating it' do
|
25
|
-
let(:routable_event_instance) { routable_event_class.new }
|
26
|
-
subject { routable_event_instance }
|
27
|
-
|
28
|
-
it 'should have proper headers' do
|
29
|
-
expect(subject.headers).to eq({routable_attr: nil})
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should have proper payload' do
|
33
|
-
expect(subject.payload).to eq({payload_attr: nil})
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'should have proper attributes' do
|
37
|
-
expect(subject.attributes).to eq({routable_attr: nil, payload_attr: nil})
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'should have empty message_metadata' do
|
41
|
-
expect(subject.message_metadata).to eq({})
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'overriding a value with message_metadata=' do
|
45
|
-
before do
|
46
|
-
routable_event_instance.message_metadata = {mandatory: false}
|
47
|
-
end
|
48
|
-
it 'should work' do
|
49
|
-
expect(subject.message_metadata).to eq(mandatory: false)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|