yeller 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +82 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/yeller/broadcaster.rb +25 -0
- data/lib/yeller/key.rb +16 -0
- data/lib/yeller/subscribable.rb +13 -0
- data/lib/yeller/subscriber.rb +24 -0
- data/lib/yeller/subscriber_collection.rb +26 -0
- data/lib/yeller/version.rb +3 -0
- data/lib/yeller.rb +20 -0
- data/yeller.gemspec +26 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9633a2d2494f3275f605be0b883e85d37e74d4ef
|
4
|
+
data.tar.gz: 54abda822f7917adbd7c9223b73dc54bd002597e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1e68fc86c720bec01d42aa4970391f51f0c070757503b4a66c064b1d9bc9cfff2fc1b9c2093cc42cf15f2b6fdf44cf52ba481ccb3546d88d4b450aba7faf6cfa
|
7
|
+
data.tar.gz: 17142e23a6d3b8f7ba5cec6c4a4ba154aa506eca2b52c708fa623ac04ca514a7ab44bd25d3a07b3c910c34b70cb0f9702e1262ed0ffff236f17551fc34245ad6
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Greg
|
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,82 @@
|
|
1
|
+
# Yeller
|
2
|
+
|
3
|
+
Simple in-process pub sub notifier for ruby. Everything runs inside the process with no queue. All the listeners get notified about events in real time with no repeats.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'yeller'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install yeller
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
There's 2 actions, as you would expect:
|
24
|
+
|
25
|
+
### Broadcast
|
26
|
+
|
27
|
+
To broadcast a message, just specify the message and the keys (they will be joined into one; this is for complex keys, so that you don't have to build them yourself)
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class MyLoudClass
|
31
|
+
def yell!
|
32
|
+
Yeller.broadcast "HELLO, EVERYONE!", "my", "many", "keys"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
and it will publish `"HELLO, EVERYONE!"` to everyone who is subscribed to any of those keys.
|
38
|
+
|
39
|
+
The keys can be a string, symbol, array, or hash. The Boradcaster will normalize them. This means that the key `[:a, :b, :c]` is the same as the key `[:c, :a, :b]`
|
40
|
+
|
41
|
+
__NOTE__: strings will strip out non-symbolizable characters, so "a@example.com" and "a/example.com" will be the same key: "a_example_com"
|
42
|
+
|
43
|
+
### Subscribe
|
44
|
+
|
45
|
+
The subscribers define what they listen to and how to respond.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class MyListenerClass
|
49
|
+
include Yeller::Subscribable
|
50
|
+
|
51
|
+
# the subscribable module provides this method
|
52
|
+
subscribe :react, "keys", "my"
|
53
|
+
|
54
|
+
def self.react( message )
|
55
|
+
puts "received #{message}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
which will print "received HELLO, EVERYONE!" when `MyLoudClass.yell!` is called.
|
61
|
+
|
62
|
+
You can also subscribe on the insance level with
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
Yeller.subscribe( MyListenerClass.new, :instance_level_react, "key", "other key" )
|
66
|
+
```
|
67
|
+
|
68
|
+
## Development
|
69
|
+
|
70
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
71
|
+
|
72
|
+
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).
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gaorlov/yeller.
|
77
|
+
|
78
|
+
|
79
|
+
## License
|
80
|
+
|
81
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
82
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "yeller"
|
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,25 @@
|
|
1
|
+
module Yeller
|
2
|
+
class Broadcaster
|
3
|
+
|
4
|
+
def initialize( )
|
5
|
+
@subscribers = SubscriberCollection.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def broadcast( message, *keys )
|
9
|
+
keys.each do |key|
|
10
|
+
normalized_key = Key.normalize(key)
|
11
|
+
@subscribers.find( normalized_key ).each do |subscriber|
|
12
|
+
subscriber.notify( message )
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def subscribe( subscriber_class, method, *keys )
|
18
|
+
keys.each do |key|
|
19
|
+
normalized_key = Key.normalize(key)
|
20
|
+
subscriber = Subscriber.new( subscriber_class, method, normalized_key )
|
21
|
+
@subscribers.add( subscriber )
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/yeller/key.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Yeller
|
2
|
+
class Key
|
3
|
+
class << self
|
4
|
+
def normalize( key )
|
5
|
+
if key.is_a? Array
|
6
|
+
string = key.map{|el| el.to_sym }.sort.join("_")
|
7
|
+
elsif key.is_a? Hash
|
8
|
+
string = key.symbolize_keys.sort.flatten.join("_")
|
9
|
+
else
|
10
|
+
string = key.to_s
|
11
|
+
end
|
12
|
+
string.gsub(/[ \\|\/\[\]\{\}.,-:;'"@?<>`~!#$%^&*()+=-]/, "_").to_sym
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Yeller
|
2
|
+
class Subscriber
|
3
|
+
attr_accessor :subscriber_class, :method, :key
|
4
|
+
|
5
|
+
def initialize( subscriber_class, method, key )
|
6
|
+
@subscriber_class = subscriber_class
|
7
|
+
@method = method
|
8
|
+
@key = key
|
9
|
+
end
|
10
|
+
|
11
|
+
def ==( comparison_object )
|
12
|
+
comparison_object.equal?(self) ||
|
13
|
+
( comparison_object.instance_of?(self.class) &&
|
14
|
+
comparison_object.method == method &&
|
15
|
+
comparison_object.key == key )
|
16
|
+
end
|
17
|
+
|
18
|
+
def notify( message )
|
19
|
+
Thread.new do
|
20
|
+
subscriber_class.send( method, message )
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Yeller
|
2
|
+
class SubscriberCollection
|
3
|
+
def initialize( )
|
4
|
+
@subscribers = {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def add( subscriber )
|
8
|
+
key = subscriber.key
|
9
|
+
|
10
|
+
@subscribers[key] ||= []
|
11
|
+
|
12
|
+
@subscribers[key] << subscriber
|
13
|
+
end
|
14
|
+
|
15
|
+
def remove( subscriber )
|
16
|
+
key = subscriber.key
|
17
|
+
@subscribers[key] = @subscribers[key].delete_if do |sub|
|
18
|
+
sub == subscriber
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def find( key )
|
23
|
+
@subscribers[key] || []
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/yeller.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "yeller/version"
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'active_support/core_ext/hash'
|
5
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
6
|
+
|
7
|
+
module Yeller
|
8
|
+
autoload :Broadcaster, "yeller/broadcaster"
|
9
|
+
autoload :Key, "yeller/key"
|
10
|
+
autoload :Subscribable, "yeller/subscribable"
|
11
|
+
autoload :Subscriber, "yeller/subscriber"
|
12
|
+
autoload :SubscriberCollection, "yeller/subscriber_collection"
|
13
|
+
|
14
|
+
mattr_accessor :_broadcaster
|
15
|
+
self._broadcaster ||= Broadcaster.new
|
16
|
+
|
17
|
+
delegate :broadcast, :subscribe, to: :_broadcaster
|
18
|
+
|
19
|
+
module_function :broadcast, :subscribe
|
20
|
+
end
|
data/yeller.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'yeller/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "yeller"
|
8
|
+
spec.version = Yeller::VERSION
|
9
|
+
spec.authors = ["Greg"]
|
10
|
+
spec.email = ["greg@avvo.com"]
|
11
|
+
|
12
|
+
spec.summary = "Simple in-process pub sub notifier for ruby."
|
13
|
+
spec.homepage = "https://github.com/gaorlov/yeller"
|
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_dependency "activesupport"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yeller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Greg
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- greg@avvo.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- lib/yeller.rb
|
85
|
+
- lib/yeller/broadcaster.rb
|
86
|
+
- lib/yeller/key.rb
|
87
|
+
- lib/yeller/subscribable.rb
|
88
|
+
- lib/yeller/subscriber.rb
|
89
|
+
- lib/yeller/subscriber_collection.rb
|
90
|
+
- lib/yeller/version.rb
|
91
|
+
- yeller.gemspec
|
92
|
+
homepage: https://github.com/gaorlov/yeller
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.5.1
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Simple in-process pub sub notifier for ruby.
|
116
|
+
test_files: []
|
117
|
+
has_rdoc:
|