hey-pubsub 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 777cd434081c96081256b6c8f4005f0d3eb56337
4
+ data.tar.gz: cddd2ed4927bdd139eeb25cf147095be362b40b6
5
+ SHA512:
6
+ metadata.gz: f002172c5f5a9052907e29e5842a7f2db05fa0adbf1046935d1c88b65db4b2eae022601c23dffc35c7c2b42ccbe9cd8238fcd7827dd155591db94d9bc53a9071
7
+ data.tar.gz: 9a205acdd2080483330edf069572a605c998f1176e9293b2fae4442c5596da0bc2f64f12b8d56a8f0811c44a6132c331659930ed96cc6e9dbb2a9caad6a210c4
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ .idea
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hey-pubsub.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Theo Mills
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,152 @@
1
+ # Hey!
2
+
3
+ Hey is a lightweight pubsub wrapper that makes it easy to change underlying messaging implementations and also includes
4
+ some convenience utilities to:
5
+
6
+ * Track a chain of events
7
+ * Sanitize sensitive data from event payloads
8
+ * Record who originally kicked off the chain of events (the actor)
9
+ * Set arbitrary data that will be included on every event published on the same thread
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'hey-pubsub'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install hey-pubsub
26
+
27
+ ## Why use Hey?
28
+
29
+ We often treat logging, exception notification and event broadcasting as separate operations within our code. Hey
30
+ encourages you to treat everything as a published event that is meaningful to the current operation being performed.
31
+
32
+ Any number of subscribers may react to the event once it's published. For example a published registration failure event
33
+ could have a logstash subscriber logging the failure details into Kibana and a HoneyBadger subscriber recording the exception
34
+ and notifying the dev team. A successful registration event, on the other hand, could trigger a welcome email delivery.
35
+
36
+ The utilities Hey provides, such as data sanitization, makes it more appealing to combine these logging and pubsub use cases.
37
+ Additionally the ability to switch out messaging implementations means you start with a simple approach (ActiveSupport
38
+ Notifications) and ease into using a more distributed, asynchronous approach as your application grows without changing
39
+ your code.
40
+
41
+ ## Shared Metadata
42
+
43
+ Hey provides utilities to share metadata across all events published on the same thread.
44
+
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
+ ### Event chain UUID
59
+
60
+ The first time an event is published via Hey a UUID will be assigned and stored on the current thread.
61
+
62
+ Any events published for the life of the current thread with include this `uuid` in their payloads. This allows a
63
+ chain of events to be associated later in a Kibana dashboard, for example.
64
+
65
+ This UUID could be used across disparate systems subscribing to the same message bus, so long as the convention is
66
+ adhered to.
67
+
68
+ ### Sanitizing payloads
69
+
70
+ There are times you'd like sensitive information to be stripped from event payloads. For example, if you are logging API
71
+ requests and responses you should redact credentials before writing to a database or logfile.
72
+
73
+ It's easier to handle this sanitization during publication, since subscribers of the events will likely not know what
74
+ values to strip. Hey provides a utility to record these sensitive values and every event published during the life of
75
+ the current thread will redact them from their payloads.
76
+
77
+ ```ruby
78
+ Hey.sanitize!("ABC123", "4222222222222222", "245-65-12763")
79
+ ```
80
+
81
+ ### Setting arbitrary data
82
+
83
+ If you need to set arbitrary values to be included on every event payload for the life of a thread, you can:
84
+
85
+ ```ruby
86
+ Hey.set(:ip_address, "127.0.0.1")
87
+ ```
88
+ ## Event publishing and subscribing
89
+
90
+ ### Adapters
91
+
92
+ Hey uses an ActiveSupport Notifications adapter by default, which is essentially a synchronous event bus. However it is
93
+ easy to configure a different adapter if needed:
94
+
95
+ ```ruby
96
+ Hey.configure do |config|
97
+ config.pubsub_adapter = FooBar::RabbitMqAdapter
98
+ end
99
+ ```
100
+
101
+ __Note: Currently the only adapter is the aforementioned `Hey::Pubsub::Adapters::AsnAdapter`__
102
+
103
+ ### Publishing
104
+
105
+ To gain all of the `Hey` goodness, you have to of course use the Hey `publish!` method:
106
+
107
+ ```ruby
108
+ Hey.publish!("registration.succeeded", { email: "john@ecommerce.com" })
109
+ ```
110
+
111
+ The resulting payload would look something like this:
112
+
113
+ ```
114
+ {
115
+ uuid: "0a5d3f22-2cff-4126-a791-c4ac31a2a5bb",
116
+ current_actor: {
117
+ id: "123343",
118
+ type: "Employee",
119
+ name: "Jack Ship"
120
+ },
121
+ email: "john@ecommerce.com"
122
+ }
123
+ ```
124
+
125
+ __Note: Though ASN supports passing objects in payloads since it's all in the same thread, do not do it. If you ever
126
+ switch to an asyncronous adapter it will fail. Only pass JSON compatible values.__
127
+
128
+ ### Subscribing
129
+
130
+ To subscribe to a published event, do this:
131
+
132
+ ```ruby
133
+ Hey.subscribe!("registration.succeeded") do |payload|
134
+ email = payload["email"]
135
+ RegistrationMailer.new_customer(email).deliver
136
+ end
137
+ ```
138
+
139
+ ## Development
140
+
141
+ 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.
142
+
143
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
144
+
145
+ ## Contributing
146
+
147
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ShippingEasy/hey-pubsub.
148
+
149
+
150
+ ## License
151
+
152
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hey/pubsub"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hey/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hey-pubsub"
8
+ spec.version = Hey::VERSION
9
+ spec.authors = ["ShippingEasy"]
10
+ spec.email = ["dev@shippingeasy.com"]
11
+
12
+ spec.summary = %q{Pubsub wrapper with utilities to chain events, sanitize payloads and record the actor.}
13
+ spec.homepage = "https://github.com/ShippingEasy/hey-pubsub"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_dependency "activesupport"
25
+ end
@@ -0,0 +1,7 @@
1
+ class Hey::Configuration
2
+ attr_accessor :pubsub_adapter
3
+
4
+ def initialize
5
+ @pubsub_adapter = Hey::Pubsub::Adapters::AsnAdapter
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ require "active_support/all"
2
+
3
+ module Hey::Pubsub::Adapters
4
+ module AsnAdapter
5
+ def self.subscribe!(event_name)
6
+ if block_given?
7
+ ActiveSupport::Notifications.subscribe(event_name) do |*args|
8
+ event = ActiveSupport::Notifications::Event.new(*args)
9
+ yield(event.payload)
10
+ end
11
+ end
12
+ end
13
+
14
+ def self.publish!(event_name, payload = {})
15
+ if block_given?
16
+ ActiveSupport::Notifications.instrument(event_name, payload) do
17
+ yield
18
+ end
19
+ else
20
+ ActiveSupport::Notifications.instrument(event_name, payload)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,42 @@
1
+ class Hey::Pubsub::Payload
2
+ def initialize(values = {})
3
+ @values = values
4
+ merge_values!
5
+ sanitize_values!
6
+ end
7
+
8
+ def to_hash
9
+ values
10
+ end
11
+
12
+ alias_method :to_h, :to_hash
13
+
14
+ private
15
+
16
+ attr_accessor :values
17
+
18
+ def merge_values!
19
+ Hey::ThreadCargo.uuid # initialize if it has never been set
20
+ self.values = Hey::ThreadCargo.to_hash.merge(values)
21
+ 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.gsub!(sanitizable_value, "")
30
+ end
31
+ end
32
+
33
+ def traverse_hash(h, &block)
34
+ h.each_pair do |k, v|
35
+ if v.is_a?(Hash)
36
+ traverse_hash(v, &block)
37
+ else
38
+ yield(k, v)
39
+ end
40
+ end
41
+ end
42
+ end
data/lib/hey/pubsub.rb ADDED
@@ -0,0 +1 @@
1
+ module Hey::Pubsub; end
@@ -0,0 +1,54 @@
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)
6
+ Thread.current[:hey] = {} if Thread.current[:hey].nil?
7
+ Thread.current[:hey][name] = value
8
+ value
9
+ end
10
+
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]
15
+ end
16
+
17
+ # Returns the actor from the current thread
18
+ def self.current_actor
19
+ get(:current_actor)
20
+ end
21
+
22
+ def self.uuid
23
+ return set(:uuid, SecureRandom.uuid) if get(:uuid).nil?
24
+ get(:uuid)
25
+ end
26
+
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
+ # Adds the supplied values to the sanitized values array. It removes nils and duplicate values from the array.
33
+ def self.sanitize!(*values)
34
+ set(SANITIZABLE_VALUES_KEY, []) if get(SANITIZABLE_VALUES_KEY).nil?
35
+ values = Array(values)
36
+ set(SANITIZABLE_VALUES_KEY, get(SANITIZABLE_VALUES_KEY).concat(values).flatten.compact.uniq)
37
+ end
38
+
39
+ # Removes all namespaced values from the current thread.
40
+ def self.purge!
41
+ Thread.current[:hey] = nil
42
+ end
43
+
44
+ def self.sanitizable_values
45
+ Array(get(SANITIZABLE_VALUES_KEY))
46
+ end
47
+
48
+ def self.to_hash
49
+ Thread.current[:hey] = {} if Thread.current[:hey].nil?
50
+ hash = Thread.current[:hey].clone
51
+ hash.delete(SANITIZABLE_VALUES_KEY)
52
+ hash
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module Hey
2
+ VERSION = "0.0.1"
3
+ end
data/lib/hey.rb ADDED
@@ -0,0 +1,43 @@
1
+ require "hey/version"
2
+
3
+ module Hey
4
+ def self.configure
5
+ yield(configuration)
6
+ end
7
+
8
+ def self.configuration
9
+ @configuration ||= Hey::Configuration.new
10
+ end
11
+
12
+ def self.pubsub_adapter
13
+ configuration.pubsub_adapter
14
+ end
15
+
16
+ def self.publish!(event_name, payload = {})
17
+ payload = Hey::Pubsub::Payload.new(payload)
18
+ pubsub_adapter.publish!(event_name, payload.to_h)
19
+ end
20
+
21
+ def self.subscribe!(event_name)
22
+ pubsub_adapter.subscribe!(event_name)
23
+ end
24
+
25
+ def self.set(name, value)
26
+ Hey::ThreadCargo.set(name, value)
27
+ end
28
+
29
+ def self.set_current_actor!(name:, id: nil, type: nil)
30
+ Hey::ThreadCargo.set_current_actor(name: name, id: id, type: type)
31
+ end
32
+
33
+ def self.sanitize!(*values)
34
+ Hey::ThreadCargo.sanitize!(values)
35
+ end
36
+ end
37
+
38
+ require "securerandom"
39
+ require "hey/configuration"
40
+ require "hey/thread_cargo"
41
+ require "hey/pubsub"
42
+ require "hey/pubsub/payload"
43
+ require "hey/pubsub/adapters/asn_adapter"
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hey-pubsub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ShippingEasy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - dev@shippingeasy.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - hey-pubsub.gemspec
86
+ - lib/hey.rb
87
+ - lib/hey/configuration.rb
88
+ - lib/hey/pubsub.rb
89
+ - lib/hey/pubsub/adapters/asn_adapter.rb
90
+ - lib/hey/pubsub/payload.rb
91
+ - lib/hey/thread_cargo.rb
92
+ - lib/hey/version.rb
93
+ homepage: https://github.com/ShippingEasy/hey-pubsub
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.6
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Pubsub wrapper with utilities to chain events, sanitize payloads and record
117
+ the actor.
118
+ test_files: []