hey-pubsub 0.1.0 → 0.2.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: 590724616c1051ab959c4ac95e4a645118456819
4
- data.tar.gz: b72160d7af916bad08377c2838928210bbc54d4f
3
+ metadata.gz: e7e86cccd486ebf7f99dcb3b3fd054ee87dc643e
4
+ data.tar.gz: a93db839d76dd98c5178ad72b4b865ba06e86902
5
5
  SHA512:
6
- metadata.gz: 4bb1fdfd774c144b7f9a84db60e3d406244efd9fa105333a195b6f7534511b4f0169193e3cfcf73e308856c173d4ca46f54e5c1a74fa18f38903ba53c91ee589
7
- data.tar.gz: a04bff268858dee84d39c54400ccd1b833108b97589ded246e17c78d342c18374818b85467c04e5f338b835e92c99e1bd295d324f5c239f14b69c5aad04ec6eb
6
+ metadata.gz: a97edb277e81c3761a444be82f8507a80c59dd4c259da5dd860ebe32cc855749f202c9a281484c7b5c5b601ddc94d2e58827ba4458a4aa491596ca48a0172562
7
+ data.tar.gz: fda8810ce7f36deed31c4c1bd96eb91d8c8dfbfc48f8fe426013d97523164c2ac3e0e24c0c7349fb5d46e433e9f7c8779ac7ff47fa3ebac8b02135cc3ecc6231
data/README.md CHANGED
@@ -51,7 +51,29 @@ chain of events to be associated later in a Kibana dashboard, for example.
51
51
  This UUID could be used across disparate systems subscribing to the same message bus, so long as the convention is
52
52
  adhered to.
53
53
 
54
- ### Sanitizing payloads
54
+ ### Contexts
55
+
56
+ Use contexts if you want to attach meta-data to all events that happen on a thread from a certain point forward.
57
+
58
+ ```ruby
59
+ Hey.context(ip_address: "127.0.0.1") do
60
+ Hey.publish!("registration.succeeded", { email: "john@ecommerce.com" })
61
+ end
62
+ ```
63
+
64
+ The payloads of all events wrapped in a context will include this extra data.
65
+
66
+ You may also nest contexts:
67
+
68
+ ```ruby
69
+ Hey.context(ip_address: "127.0.0.1") do
70
+ Hey.context(current_actor: "Jack Ship") do
71
+ Hey.publish!("registration.succeeded", { email: "john@ecommerce.com" })
72
+ end
73
+ end
74
+ ```
75
+
76
+ #### Sanitizing payloads
55
77
 
56
78
  There are times you'd like sensitive information to be stripped from event payloads. For example, if you are logging API
57
79
  requests and responses you should redact credentials before writing to a database or logfile.
@@ -60,21 +82,52 @@ It's easier to handle this sanitization during publication, since subscribers of
60
82
  values to strip. Hey provides a utility to record these sensitive values and every event published during the life of
61
83
  the current thread will redact them from their payloads.
62
84
 
85
+ You can sanitize data by using the key `sanitize` in your contexts:
86
+
87
+ ```ruby
88
+ Hey.context(sanitize: ["227-76-1234"]) do
89
+ Hey.publish!("registration.succeeded", { email: "john@ecommerce.com" })
90
+ end
91
+ ```
92
+
93
+ #### Namespacing events
94
+
95
+ You can automatically namespace your event names using contexts and a top-level namespace.
96
+
97
+ A top-level namespace can be configured like this:
98
+
63
99
  ```ruby
64
- Hey.sanitize!("ABC123", "4222222222222222", "245-65-12763")
100
+ Hey.configure do |config|
101
+ config.global_namespace = “my-app”
102
+ end
65
103
  ```
66
104
 
67
- ### Setting arbitrary data
105
+ As can a delimiter for chaining an event name together, the default of which is “:” :
106
+
107
+ ```ruby
108
+ Hey.configure do |config|
109
+ config.delimiter = “/”
110
+ end
111
+ ```
68
112
 
69
- If you need to set arbitrary values to be included on every event payload for the life of a thread, you can:
113
+ With this configuration, an event’s name would look like this:
70
114
 
71
115
  ```ruby
72
- Hey.set(:ip_address, "127.0.0.1")
116
+ Hey.publish!("registered", { email: "john@ecommerce.com" })
117
+ => "my-app/registered"
73
118
  ```
74
119
 
75
- ### Writing customer setters
120
+ You can also set a namespace on contexts and they will automatically be chained together in the order they are nested:
76
121
 
77
- TODO
122
+ ```ruby
123
+ Hey.context(namespace: “foo”) do
124
+ Hey.context(namespace: “bar”) do
125
+ Hey.publish!(“hooray”)
126
+ end
127
+ end
128
+
129
+ => "foo:bar:hooray"
130
+ ```
78
131
 
