hey-pubsub 0.0.8 → 0.1.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: 17e52d47a26ddc8bc246c9a529de490e5296b34e
4
- data.tar.gz: b2802f7e7e17ea5177e1f4a8827bc68891365aad
3
+ metadata.gz: 590724616c1051ab959c4ac95e4a645118456819
4
+ data.tar.gz: b72160d7af916bad08377c2838928210bbc54d4f
5
5
  SHA512:
6
- metadata.gz: 7f41afce64925bcfba2f0c476f916b15d37269aa72a8b517ae33dbeceb8aaf10e20556073e39d14faa1bf5ee27ec5493e7332b48a9e395f92be3839f68b17386
7
- data.tar.gz: 97c26cd93623698941e0cc0e4a75445f53586b88b443a2108785662d0c8ee56f1904fe4e5c38ebbb96f77e3e27f2f1b89a70147e03c7bb0f95f20f454d0e7f4e
6
+ metadata.gz: 4bb1fdfd774c144b7f9a84db60e3d406244efd9fa105333a195b6f7534511b4f0169193e3cfcf73e308856c173d4ca46f54e5c1a74fa18f38903ba53c91ee589
7
+ data.tar.gz: a04bff268858dee84d39c54400ccd1b833108b97589ded246e17c78d342c18374818b85467c04e5f338b835e92c99e1bd295d324f5c239f14b69c5aad04ec6eb
@@ -3,6 +3,5 @@ class Hey::Configuration
3
3
 
4
4
  def initialize
5
5
  @pubsub_adapter = Hey::Pubsub::Adapters::AsnAdapter
6
- @namespace = "pubsub"
7
6
  end
8
7
  end
@@ -0,0 +1 @@
1
+ class Hey::Context < OpenStruct; end
@@ -28,9 +28,8 @@ class Hey::Pubsub::Event
28
28
  end
29
29
 
30
30
  def metadata
31
- merged_data = Hey::ThreadCargo.to_h.merge(@metadata)
32
- merged_data.delete(:uuid)
33
- merged_data.delete(Hey::ThreadCargo::SANITIZABLE_VALUES_KEY)
31
+ context_metadata = Hey::ThreadCargo.contexts.reverse.map(&:to_h).reduce(Hash.new, :merge)
32
+ merged_data = context_metadata.merge(@metadata)
34
33
  Hey::SanitizedHash.new(merged_data).to_h
35
34
  end
36
35
 
@@ -12,11 +12,13 @@ class Hey::SanitizedHash
12
12
  attr_reader :hash
13
13
 
14
14
  def sanitizable_values
15
- @sanitizable_values ||= Hey::ThreadCargo.sanitizable_values.collect { |value| [value, ""] }.to_h
15
+ @sanitizable_values ||= Hey::ThreadCargo.contexts.map do |context|
16
+ Array[context[Hey::SANITIZE_KEY]]
17
+ end.flatten.compact
16
18
  end
17
19
 
18
20
  def sanitize!(value)
19
- Hey::ThreadCargo.sanitizable_values.each { |substr| value.to_s.gsub!(substr, "") }
21
+ sanitizable_values.each { |substr| value.to_s.gsub!(substr, "") }
20
22
  end
21
23
 
22
24
  def traverse(h, &block)
@@ -1,29 +1,29 @@
1
1
  class Hey::ThreadCargo
2
- SANITIZABLE_VALUES_KEY = :sanitizable_values
3
-
4
- # Sets a namespaced value on the current thread
5
- def self.set(name, value)
2
+ def self.init!
6
3
  Thread.current[:hey] = {} if Thread.current[:hey].nil?
7
- Thread.current[:hey][name] = value
8
- value
4
+ Thread.current[:hey][:uuid] = SecureRandom.uuid if Thread.current[:hey][:uuid].nil?
9
5
  end
10
6
 
11
- # Returns a namespaced value from the current thread
12
- def self.get(name)
13
- return nil if Thread.current[:hey].nil?
14
- Thread.current[:hey][name]
7
+ def self.init_contexts!
8
+ init!
9
+ Thread.current[:hey][:contexts] = [] if Thread.current[:hey][:contexts].nil?
15
10
  end
