hey-pubsub 0.0.4 → 0.0.5

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: 1ad31b662949d67bd450ad5ded270916cbf41f16
4
- data.tar.gz: 09ca24e92b72f232d5e13f9e4a0aac3626b70006
3
+ metadata.gz: 47678c0e57e35fa02d880ddb05d0905f5a1fdb35
4
+ data.tar.gz: 1675e2ed28ae1f7a7f9b7211d647f7e34cac8998
5
5
  SHA512:
6
- metadata.gz: fb9033c52499b8d13d84f9737b910ee09ca6e7fac44e0c45ceb1054f9e410d87f1a20a4199ddd7a8662f7c98ffd51e5a4c4e18748339e72923b6bc1903b305d1
7
- data.tar.gz: 97a3eec4d667df0ad59dfcc84ba8b8e66823ce37a98ecd1f804be675ecf3e727b9308101d3880aabd26e33b7073097d9c0a90a01ba84f9b035063b7344d7b160
6
+ metadata.gz: 220af4af52b73ce75ceb3aa127002750d077b3283c2aebb69e42a72351a9ce2ea721cf8bb9bdbfd73af44a7d245675e68fc6d927b01c557ee748b01ca63cac5c
7
+ data.tar.gz: 33a541aefdef8390cbf7faf0783c451e7b7c5f5ba072e01f7319621164f20234a40b2b27cff44a9b46b3d5e4300e614e20bfe07964b04656fa17611995b26743
@@ -5,8 +5,16 @@ module Hey::Pubsub::Adapters
5
5
  def self.subscribe!(event_name)
6
6
  if block_given?
7
7
  ActiveSupport::Notifications.subscribe(event_name) do |*args|
8
- event = ActiveSupport::Notifications::Event.new(*args)
9
- yield(event.payload)
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,
13
+ started_at: asn_event.time,
14
+ ended_at: asn_event.end,
15
+ metadata: payload)
16
+
17
+ yield(event.to_hash)
10
18
  end
11
19
  end
12
20
  end
@@ -0,0 +1,28 @@
1
+ class Hey::Pubsub::Event
2
+ def initialize(name:, uuid:, started_at:, ended_at:, metadata: {})
3
+ @name = name
4
+ @uuid = uuid
5
+ @started_at = started_at
6
+ @ended_at = ended_at
7
+ @metadata = metadata
8
+ end
9
+
10
+ def to_hash
11
+ {
12
+ uuid: uuid,
13
+ name: name,
14
+ started_at: started_at,
15
+ ended_at: ended_at,
16
+ duration: duration,
17
+ metadata: metadata
18
+ }
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :name, :uuid, :metadata, :started_at, :ended_at
24
+
25
+ def duration
26
+ @duration ||= 1000.0 * (ended_at - started_at)
27
+ end
28
+ end
@@ -2,15 +2,12 @@ class Hey::Pubsub::Payload
2
2
  def initialize(values = {})
3
3
  @values = values
4
4
  merge_values!
5
- sanitize_values!
6
5
  end
7
6
 
8
- def to_hash
9
- values
7
+ def to_h
8
+ Hey::SanitizedHash.new(values).to_h
10
9
  end
11
10
 
12
- alias_method :to_h, :to_hash
13
-
14
11
  private
15
12
 
16
13
  attr_accessor :values
@@ -19,25 +16,4 @@ class Hey::Pubsub::Payload
19
16
  Hey::ThreadCargo.uuid # initialize if it has never been set
20
17
  self.values = Hey::ThreadCargo.to_hash.merge(values)
21
18
  end
22
-
23
- def sanitize_values!
24
- traverse_hash(values) { |k, v| [k, sanitize_value!(v)] }
25
- end
26
-
27
- def sanitize_value!(value)
28
- Hey::ThreadCargo.sanitizable_values.each do |sanitizable_value|
29
- value = value.to_s.gsub(sanitizable_value, "")
30
- end
31
- value
32
- end
33
-
34
- def traverse_hash(h, &block)
35
- h.each_pair do |k, v|
36
- if v.is_a?(Hash)
37
- traverse_hash(v, &block)
38
- else
39
- yield(k, v)
40
- end
41
- end
42
- end
43
19
  end
@@ -0,0 +1,35 @@
1
+ class Hey::SanitizedHash
2
+ def initialize(hash = {})
3
+ @hash = deep_dup(hash)
4
+ end
5
+
6
+ def to_h
7
+ traverse(hash) { |k, v| [k, sanitize!(v)] }
8
+ end
9
+
10
+ private
11
+
12
+ attr_reader :hash
13
+
14
+ def sanitizable_values
15
+ @sanitizable_values ||= Hey::ThreadCargo.sanitizable_values.collect { |value| [value, ""] }.to_h
16
+ end
17
+
18
+ def sanitize!(value)
19
+ Hey::ThreadCargo.sanitizable_values.each { |substr| value.to_s.gsub!(substr, "") }
20
+ end
21
+
22
+ def traverse(h, &block)
23
+ h.each_pair do |k, v|
24
+ if v.is_a?(Hash)
25
+ traverse(v, &block)
26
+ else
27
+ yield(k, v)
28
+ end
29
+ end
30
+ end
31
+
32
+ def deep_dup(object)
33
+ Marshal.load(Marshal.dump(object))
34
+ end
35
+ end
data/lib/hey/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hey
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/hey.rb CHANGED
@@ -18,8 +18,8 @@ module Hey
18
18
  pubsub_adapter.publish!(event_name, payload.to_h, &block)
19
19
  end
20
20
 
21
- def self.subscribe!(event_name)
22
- pubsub_adapter.subscribe!(event_name)
21
+ def self.subscribe!(event_name, &block)
22
+ pubsub_adapter.subscribe!(event_name, &block)
23
23
  end
24
24
 
25
25
  def self.set(name, value)
@@ -38,6 +38,8 @@ end
38
38
  require "securerandom"
39
39
  require "hey/configuration"
40
40
  require "hey/thread_cargo"
41
+ require "hey/sanitized_hash"
41
42
  require "hey/pubsub"
42
43
  require "hey/pubsub/payload"
44
+ require "hey/pubsub/event"
43
45
  require "hey/pubsub/adapters/asn_adapter"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hey-pubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - ShippingEasy
@@ -87,7 +87,9 @@ files:
87
87
  - lib/hey/configuration.rb
88
88
  - lib/hey/pubsub.rb
89
89
  - lib/hey/pubsub/adapters/asn_adapter.rb
90
+ - lib/hey/pubsub/event.rb
90
91
  - lib/hey/pubsub/payload.rb
92
+ - lib/hey/sanitized_hash.rb
91
93
  - lib/hey/thread_cargo.rb
92
94
  - lib/hey/version.rb
93
95
  homepage: https://github.com/ShippingEasy/hey-pubsub
@@ -110,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
112
  version: '0'
111
113
  requirements: []
112
114
  rubyforge_project:
113
- rubygems_version: 2.2.2
115
+ rubygems_version: 2.4.6
114
116
  signing_key:
115
117
  specification_version: 4
116
118
  summary: Pubsub wrapper with utilities to chain events, sanitize payloads and record