hey-pubsub 0.0.5 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -14
- data/hey-pubsub.gemspec +1 -1
- data/lib/hey/configuration.rb +2 -1
- data/lib/hey/pubsub/adapters/asn_adapter.rb +6 -9
- data/lib/hey/pubsub/event.rb +31 -14
- data/lib/hey/thread_cargo.rb +2 -14
- data/lib/hey/version.rb +1 -1
- data/lib/hey.rb +15 -16
- metadata +3 -4
- data/lib/hey/pubsub/payload.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db65819c7adc7ad9daaa79ce3a84a7f8a8d14d13
|
4
|
+
data.tar.gz: e8e130a00d678bc0bb8e3d432106b5682dcea1d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7bfdd6b8a72804d692e6bdf5501db7ca5fdfb5934a0221d2d250a6315ade1411851d735462f3c2391ceac57c8cb8cb2462d7a4ad56f98790c76eb0dd4da56bc
|
7
|
+
data.tar.gz: dcf7cf396f92c79b9e484523cc9ebcc3f60f7eea1ea345194a727a7b49b092d9f6ecb44515220493868177cd78598b515b5953a012093725a32bb82a0d47ee56
|
data/README.md
CHANGED
@@ -5,7 +5,6 @@ some convenience utilities to:
|
|
5
5
|
|
6
6
|
* Track a chain of events
|
7
7
|
* Sanitize sensitive data from event payloads
|
8
|
-
* Record who originally kicked off the chain of events (the actor)
|
9
8
|
* Set arbitrary data that will be included on every event published on the same thread
|
10
9
|
|
11
10
|
## Installation
|
@@ -42,19 +41,6 @@ your code.
|
|
42
41
|
|
43
42
|
Hey provides utilities to share metadata across all events published on the same thread.
|
44
43
|
|
45
|
-
### Setting the current actor
|
46
|
-
|
47
|
-
It's often useful to know who kicked off a chain of events, whether it's a user, employee or the system itself via
|
48
|
-
some automated process. We call this entity the "current actor".
|
49
|
-
|
50
|
-
As soon as you know who the current actor is for a a business process, you should set it:
|
51
|
-
|
52
|
-
```ruby
|
53
|
-
Hey.set_current_actor!(id: 13234, type: "Employee", name: "Jack Ship")
|
54
|
-
```
|
55
|
-
|
56
|
-
Any events published for the life of the current thread with include `current_actor` in their payloads.
|
57
|
-
|
58
44
|
### Event chain UUID
|
59
45
|
|
60
46
|
The first time an event is published via Hey a UUID will be assigned and stored on the current thread.
|
@@ -85,6 +71,11 @@ If you need to set arbitrary values to be included on every event payload for th
|
|
85
71
|
```ruby
|
86
72
|
Hey.set(:ip_address, "127.0.0.1")
|
87
73
|
```
|
74
|
+
|
75
|
+
### Writing customer setters
|
76
|
+
|
77
|
+
TODO
|
78
|
+
|
88
79
|
## Event publishing and subscribing
|
89
80
|
|
90
81
|
### Adapters
|
data/hey-pubsub.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["ShippingEasy"]
|
10
10
|
spec.email = ["dev@shippingeasy.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{Pubsub wrapper with utilities to chain events, sanitize payloads and record the
|
12
|
+
spec.summary = %q{Pubsub wrapper with utilities to chain events, sanitize payloads and record data on all events on the same thread.}
|
13
13
|
spec.homepage = "https://github.com/ShippingEasy/hey-pubsub"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
data/lib/hey/configuration.rb
CHANGED
@@ -6,26 +6,23 @@ module Hey::Pubsub::Adapters
|
|
6
6
|
if block_given?
|
7
7
|
ActiveSupport::Notifications.subscribe(event_name) do |*args|
|
8
8
|
asn_event = ActiveSupport::Notifications::Event.new(*args)
|
9
|
-
|
10
|
-
payload = asn_event.payload.dup
|
11
|
-
event = Hey::Pubsub::Event.new(uuid: payload.delete(:uuid),
|
12
|
-
name: asn_event.name,
|
9
|
+
event = Hey::Pubsub::Event.new(name: asn_event.name,
|
13
10
|
started_at: asn_event.time,
|
14
11
|
ended_at: asn_event.end,
|
15
|
-
metadata: payload)
|
12
|
+
metadata: asn_event.payload.dup)
|
16
13
|
|
17
|
-
yield(event.
|
14
|
+
yield(event.to_h)
|
18
15
|
end
|
19
16
|
end
|
20
17
|
end
|
21
18
|
|
22
|
-
def self.publish!(
|
19
|
+
def self.publish!(event)
|
23
20
|
if block_given?
|
24
|
-
ActiveSupport::Notifications.instrument(
|
21
|
+
ActiveSupport::Notifications.instrument(event.name, event.metadata) do
|
25
22
|
yield
|
26
23
|
end
|
27
24
|
else
|
28
|
-
ActiveSupport::Notifications.instrument(
|
25
|
+
ActiveSupport::Notifications.instrument(event.name, event.metadata)
|
29
26
|
end
|
30
27
|
end
|
31
28
|
end
|
data/lib/hey/pubsub/event.rb
CHANGED
@@ -1,28 +1,45 @@
|
|
1
1
|
class Hey::Pubsub::Event
|
2
|
-
|
2
|
+
attr_writer :name, :metadata, :started_at, :ended_at
|
3
|
+
|
4
|
+
def initialize(name:, started_at: nil, ended_at: nil, metadata: {})
|
3
5
|
@name = name
|
4
|
-
@uuid = uuid
|
5
6
|
@started_at = started_at
|
6
7
|
@ended_at = ended_at
|
7
8
|
@metadata = metadata
|
8
9
|
end
|
9
10
|
|
10
|
-
def
|
11
|
-
{
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
11
|
+
def to_h
|
12
|
+
hash = { uuid: Hey::ThreadCargo.uuid, name: name, metadata: metadata }
|
13
|
+
hash[:started_at] = started_at unless started_at.nil?
|
14
|
+
hash[:ended_at] = ended_at unless ended_at.nil?
|
15
|
+
hash[:duration] = duration unless duration.nil?
|
16
|
+
hash
|
17
|
+
end
|
18
|
+
|
19
|
+
def started_at
|
20
|
+
return if @started_at.nil?
|
21
|
+
@started_at.strftime("%Y-%m-%dT%H:%M:%S.%L")
|
19
22
|
end
|
20
23
|
|
21
|
-
|
24
|
+
def ended_at
|
25
|
+
return if @ended_at.nil?
|
26
|
+
@ended_at.strftime("%Y-%m-%dT%H:%M:%S.%L")
|
27
|
+
end
|
22
28
|
|
23
|
-
|
29
|
+
def metadata
|
30
|
+
merged_data = Hey::ThreadCargo.to_h.merge(@metadata)
|
31
|
+
merged_data.delete(:uuid)
|
32
|
+
merged_data.delete(Hey::ThreadCargo::SANITIZABLE_VALUES_KEY)
|
33
|
+
Hey::SanitizedHash.new(merged_data).to_h
|
34
|
+
end
|
35
|
+
|
36
|
+
def name
|
37
|
+
return @name if Hey.configuration.namespace.nil?
|
38
|
+
"#{Hey.configuration.namespace}.#{@name}"
|
39
|
+
end
|
24
40
|
|
25
41
|
def duration
|
26
|
-
|
42
|
+
return if @ended_at.nil? || @started_at.nil?
|
43
|
+
1000.0 * (@ended_at - @started_at)
|
27
44
|
end
|
28
45
|
end
|
data/lib/hey/thread_cargo.rb
CHANGED
@@ -14,21 +14,11 @@ class Hey::ThreadCargo
|
|
14
14
|
Thread.current[:hey][name]
|
15
15
|
end
|
16
16
|
|
17
|
-
# Returns the actor from the current thread
|
18
|
-
def self.current_actor
|
19
|
-
get(:current_actor)
|
20
|
-
end
|
21
|
-
|
22
17
|
def self.uuid
|
23
18
|
return set(:uuid, SecureRandom.uuid) if get(:uuid).nil?
|
24
19
|
get(:uuid)
|
25
20
|
end
|
26
21
|
|
27
|
-
# Sets the actor to the current thread
|
28
|
-
def self.set_current_actor(name:, type: nil, id: nil)
|
29
|
-
set(:current_actor, { name: name, type: type, id: id})
|
30
|
-
end
|
31
|
-
|
32
22
|
# Adds the supplied values to the sanitized values array. It removes nils and duplicate values from the array.
|
33
23
|
def self.sanitize!(*values)
|
34
24
|
set(SANITIZABLE_VALUES_KEY, []) if get(SANITIZABLE_VALUES_KEY).nil?
|
@@ -45,10 +35,8 @@ class Hey::ThreadCargo
|
|
45
35
|
Array(get(SANITIZABLE_VALUES_KEY))
|
46
36
|
end
|
47
37
|
|
48
|
-
def self.
|
38
|
+
def self.to_h
|
49
39
|
Thread.current[:hey] = {} if Thread.current[:hey].nil?
|
50
|
-
|
51
|
-
hash.delete(SANITIZABLE_VALUES_KEY)
|
52
|
-
hash
|
40
|
+
Thread.current[:hey].clone
|
53
41
|
end
|
54
42
|
end
|
data/lib/hey/version.rb
CHANGED
data/lib/hey.rb
CHANGED
@@ -13,26 +13,26 @@ module Hey
|
|
13
13
|
configuration.pubsub_adapter
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
payload =
|
18
|
-
|
19
|
-
|
16
|
+
module Behavior
|
17
|
+
def publish!(event_name, payload = {}, &block)
|
18
|
+
event = Hey::Pubsub::Event.new(name: event_name, metadata: payload)
|
19
|
+
pubsub_adapter.publish!(event, &block)
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
def subscribe!(event_name, &block)
|
23
|
+
pubsub_adapter.subscribe!(event_name, &block)
|
24
|
+
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
def set(name, value)
|
27
|
+
Hey::ThreadCargo.set(name, value)
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
30
|
+
def sanitize!(*values)
|
31
|
+
Hey::ThreadCargo.sanitize!(values)
|
32
|
+
end
|
31
33
|
end
|
32
34
|
|
33
|
-
|
34
|
-
Hey::ThreadCargo.sanitize!(values)
|
35
|
-
end
|
35
|
+
extend Hey::Behavior
|
36
36
|
end
|
37
37
|
|
38
38
|
require "securerandom"
|
@@ -40,6 +40,5 @@ require "hey/configuration"
|
|
40
40
|
require "hey/thread_cargo"
|
41
41
|
require "hey/sanitized_hash"
|
42
42
|
require "hey/pubsub"
|
43
|
-
require "hey/pubsub/payload"
|
44
43
|
require "hey/pubsub/event"
|
45
44
|
require "hey/pubsub/adapters/asn_adapter"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hey-pubsub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ShippingEasy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -88,7 +88,6 @@ files:
|
|
88
88
|
- lib/hey/pubsub.rb
|
89
89
|
- lib/hey/pubsub/adapters/asn_adapter.rb
|
90
90
|
- lib/hey/pubsub/event.rb
|
91
|
-
- lib/hey/pubsub/payload.rb
|
92
91
|
- lib/hey/sanitized_hash.rb
|
93
92
|
- lib/hey/thread_cargo.rb
|
94
93
|
- lib/hey/version.rb
|
@@ -116,5 +115,5 @@ rubygems_version: 2.4.6
|
|
116
115
|
signing_key:
|
117
116
|
specification_version: 4
|
118
117
|
summary: Pubsub wrapper with utilities to chain events, sanitize payloads and record
|
119
|
-
the
|
118
|
+
data on all events on the same thread.
|
120
119
|
test_files: []
|
data/lib/hey/pubsub/payload.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
class Hey::Pubsub::Payload
|
2
|
-
def initialize(values = {})
|
3
|
-
@values = values
|
4
|
-
merge_values!
|
5
|
-
end
|
6
|
-
|
7
|
-
def to_h
|
8
|
-
Hey::SanitizedHash.new(values).to_h
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
attr_accessor :values
|
14
|
-
|
15
|
-
def merge_values!
|
16
|
-
Hey::ThreadCargo.uuid # initialize if it has never been set
|
17
|
-
self.values = Hey::ThreadCargo.to_hash.merge(values)
|
18
|
-
end
|
19
|
-
end
|