16
11
 
17
12
  def self.uuid
18
- return set(:uuid, SecureRandom.uuid) if get(:uuid).nil?
19
- get(:uuid)
13
+ init!
14
+ Thread.current[:hey][:uuid]
15
+ end
16
+
17
+ # Sets a context on the current thread
18
+ def self.add_context(context)
19
+ self.init_contexts!
20
+ Thread.current[:hey][:contexts] << context
20
21
  end
21
22
 
22
- # Adds the supplied values to the sanitized values array. It removes nils and duplicate values from the array.
23
- def self.sanitize!(*values)
24
- set(SANITIZABLE_VALUES_KEY, []) if get(SANITIZABLE_VALUES_KEY).nil?
25
- values = Array(values)
26
- set(SANITIZABLE_VALUES_KEY, get(SANITIZABLE_VALUES_KEY).concat(values).flatten.compact.uniq)
23
+ # Returns a context from the current thread
24
+ def self.remove_context(context)
25
+ self.init_contexts!
26
+ Thread.current[:hey][:contexts].delete(context)
27
27
  end
28
28
 
29
29
  # Removes all namespaced values from the current thread.
@@ -31,12 +31,8 @@ class Hey::ThreadCargo
31
31
  Thread.current[:hey] = nil
32
32
  end
33
33
 
34
- def self.sanitizable_values
35
- Array(get(SANITIZABLE_VALUES_KEY))
36
- end
37
-
38
- def self.to_h
39
- Thread.current[:hey] = {} if Thread.current[:hey].nil?
40
- Thread.current[:hey].clone
34
+ def self.contexts
35
+ self.init_contexts!
36
+ Thread.current[:hey][:contexts]
41
37
  end
42
38
  end
data/lib/hey/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hey
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/hey.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  require "hey/version"
2
+ require "ostruct"
3
+ require "securerandom"
2
4
 
3
5
  module Hey
6
+ SANITIZE_KEY = :sanitize
7
+
4
8
  def self.configure
5
9
  yield(configuration)
6
10
  end
@@ -24,20 +28,21 @@ module Hey
24
28
  pubsub_adapter.subscribe!(event_name, &block)
25
29
  end
26
30
 
27
- def set(name, value)
28
- Hey::ThreadCargo.set(name, value)
29
- end
30
-
31
- def sanitize!(*values)
32
- Hey::ThreadCargo.sanitize!(values)
31
+ def context(**hash)
32
+ _context = Context.new(hash)
33
+ ThreadCargo.add_context(_context)
34
+ yield
35
+ ensure
36
+ ThreadCargo.remove_context(_context)
37
+ ThreadCargo.purge! if ThreadCargo.contexts.empty?
33
38
  end
34
39
  end
35
40
 
36
41
  extend Hey::Behavior
37
42
  end
38
43
 
39
- require "securerandom"
40
44
  require "hey/configuration"
45
+ require "hey/context"
41
46
  require "hey/thread_cargo"
42
47
  require "hey/sanitized_hash"
43
48
  require "hey/pubsub"
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.8
4
+ version: 0.1.0
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-21 00:00:00.000000000 Z
11
+ date: 2015-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,6 +85,7 @@ files:
85
85
  - hey-pubsub.gemspec
86
86
  - lib/hey.rb
87
87
  - lib/hey/configuration.rb
88
+ - lib/hey/context.rb
88
89
  - lib/hey/pubsub.rb
89
90
  - lib/hey/pubsub/adapters/asn_adapter.rb
90
91
  - lib/hey/pubsub/event.rb
@@ -111,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
112
  version: '0'
112
113
  requirements: []
113
114
  rubyforge_project:
114
- rubygems_version: 2.4.6
115
+ rubygems_version: 2.2.2
115
116
  signing_key:
116
117
  specification_version: 4
117
118
  summary: Pubsub wrapper with utilities to chain events, sanitize payloads and record