noticer 0.1.0

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: 43653b4656c7a1b1cbf3da2176e40faadd0f865a
4
+ data.tar.gz: e2db94dbe6652c20cd2fd802ddbc6188e905e044
5
+ SHA512:
6
+ metadata.gz: c2004bb679bbd12cb037a266f66d1be08d67f305fc46b9c0db0aebea5d2bcb3c8deb260c149fd569cba63e4a96ae0967804de230a3bd3ace57890d1c02be7b0f
7
+ data.tar.gz: 2ffcfd029020eb6bea1e7a88212973de307e64208e4d63e5a7946abc137d9015753f5bc6fc785419c3403dfb21b07b70013602389bdc7b2dc6e1b1ba010a4f4d
data/.editorconfig ADDED
@@ -0,0 +1,12 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
10
+
11
+ [*.{sh,markdown}]
12
+ indent_size = 4
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.2
6
+ - 2.1.8
7
+ - 2.2.4
8
+ - 2.3.0
9
+ - jruby-9.0.5.0
10
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in noticer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Leonardo Alifraco
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,74 @@
1
+ [![Build Status](https://travis-ci.org/leonardoalifraco/noticer.svg?branch=master)](https://travis-ci.org/leonardoalifraco/noticer)
2
+ [![Code Climate](https://codeclimate.com/github/leonardoalifraco/noticer/badges/gpa.svg)](https://codeclimate.com/github/leonardoalifraco/noticer)
3
+
4
+ # Noticer
5
+
6
+ Noticer is a gem that allows the emission of notifications using a topic routing algorithm.
7
+
8
+ Configure the routing patterns along with a callback which is executed when the notification routing key matches the pattern.
9
+
10
+ It's useful when you want to abstract your application from different notification providers, or when the notification providers does not have message routing features (ie: AWS SNS).
11
+
12
+ Routing patterns allow the following wildcards:
13
+
14
+ ```
15
+ * (star) can substitute for exactly one word.
16
+ # (hash) can substitute for zero or more words.
17
+ ```
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ``` ruby
24
+ gem 'noticer'
25
+ ```
26
+
27
+ Then run:
28
+
29
+ ``` shell
30
+ bundle install
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ First configure the routing patterns along with the callbacks.
36
+
37
+ ``` ruby
38
+ Noticer.configure do |config|
39
+ config.notification_routes = [
40
+ {
41
+ routing_patterns: ['tree.green', 'tree.red'],
42
+ callback: -> (routing_key, message) {
43
+ # notify by email
44
+ }
45
+ },
46
+ {
47
+ routing_patterns: ['tree.*'],
48
+ callback: -> (routing_key, message) {
49
+ # notify to log
50
+ }
51
+ }
52
+ ]
53
+ end
54
+ ```
55
+
56
+ Then emit the required messages.
57
+
58
+ ``` ruby
59
+ # This will dispatch both notification callbacks
60
+ Noticer.emit('tree.green', 'A green tree was planted.')
61
+
62
+
63
+ # This will dispatch only the log callback
64
+ Noticer.emit('tree.blue', 'A blue tree was planted.')
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/leonardoalifraco/noticer.
70
+
71
+
72
+ ## License
73
+
74
+ 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 "noticer"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ module Noticer
2
+ class Configuration
3
+ attr_accessor :notification_routes
4
+
5
+ def initialize
6
+ @notification_routes = []
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,47 @@
1
+ module Noticer
2
+ class Dispatcher
3
+ def initialize(configuration = nil)
4
+ @configuration = configuration || Configuration.new
5
+ end
6
+
7
+ def emit(routing_key, message)
8
+ self.filtered_callbacks(routing_key).each do |ac|
9
+ ac[:callback].call(routing_key, message)
10
+ end
11
+ end
12
+
13
+ private
14
+ def filtered_callbacks(routing_key)
15
+ return @configuration.notification_routes.select do |ac|
16
+ ac[:routing_patterns].any? do |pattern|
17
+ topic_matches(pattern, routing_key)
18
+ end
19
+ end
20
+ end
21
+
22
+ def split_topic_key(key)
23
+ key.split(/\./).map(&:to_sym)
24
+ end
25
+
26
+ def topic_matches(p, r)
27
+ topic_matches_1(split_topic_key(p), split_topic_key(r))
28
+ end
29
+
30
+ def topic_matches_1(p, r)
31
+ p_head, *p_tail = *p
32
+ r_head, *r_tail = *r
33
+ return true if p.size == 1 && p_head == :"#"
34
+ return last_topic_match(p_tail, [], r.reverse) if p_head == :"#"
35
+ return true if p.empty? && r.empty?
36
+ return topic_matches_1(p_tail, r_tail) if p_head == :"*"
37
+ return topic_matches_1(p_tail, r_tail) if p_head == r_head
38
+ false
39
+ end
40
+
41
+ def last_topic_match(p, r, a)
42
+ return topic_matches_1(p, r) if a.empty?
43
+ backtrack_next, *backtrack_list = *a
44
+ return topic_matches_1(p, r) || last_topic_match(p, r.unshift(backtrack_next), backtrack_list)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Noticer
2
+ VERSION = "0.1.0"
3
+ end
data/lib/noticer.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "noticer/version"
2
+ require "noticer/configuration"
3
+ require "noticer/dispatcher"
4
+
5
+ module Noticer
6
+ class << self
7
+ attr_accessor :configuration
8
+
9
+ def configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ def configure
14
+ yield(configuration)
15
+ end
16
+ end
17
+ end
data/noticer.gemspec ADDED
@@ -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 'noticer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "noticer"
8
+ spec.version = Noticer::VERSION
9
+ spec.authors = ["Leonardo Alifraco"]
10
+ spec.email = ["leonardo.alifraco@gmail.com"]
11
+
12
+ spec.summary = "Noticer a gem that allows the emission of notifications using a topic routing algorithm."
13
+ spec.description = "Noticer a gem that allows the emission of notifications using a topic routing algorithm."
14
+ spec.homepage = "https://github.com/leonardoalifraco/noticer"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: noticer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leonardo Alifraco
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-18 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Noticer a gem that allows the emission of notifications using a topic
56
+ routing algorithm.
57
+ email:
58
+ - leonardo.alifraco@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".editorconfig"
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/noticer.rb
74
+ - lib/noticer/configuration.rb
75
+ - lib/noticer/dispatcher.rb
76
+ - lib/noticer/version.rb
77
+ - noticer.gemspec
78
+ homepage: https://github.com/leonardoalifraco/noticer
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.5.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Noticer a gem that allows the emission of notifications using a topic routing
102
+ algorithm.
103
+ test_files: []