79
132
  ## Event publishing and subscribing
80
133
 
@@ -127,6 +180,19 @@ Hey.subscribe!("registration.succeeded") do |payload|
127
180
  end
128
181
  ```
129
182
 
183
+ ## Middleware
184
+
185
+ A faraday middleware adapter is provided to automatically broadcast all requests and responses.
186
+
187
+ ```ruby
188
+ faraday_connection = ::Faraday.new(url: base_url) do |faraday|
189
+ faraday.use Hey::Pubsub::Middleware::Faraday
190
+ faraday.adapter ::Faraday.default_adapter
191
+ end
192
+ ```
193
+
194
+ It will broadcast the event names "request" and "response", so be sure to use contexts and namespacing to make the event names more meaningful.
195
+
130
196
  ## Development
131
197
 
132
198
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,7 +1,8 @@
1
1
  class Hey::Configuration
2
- attr_accessor :pubsub_adapter, :namespace
2
+ attr_accessor :pubsub_adapter, :global_namespace, :delimiter
3
3
 
4
4
  def initialize
5
5
  @pubsub_adapter = Hey::Pubsub::Adapters::AsnAdapter
6
+ @delimiter = ":"
6
7
  end
7
8
  end
@@ -0,0 +1,19 @@
1
+ class Hey::EventName
2
+ def initialize(name)
3
+ @name = name
4
+ end
5
+
6
+ def to_s
7
+ [Hey.global_namespace, namespaces, name].flatten.compact.join(Hey.configuration.delimiter)
8
+ end
9
+
10
+ private
11
+
12
+ attr_reader :name
13
+
14
+ def namespaces
15
+ @namespaces ||= Hey::ThreadCargo.contexts.map do |context|
16
+ Array[context[:namespace]]
17
+ end.flatten.compact
18
+ end
19
+ end
@@ -3,7 +3,7 @@ class Hey::Pubsub::Event
3
3
  attr_accessor :name
4
4
 
5
5
  def initialize(name:, started_at: nil, ended_at: nil, metadata: {})
6
- @name = name
6
+ @name = Hey::EventName.new(name).to_s
7
7
  @started_at = started_at
8
8
  @ended_at = ended_at
9
9
  @metadata = metadata
@@ -0,0 +1,19 @@
1
+ module Hey::Pubsub::Middleware
2
+ class Faraday
3
+ def call(env)
4
+ Hey.publish("request", request: env)
5
+
6
+ @app.call(env).on_complete do |env|
7
+ Hey.publish("response", response: env)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
data/lib/hey/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hey
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/hey.rb CHANGED
@@ -17,9 +17,12 @@ module Hey
17
17
  configuration.pubsub_adapter
18
18
  end
19
19
 
20
+ def self.global_namespace
21
+ configuration.global_namespace
22
+ end
23
+
20
24
  module Behavior
21
25
  def publish!(event_name, payload = {}, &block)
22
- event_name = "#{Hey.configuration.namespace}.#{event_name}" unless Hey.configuration.namespace.nil?
23
26
  event = Hey::Pubsub::Event.new(name: event_name, metadata: payload)
24
27
  pubsub_adapter.publish!(event, &block)
25
28
  end
@@ -43,6 +46,7 @@ end
43
46
 
44
47
  require "hey/configuration"
45
48
  require "hey/context"
49
+ require "hey/event_name"
46
50
  require "hey/thread_cargo"
47
51
  require "hey/sanitized_hash"
48
52
  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.1.0
4
+ version: 0.2.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-08-07 00:00:00.000000000 Z
11
+ date: 2015-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,9 +86,11 @@ files:
86
86
  - lib/hey.rb
87
87
  - lib/hey/configuration.rb
88
88
  - lib/hey/context.rb
89
+ - lib/hey/event_name.rb
89
90
  - lib/hey/pubsub.rb
90
91
  - lib/hey/pubsub/adapters/asn_adapter.rb
91
92
  - lib/hey/pubsub/event.rb
93
+ - lib/hey/pubsub/middleware/faraday.rb
92
94
  - lib/hey/sanitized_hash.rb
93
95
  - lib/hey/thread_cargo.rb
94
96
  - lib/hey/version.rb
@@ -112,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
114
  version: '0'
113
115
  requirements: []
114
116
  rubyforge_project:
115
- rubygems_version: 2.2.2
117
+ rubygems_version: 2.4.8
116
118
  signing_key:
117
119
  specification_version: 4
118
120
  summary: Pubsub wrapper with utilities to chain events, sanitize payloads and record