communique 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -3
- data/Rakefile +1 -2
- data/communique.gemspec +22 -18
- data/lib/communique.rb +13 -12
- data/lib/communique/configuration.rb +3 -1
- data/lib/communique/handler.rb +7 -4
- data/lib/communique/models/action.rb +2 -4
- data/lib/communique/models/notifiable.rb +5 -3
- data/lib/communique/models/notification.rb +7 -10
- data/lib/communique/version.rb +2 -1
- data/spec/communique_spec.rb +22 -2
- data/spec/models/handler_spec.rb +37 -7
- data/spec/models/notification_spec.rb +1 -4
- data/spec/spec_helper.rb +17 -7
- data/spec/support/dummy/handler_dummy.rb +4 -1
- data/spec/support/dummy/notifiable_dummy.rb +1 -0
- data/spec/support/dummy/notifiable_dummy_spec.rb +4 -7
- metadata +45 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83a7e5be37e23918ac8477417e6bb769d3e0d524
|
4
|
+
data.tar.gz: 9e754661e47632b80ab8e1076941141ad0b37e8a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 627f1c1c594e0484808169074b956f1cd80fc26856192087db062d891696d97223c8d270998405d3aa123b855f136a3f45a8792b494fb0c9602a4bcecfc9e7d7
|
7
|
+
data.tar.gz: 16278c48625816efe4083323d06e782b1392a4834aac19589502ceafc6cc59aaa0c0e830ed96aaaabd233993cf57686afb027da51dfa4c19ab71df5a6352410a
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
# Communiqué
|
2
|
-
|
3
1
|
![communique logo](https://raw.github.com/Vestorly/communique/master/assets/logo.png)
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/Vestorly/communique/badges/gpa.svg)](https://codeclimate.com/github/Vestorly/communique)
|
4
|
+
|
4
5
|
Simple way to store warnings and errors affecting users as messages. Stored
|
5
6
|
messages can be linked to a helpful link so they can read more about the problem
|
6
7
|
|
@@ -23,8 +24,18 @@ Or install it yourself as:
|
|
23
24
|
$ gem install communique
|
24
25
|
|
25
26
|
## Usage
|
27
|
+
`include Communique::Notifiable` in the model you want to have notifications eg. the user model.
|
26
28
|
|
27
|
-
|
29
|
+
To create a notification:
|
30
|
+
```
|
31
|
+
Communique.notify(
|
32
|
+
@user,
|
33
|
+
'this_is_the_action_key',
|
34
|
+
context: 'you get some context',
|
35
|
+
more_context: 'you get some context',
|
36
|
+
even_more_context: 'you get some context'
|
37
|
+
)
|
38
|
+
```
|
28
39
|
|
29
40
|
## Contributing
|
30
41
|
|
data/Rakefile
CHANGED
@@ -1,2 +1 @@
|
|
1
|
-
require
|
2
|
-
|
1
|
+
require 'bundler/gem_tasks'
|
data/communique.gemspec
CHANGED
@@ -4,29 +4,33 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'communique/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'communique'
|
8
8
|
spec.version = Communique::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.summary = %q
|
9
|
+
spec.authors = ['Michael Polycarpou']
|
10
|
+
spec.email = ['michaelpolycarpou@gmail.com']
|
11
|
+
spec.summary = %q(
|
12
12
|
Simple way to store warnings and errors affecting users as messages. Stored
|
13
|
-
messages can be linked to a helpful link so they can read more about
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
spec.
|
13
|
+
messages can be linked to a helpful link so they can read more about
|
14
|
+
the problem
|
15
|
+
)
|
16
|
+
spec.description = %q(
|
17
|
+
Includes a couple models and an api to make it easy to create notifications
|
18
|
+
and the actions associated with them.
|
19
|
+
)
|
20
|
+
spec.homepage = 'https://www.vestorly.com/'
|
21
|
+
spec.license = 'MIT'
|
21
22
|
|
22
23
|
spec.files = `git ls-files -z`.split("\x0")
|
23
24
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
24
25
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
25
|
-
spec.require_paths = [
|
26
|
+
spec.require_paths = ['lib']
|
26
27
|
|
27
|
-
spec.add_development_dependency
|
28
|
-
spec.add_development_dependency
|
29
|
-
spec.add_development_dependency
|
30
|
-
spec.add_development_dependency 'rspec',
|
31
|
-
spec.add_development_dependency
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
29
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
30
|
+
spec.add_development_dependency 'mongoid', '~> 4.0'
|
31
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
32
|
+
spec.add_development_dependency 'database_cleaner', '~> 1.4'
|
33
|
+
spec.add_development_dependency 'pry'
|
34
|
+
spec.add_development_dependency 'simplecov'
|
35
|
+
spec.add_development_dependency 'simplecov-gem-adapter'
|
32
36
|
end
|
data/lib/communique.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
|
1
|
+
require 'communique/version'
|
2
|
+
require 'communique/configuration'
|
3
|
+
require 'communique/models/action'
|
4
|
+
require 'communique/models/notifiable'
|
5
|
+
require 'communique/models/notification'
|
6
|
+
require 'communique/handler'
|
7
|
+
|
8
|
+
# communique external methods
|
8
9
|
module Communique
|
9
|
-
def self.notify(notifiable, action_key, context_info=nil)
|
10
|
+
def self.notify(notifiable, action_key, context_info = nil)
|
10
11
|
action = Action.find_or_create_by(key: action_key)
|
11
12
|
|
12
13
|
notification = Notification.create(
|
@@ -25,15 +26,15 @@ module Communique
|
|
25
26
|
Action.all.to_a
|
26
27
|
end
|
27
28
|
|
28
|
-
def self.viewed_all!
|
29
|
-
Notification.viewed_all!
|
29
|
+
def self.viewed_all!(notifiable)
|
30
|
+
Notification.viewed_all!(notifiable)
|
30
31
|
end
|
31
32
|
|
32
|
-
def self.viewed!
|
33
|
+
def self.viewed!(notifiable, seen_notification_ids)
|
33
34
|
Notification.viewed! notifiable, seen_notification_ids
|
34
35
|
end
|
35
36
|
|
36
|
-
def self.count_unseen
|
37
|
+
def self.count_unseen(notifiable)
|
37
38
|
Notification.count_unseen notifiable
|
38
39
|
end
|
39
40
|
end
|
@@ -1,13 +1,15 @@
|
|
1
|
+
# top level gem module
|
1
2
|
module Communique
|
2
3
|
class << self
|
3
4
|
attr_accessor :config
|
4
5
|
end
|
5
6
|
|
6
|
-
def self.configure
|
7
|
+
def self.configure
|
7
8
|
self.config ||= Configuration.new
|
8
9
|
yield config if block_given?
|
9
10
|
end
|
10
11
|
|
12
|
+
# Configuration is in charge of handlers other future configurations
|
11
13
|
class Configuration
|
12
14
|
attr_accessor :user_klass
|
13
15
|
attr_accessor :notification_handler
|
data/lib/communique/handler.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
module Communique
|
2
2
|
class NotificationHandlerNotCallable < StandardError; end
|
3
|
+
|
4
|
+
# notification handler can do things like send push notifications and emails
|
3
5
|
class Handler
|
4
|
-
def self.external_services
|
6
|
+
def self.external_services(notification)
|
5
7
|
config = Communique.config
|
6
8
|
return if config.nil?
|
7
9
|
return if config.notification_handler.nil?
|
@@ -15,9 +17,10 @@ module Communique
|
|
15
17
|
end
|
16
18
|
|
17
19
|
def self.validate_notification_handler!(handler)
|
18
|
-
|
19
|
-
|
20
|
-
|
20
|
+
unless handler.respond_to?(:call)
|
21
|
+
fail(
|
22
|
+
NotificationHandlerNotCallable,
|
23
|
+
'notification_handler needs to be nil or a callable block'
|
21
24
|
)
|
22
25
|
end
|
23
26
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'mongoid'
|
2
2
|
module Communique
|
3
|
+
# notificaitons contain an action which gives more info about a certain
|
4
|
+
# message/error. These are what is shown to the user
|
3
5
|
class Action
|
4
6
|
include ::Mongoid::Document
|
5
7
|
include ::Mongoid::Timestamps
|
@@ -12,9 +14,5 @@ module Communique
|
|
12
14
|
field :level
|
13
15
|
field :send_email, type: Boolean, default: false
|
14
16
|
has_many :notifications
|
15
|
-
|
16
|
-
def act
|
17
|
-
'lets act on this'
|
18
|
-
end
|
19
17
|
end
|
20
18
|
end
|
@@ -1,11 +1,13 @@
|
|
1
1
|
module Communique
|
2
|
+
# generic model that contains many notifications
|
2
3
|
module Notifiable
|
3
|
-
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
included do
|
6
|
-
has_many
|
6
|
+
has_many(
|
7
|
+
:notifications,
|
7
8
|
class_name: 'Communique::Notification',
|
8
|
-
|
9
|
+
as: :notifiable
|
10
|
+
)
|
9
11
|
end
|
10
12
|
end
|
11
13
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'mongoid'
|
2
2
|
module Communique
|
3
|
+
# active model definition for notifications
|
3
4
|
class Notification
|
4
5
|
include ::Mongoid::Document
|
5
6
|
include ::Mongoid::Timestamps
|
@@ -11,22 +12,18 @@ module Communique
|
|
11
12
|
belongs_to :notifiable, polymorphic: true
|
12
13
|
belongs_to :action
|
13
14
|
|
14
|
-
def
|
15
|
-
'successful notification'
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.viewed_all! notifiable
|
15
|
+
def self.viewed_all!(notifiable)
|
19
16
|
notifiable.notifications.update_all(seen: true)
|
20
17
|
end
|
21
18
|
|
22
|
-
def self.viewed!
|
19
|
+
def self.viewed!(notifiable, seen_notification_ids)
|
23
20
|
notifiable.notifications.where(
|
24
|
-
:_id.in =>
|
25
|
-
).update_all(:
|
21
|
+
:_id.in => seen_notification_ids
|
22
|
+
).update_all(seen: true)
|
26
23
|
end
|
27
24
|
|
28
|
-
def self.count_unseen
|
29
|
-
notifiable.notifications.where(:
|
25
|
+
def self.count_unseen(notifiable)
|
26
|
+
notifiable.notifications.where(seen: false).count
|
30
27
|
end
|
31
28
|
end
|
32
29
|
end
|
data/lib/communique/version.rb
CHANGED
data/spec/communique_spec.rb
CHANGED
@@ -1,4 +1,24 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
describe Communique do
|
3
|
-
|
3
|
+
describe '#actions' do
|
4
|
+
it 'returns all actions' do
|
5
|
+
dummy = NotifiableDummy.create
|
6
|
+
Communique.notify(
|
7
|
+
dummy,
|
8
|
+
'r2d2_broke_with_a_500',
|
9
|
+
name: 'broken robot',
|
10
|
+
location: 'proxy issue'
|
11
|
+
)
|
12
|
+
Communique.notify(
|
13
|
+
dummy,
|
14
|
+
'3CPO_fell_down_and_hit_his_head',
|
15
|
+
name: 'broken robot',
|
16
|
+
location: 'nginx caching issue'
|
17
|
+
)
|
18
|
+
expect(Communique.actions.count).to eq 2
|
19
|
+
expect(Communique.actions.map(&:key)).to contain_exactly(
|
20
|
+
'r2d2_broke_with_a_500', '3CPO_fell_down_and_hit_his_head'
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
4
24
|
end
|
data/spec/models/handler_spec.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
describe Communique::Handler do
|
3
3
|
describe '#external_services' do
|
4
4
|
context 'no config block' do
|
5
|
-
|
6
5
|
it 'works with no config block' do
|
7
|
-
expect(Communique::Handler)
|
6
|
+
expect(Communique::Handler)
|
7
|
+
.to receive(:external_services)
|
8
|
+
.and_return nil
|
8
9
|
|
9
10
|
dummy = NotifiableDummy.create
|
10
11
|
Communique.notify(
|
@@ -16,10 +17,9 @@ describe Communique::Handler do
|
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
|
-
context 'with config block' do
|
20
|
-
let(:dummy_handler){HandlerDummy.new}
|
20
|
+
context 'with valid config block' do
|
21
|
+
let(:dummy_handler) { HandlerDummy.new }
|
21
22
|
before(:each) do
|
22
|
-
|
23
23
|
Communique.configure do |config|
|
24
24
|
config.set_notification_handler do |notification, action, dummy|
|
25
25
|
dummy_handler.push_notification(notification, action, dummy)
|
@@ -36,10 +36,40 @@ describe Communique::Handler do
|
|
36
36
|
location: 'it moves really fast'
|
37
37
|
)
|
38
38
|
|
39
|
-
expect(dummy_handler.notification.context_info[:name])
|
39
|
+
expect(dummy_handler.notification.context_info[:name])
|
40
|
+
.to eq('locked star')
|
40
41
|
expect(dummy_handler.action.key).to eq('death_star_deadlock')
|
41
42
|
expect(dummy_handler.notifiable.class.name).to eq('NotifiableDummy')
|
42
43
|
end
|
43
44
|
end
|
45
|
+
|
46
|
+
context 'with invalid config block' do
|
47
|
+
let(:dummy_handler) { HandlerDummy.new }
|
48
|
+
before(:each) do
|
49
|
+
Communique.configure do |config|
|
50
|
+
config.notification_handler = 'not callable'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
after(:each) do
|
54
|
+
Communique.configure do |config|
|
55
|
+
config.notification_handler = nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'external block gets called' do
|
60
|
+
dummy = NotifiableDummy.create
|
61
|
+
expect do
|
62
|
+
Communique.notify(
|
63
|
+
dummy,
|
64
|
+
'death_star_deadlock',
|
65
|
+
name: 'locked star',
|
66
|
+
location: 'it moves really fast'
|
67
|
+
)
|
68
|
+
end.to raise_error(
|
69
|
+
Communique::NotificationHandlerNotCallable,
|
70
|
+
'notification_handler needs to be nil or a callable block'
|
71
|
+
)
|
72
|
+
end
|
73
|
+
end
|
44
74
|
end
|
45
75
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
describe Communique::Notification do
|
3
3
|
describe '#viewed_all!' do
|
4
4
|
it 'makes all notifications seen' do
|
@@ -25,7 +25,6 @@ describe Communique::Notification do
|
|
25
25
|
|
26
26
|
expect(dummy.notifications.first.seen).to be true
|
27
27
|
expect(dummy.notifications.last.seen).to be true
|
28
|
-
|
29
28
|
end
|
30
29
|
end
|
31
30
|
|
@@ -54,7 +53,6 @@ describe Communique::Notification do
|
|
54
53
|
|
55
54
|
expect(dummy.notifications.first.seen).to be true
|
56
55
|
expect(dummy.notifications.last.seen).to be false
|
57
|
-
|
58
56
|
end
|
59
57
|
end
|
60
58
|
|
@@ -77,7 +75,6 @@ describe Communique::Notification do
|
|
77
75
|
Communique.viewed_all! dummy
|
78
76
|
dummy.reload
|
79
77
|
expect(Communique.count_unseen(dummy)).to be 0
|
80
|
-
|
81
78
|
end
|
82
79
|
end
|
83
80
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,15 +1,25 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
require 'simplecov'
|
2
|
+
require 'simplecov-gem-adapter'
|
3
|
+
# save to CircleCI's artifacts directory if we're on CircleCI
|
4
|
+
if ENV['CIRCLE_ARTIFACTS']
|
5
|
+
dir = File.join(ENV['CIRCLE_ARTIFACTS'], "coverage")
|
6
|
+
SimpleCov.coverage_dir(dir)
|
7
|
+
end
|
8
|
+
|
9
|
+
SimpleCov.start('gem')
|
10
|
+
|
11
|
+
['../lib', './models', '../config'].each do |f|
|
12
|
+
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), f))
|
13
|
+
end
|
4
14
|
|
5
15
|
require 'database_cleaner'
|
6
16
|
require 'mongoid'
|
7
17
|
require 'communique'
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
18
|
+
require 'support/dummy/notifiable_dummy.rb'
|
19
|
+
require 'support/dummy/handler_dummy.rb'
|
20
|
+
require 'models/notification_spec.rb'
|
11
21
|
|
12
|
-
Mongoid.load!(
|
22
|
+
Mongoid.load!('./config/mongoid.yml', :test)
|
13
23
|
|
14
24
|
RSpec.configure do |config|
|
15
25
|
config.before(:suite) do
|
@@ -1,9 +1,12 @@
|
|
1
|
+
# support spec class that would be replaced with a pusher or email sender.
|
2
|
+
# It is able to save the parameters passed to it
|
3
|
+
# so that we can test their values.
|
1
4
|
class HandlerDummy
|
2
5
|
attr_reader :notification
|
3
6
|
attr_reader :action
|
4
7
|
attr_reader :notifiable
|
5
8
|
|
6
|
-
def push_notification
|
9
|
+
def push_notification(notification, action, notifiable)
|
7
10
|
@notification = notification
|
8
11
|
@action = action
|
9
12
|
@notifiable = notifiable
|
@@ -1,9 +1,8 @@
|
|
1
|
-
require
|
2
|
-
require 'pry'
|
1
|
+
require 'spec_helper'
|
3
2
|
describe NotifiableDummy do
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
it 'has a configuration already set' do
|
4
|
+
expect(Communique.config).to_not be_nil
|
5
|
+
end
|
7
6
|
|
8
7
|
it 'has notifications' do
|
9
8
|
dummy = NotifiableDummy.new
|
@@ -16,7 +15,5 @@ describe NotifiableDummy do
|
|
16
15
|
|
17
16
|
)
|
18
17
|
expect(dummy.notifications.first.context_info[:name]).to eq('hello')
|
19
|
-
|
20
18
|
end
|
21
|
-
|
22
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: communique
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Polycarpou
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,8 +80,50 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '1.4'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov-gem-adapter
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
83
125
|
description: "\n Includes a couple models and an api to make it easy to create
|
84
|
-
notifications
|
126
|
+
notifications\n and the actions associated with them.\n "
|
85
127
|
email:
|
86
128
|
- michaelpolycarpou@gmail.com
|
87
129
|
executables: []
|