unotifier 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a19987dbc68516497b96678c785b0d4c48f5e99fe0ac2c51b0b361ec389914da
4
+ data.tar.gz: d45bd37bd9de65c2d8f27ca48ee390013ab6d15cf68e68e83232f00647ca7bb4
5
+ SHA512:
6
+ metadata.gz: 6371d94478f0443dd43d6b48a46f796bd40d7c5efa8cf2ea249aed2f25d420459e1800d2df991d778c4bae3ab558aab3cab0e30ce8c390c1d980ee807290eba3
7
+ data.tar.gz: fef78dda39517b3e146a483a2370add2220949e9258ad685bde790137699a883a1b0c0ee9ddbe853fa4eb3ea276865d95e89899721c62989c986905ec7bcbda4
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .byebug_history
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in unotifier.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ unotifier (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (10.0.2)
10
+ coderay (1.1.2)
11
+ concurrent-ruby (1.1.3)
12
+ diff-lcs (1.3)
13
+ i18n (1.1.1)
14
+ concurrent-ruby (~> 1.0)
15
+ method_source (0.9.2)
16
+ pry (0.12.2)
17
+ coderay (~> 1.1.0)
18
+ method_source (~> 0.9.0)
19
+ rake (10.5.0)
20
+ rspec (3.8.0)
21
+ rspec-core (~> 3.8.0)
22
+ rspec-expectations (~> 3.8.0)
23
+ rspec-mocks (~> 3.8.0)
24
+ rspec-core (3.8.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-expectations (3.8.2)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-mocks (3.8.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.8.0)
32
+ rspec-support (3.8.0)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ bundler (~> 1.17)
39
+ byebug
40
+ i18n
41
+ unotifier!
42
+ pry
43
+ rake (~> 10.0)
44
+ rspec (~> 3.0)
45
+
46
+ BUNDLED WITH
47
+ 1.17.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Artem Rashev
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,44 @@
1
+ # UNotifier
2
+
3
+ [![Build Status](https://travis-ci.com/mgpnd/unotifier.svg?branch=master)](https://travis-ci.com/mgpnd/unotifier)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'unotifier'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install unotifier
20
+
21
+ ## Usage
22
+
23
+ To store your notifications in database UNotifier uses provided ActiveRecord model. It must have the next attributes:
24
+ * `id` - required
25
+ * `key` - required
26
+ * `target` - required
27
+ * `title` - required
28
+ * `body` - optional
29
+ * `autohide_delay` - optional
30
+ * `link` - optional
31
+
32
+ `target` is a user or any other notification reciever entity. It also must provide several attributes:
33
+ * `login`
34
+ * `online?` - to determine whether to send *onsite* or *external* notification
35
+ * `locale` - to send proper locale to I18n
36
+
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mgpnd/unotifier.
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://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,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "unotifier"
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
+ require "pry"
10
+ Pry.start(__FILE__)
data/bin/rake ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rake", "rake")
data/bin/rspec ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
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,15 @@
1
+ module UNotifier
2
+ class Configuration
3
+ attr_accessor :site_providers
4
+ attr_accessor :external_providers
5
+ attr_accessor :resource_class
6
+ attr_accessor :notifications_path
7
+
8
+ def initialize
9
+ @site_providers = []
10
+ @external_providers = []
11
+ @resource_class = nil
12
+ @notifications_path = nil
13
+ end
14
+ end
15
+ end
data/lib/exceptions.rb ADDED
@@ -0,0 +1,57 @@
1
+ module UNotifier
2
+ class UNotifierError < StandardError; end
3
+
4
+ class NotificationsConfigNotFoundError < UNotifierError
5
+ attr_reader :path, :absolute_path
6
+
7
+ def initialize(path)
8
+ @path = path
9
+ @absolute_path = File.expand_path(@path)
10
+ message = "Configuration file not found at #{@absolute_path}"
11
+ super(message)
12
+ end
13
+ end
14
+
15
+ class NotificationNotFoundError < UNotifierError
16
+ attr_reader :key
17
+
18
+ def initialize(key)
19
+ @key = key
20
+ message = "Notification with key '#{@key}' not found"
21
+ super(message)
22
+ end
23
+ end
24
+
25
+ class UnknownTargetError < UNotifierError
26
+ attr_reader :key, :target
27
+
28
+ def initialize(key, target)
29
+ @key = key
30
+ @target = target
31
+ message = "Unknown target '#{@target}' for notification '#{@key}'"
32
+ super(message)
33
+ end
34
+ end
35
+
36
+ class EmptyLocaleKeyError < UNotifierError
37
+ attr_reader :key
38
+
39
+ def initialize(key)
40
+ @key = key
41
+ message = ":locale_key parameter is required for fetching '#{key}' locale"
42
+ super(message)
43
+ end
44
+ end
45
+
46
+ class UnknownLocaleKeyError < UNotifierError
47
+ attr_reader :key, :locale_key, :options
48
+
49
+ def initialize(key, locale_key, options)
50
+ @key = key
51
+ @locale_key = locale_key
52
+ @options = options
53
+ message = "Unkown locale key '#{@locale_key}' for '#{@key}'. Available options: #{@options}"
54
+ super(message)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,33 @@
1
+ module UNotifier
2
+ module Provider
3
+ class ActionCable
4
+ attr_reader :server
5
+ attr_accessor :autohide_delay, :channel_name
6
+
7
+ def initialize(server, channel_name: nil, autohide_delay: 30)
8
+ @server = server
9
+ @autohide_delay = autohide_delay
10
+ @channel_name =
11
+ channel_name ||
12
+ -> (notification) { "notifications_for_user_#{notification.target.login}" }
13
+ end
14
+
15
+ def notify(notification)
16
+ if notification.target.online?
17
+ @server.broadcast @channel_name.call(notification), serialize_notification(notification)
18
+ end
19
+ end
20
+
21
+ def serialize_notification(notification)
22
+ {
23
+ id: notification.id,
24
+ title: notification.title,
25
+ body: notification.body,
26
+ autohide_delay: notification.autohide_delay || @autohide_delay,
27
+ user_login: notification.target.login,
28
+ link: notification.link,
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,14 @@
1
+ module UNotifier
2
+ module Provider
3
+ class ActionMailer
4
+ def initialize(mailer, sending_method: :new_notification)
5
+ @mailer = mailer
6
+ @sending_method = sending_method
7
+ end
8
+
9
+ def notify(notification)
10
+ @mailer.public_send(@sending_method, notification).deliver
11
+ end
12
+ end
13
+ end
14
+ end
data/lib/provider.rb ADDED
@@ -0,0 +1,7 @@
1
+ require_relative "provider/action_cable"
2
+ require_relative "provider/action_mailer"
3
+
4
+ module UNotifier
5
+ module Provider
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module UNotifier
2
+ VERSION = "0.1.0"
3
+ end
data/lib/unotifier.rb ADDED
@@ -0,0 +1,111 @@
1
+ require "yaml"
2
+ require "i18n"
3
+
4
+ require_relative "unotifier/version"
5
+ require_relative "exceptions"
6
+ require_relative "provider"
7
+ require_relative "configuration"
8
+
9
+ module UNotifier
10
+ class << self
11
+ attr_writer :configuration
12
+ end
13
+
14
+ def self.configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ def self.configure
19
+ yield(configuration)
20
+ end
21
+
22
+ def self.notifications_config
23
+ @notifications_config ||= YAML.load_file(configuration.notifications_path)
24
+ rescue Errno::ENOENT => e
25
+ raise NotificationsConfigNotFoundError.new(configuration.notifications_path)
26
+ end
27
+
28
+ def self.reload!
29
+ @configuration = nil
30
+ @notifications_config = nil
31
+ end
32
+
33
+ def self.load_notification(key)
34
+ notifications_config.dig(*key.split('.'))
35
+ end
36
+
37
+ def self.load_notification!(key)
38
+ notification = load_notification(key)
39
+ raise NotificationNotFoundError.new(key) unless notification
40
+ notification
41
+ end
42
+
43
+ def self.urgency_for(key, params = {})
44
+ config = load_notification!(key)
45
+
46
+ if config["target"].kind_of?(Hash)
47
+ urgency = config.dig("target", params[:target], "urgency")
48
+ raise UnknownTargetError.new(key, params[:target]) unless urgency
49
+ urgency
50
+ else
51
+ config["urgency"]
52
+ end
53
+ end
54
+
55
+ def self.locale_key_for(key, params = {})
56
+ config = load_notification!(key)
57
+
58
+ if config["locale_options"].kind_of?(Array)
59
+ raise EmptyLocaleKeyError.new(key) unless params[:locale_key]
60
+ raise UnknownLocaleKeyError.new(
61
+ key, params[:locale_key], config["locale_options"]
62
+ ) unless config["locale_options"].include?(params[:locale_key])
63
+ "notifications.#{key}.#{params[:locale_key]}"
64
+ else
65
+ "notifications.#{key}"
66
+ end
67
+ end
68
+
69
+ def self.notify(key, target, params = {})
70
+ user_settings = target.notification_settings[key]
71
+ user_settings ||= "external"
72
+ config = load_notification!(key)
73
+ locale_key = locale_key_for(key, params)
74
+ urgency = urgency_for(key, params)
75
+
76
+ notification = configuration.resource_class.new(
77
+ key: key,
78
+ title: I18n.t("#{locale_key}.title", params.merge(locale: target.locale, default: "")),
79
+ body: I18n.t("#{locale_key}.title", params.merge(locale: target.locale, default: "")),
80
+ link: params[:link],
81
+ urgency: urgency
82
+ )
83
+ notification.save!
84
+
85
+ notify_with = []
86
+
87
+ case urgency
88
+ when "immediate"
89
+ notify_with += configuration.site_providers if notification.target.online?
90
+ notify_with += configuration.external_providers
91
+ when "regular"
92
+ if notification.target.online?
93
+ notify_with += configuration.site_providers
94
+ elsif user_settings == "external"
95
+ notify_with += configuration.external_providers
96
+ end
97
+ when "optional"
98
+ if user_settings != "off"
99
+ if notification.target.online?
100
+ notify_with += configuration.site_providers
101
+ elsif user_settings == "external"
102
+ notify_with += configuration.external_providers
103
+ end
104
+ end
105
+ when "onsite"
106
+ notify_with += configuration.site_providers if notification.target.online?
107
+ end
108
+
109
+ notify_with.each { |p| p.notify(notification) }
110
+ end
111
+ end
data/unotifier.gemspec ADDED
@@ -0,0 +1,39 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "unotifier/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unotifier"
8
+ spec.version = UNotifier::VERSION
9
+ spec.authors = ["Artem Rashev"]
10
+ spec.email = ["stillintop@gmail.com"]
11
+
12
+ spec.summary = "Ultimate user notification tool for web apps"
13
+ spec.homepage = "https://github.com/mgpnd/unotifier"
14
+ spec.license = "MIT"
15
+
16
+ if spec.respond_to?(:metadata)
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["sourc_code_uri"] = "https://github.com/mgpnd/unotifier"
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against " \
21
+ "public gem pushes."
22
+ end
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) || f.match(%r{^(\.rspec|\.travis\.yml)}) }
28
+ end
29
+ spec.bindir = "bin"
30
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.17"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ spec.add_development_dependency "pry"
37
+ spec.add_development_dependency "byebug"
38
+ spec.add_development_dependency "i18n"
39
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unotifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Artem Rashev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-27 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: i18n
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
+ description:
98
+ email:
99
+ - stillintop@gmail.com
100
+ executables:
101
+ - console
102
+ - rake
103
+ - rspec
104
+ - setup
105
+ extensions: []
106
+ extra_rdoc_files: []
107
+ files:
108
+ - ".gitignore"
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - bin/rake
116
+ - bin/rspec
117
+ - bin/setup
118
+ - lib/configuration.rb
119
+ - lib/exceptions.rb
120
+ - lib/provider.rb
121
+ - lib/provider/action_cable.rb
122
+ - lib/provider/action_mailer.rb
123
+ - lib/unotifier.rb
124
+ - lib/unotifier/version.rb
125
+ - unotifier.gemspec
126
+ homepage: https://github.com/mgpnd/unotifier
127
+ licenses:
128
+ - MIT
129
+ metadata:
130
+ homepage_uri: https://github.com/mgpnd/unotifier
131
+ sourc_code_uri: https://github.com/mgpnd/unotifier
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.7.8
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Ultimate user notification tool for web apps
152
+ test_files: []