active_notifier 0.1.0 → 0.2.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 +4 -4
- data/README.md +111 -4
- data/lib/active_notifier/adaptable.rb +1 -3
- data/lib/active_notifier/adapters/abstract_adapter.rb +10 -0
- data/lib/active_notifier/adapters/dingtalk_adapter.rb +2 -2
- data/lib/active_notifier/configurable.rb +45 -17
- data/lib/active_notifier/core.rb +27 -14
- data/lib/active_notifier/errors.rb +0 -13
- data/lib/active_notifier/version.rb +1 -1
- data/lib/active_notifier.rb +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24d5eac70996ae751cbfac298f4bd78074b53f913c19325347bf50bfcd557eb9
|
4
|
+
data.tar.gz: 7716f56010b04db00fe06e7a3b5773350a9a58ffcb4b117a64bfd17ed3fe18a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4793839706e3f4fad3151255736fc0b820611997cd044e793c125b93e1375aab281dd476165525748f6229cd6b45693d0536dad31cc66a47a3924f43119adf4
|
7
|
+
data.tar.gz: 77e1fa443c84606455d712a7340d474b607ebcfa3fb59567f334a19a370384d5d7656fd94a9cd0f9fe4a7265c8b42139ca210e5dfe83cbf8567918e991fc00a5
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# ActiveNotifier
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Notify message through webhooks.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,116 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
### Basic
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
ActiveNotifer.config.channel_tokens = { default: "xxx" }
|
27
|
+
ActiveNotifier.exec(:default)
|
28
|
+
# This will notifier message, use options:
|
29
|
+
# token: config.channel_tokens[:default],
|
30
|
+
# template: "#{config.template_home}/default.*.erb"
|
31
|
+
|
32
|
+
ActiveNotifier.exec(:default, template: "orders/create")
|
33
|
+
# Use specific template file but not get by channel
|
34
|
+
# This will notifier message, use options:
|
35
|
+
# token: config.channel_tokens[:default],
|
36
|
+
# template: "#{config.template_home}/orders/create.*.erb"
|
37
|
+
|
38
|
+
ActiveNotifier.exec(:default, token: "special_token", data: { title: "Title", message: "Message" })
|
39
|
+
# Use specific token but not get by channel
|
40
|
+
# This will notifier message, use options:
|
41
|
+
# token: "special_token",
|
42
|
+
# template: "#{config.template_home}/default.*.erb"
|
43
|
+
# And the Hash data will inject to template, and get a dynamic message after parse template
|
44
|
+
```
|
45
|
+
|
46
|
+
### Use custom template
|
47
|
+
|
48
|
+
Set token and template home for Rails application
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
ActiveNotifier.configure do |config|
|
52
|
+
config.channel_tokens = { default: "xxx" }
|
53
|
+
config.template_home = Rails.root.join("app", "views", "active_notifier")
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
Set the default message template
|
58
|
+
|
59
|
+
> app/views/active_notifier/default.markdown.erb
|
60
|
+
|
61
|
+
```erb
|
62
|
+
## #{data[:title]}
|
63
|
+
> #{data[:message]}
|
64
|
+
```
|
65
|
+
|
66
|
+
Notify message
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
ActiveNotifier.exec(:default, data: { title: "Title", message: "Message" }) # => Notifer OK
|
70
|
+
```
|
71
|
+
|
72
|
+
### Send message with dynamic type
|
73
|
+
|
74
|
+
Message type will dynamic set according by valid template files, when we
|
75
|
+
set a template named `order.text.erb`
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
ActiveNotifier.configure do |config|
|
79
|
+
config.channel_tokens = { order: "xxx" }
|
80
|
+
config.template_home = Rails.root.join("app", "views", "active_notifier")
|
81
|
+
```
|
82
|
+
|
83
|
+
> app/views/active_notifier/order.text.erb
|
84
|
+
|
85
|
+
```erb
|
86
|
+
title: #{data[:title]}
|
87
|
+
message: #{data[:message]}
|
88
|
+
```
|
89
|
+
|
90
|
+
When there is only text type template for order, it will be OK the
|
91
|
+
notify message without set type option
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
ActiveNotifier.exec(:default, data: { title: "Title", message: "Message" }) # => Notifer message OK
|
95
|
+
```
|
96
|
+
|
97
|
+
Also, even though there are multiple types templates, like both `order.text.erb` and `order.markdown.erb`
|
98
|
+
are exist, we still can make it OK which no need for type option, just
|
99
|
+
set the priority type
|
100
|
+
|
101
|
+
> app/views/active_notifier/order.text.erb
|
102
|
+
|
103
|
+
```erb
|
104
|
+
title: #{data[:title]}
|
105
|
+
message: #{data[:message]}
|
106
|
+
```
|
107
|
+
|
108
|
+
> app/views/active_notifier/order.markdown.erb
|
109
|
+
|
110
|
+
```erb
|
111
|
+
## #{data[:title]}
|
112
|
+
> #{data[:message]}
|
113
|
+
```
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
# default priority_type is :markdown
|
117
|
+
ActiveNotifer.config.priority_type = :text
|
118
|
+
|
119
|
+
# Now this will choose text by default
|
120
|
+
ActiveNotifier.exec(:default, data: { title: "Title", message: "Message" }) # => Notifer message OK by text type template
|
121
|
+
```
|
122
|
+
|
123
|
+
### Set a short constant to notify(Rails initializer will set this by default)
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
ActiveNotifer.config.const_name = :Notifier
|
127
|
+
Notifer.exec(...)
|
128
|
+
```
|
129
|
+
|
130
|
+
### Other
|
131
|
+
|
132
|
+
See complete abilities of ActiveNotifier, please follow [Spec files](https://github.com/pinewong/active_notifier/blob/master/spec/active_adapter_spec.rb)
|
26
133
|
|
27
134
|
## Development
|
28
135
|
|
@@ -6,6 +6,16 @@ module ActiveNotifier
|
|
6
6
|
@adapter = adapter_class.new
|
7
7
|
end
|
8
8
|
|
9
|
+
# Notify message
|
10
|
+
# @abstract Implement through setting a real adapter, like :dingtalk
|
11
|
+
# @param token [String] Channel webhook token
|
12
|
+
# @param type [Symbol] Message type
|
13
|
+
# @param message [String] Message body
|
14
|
+
# @param options [Hash] ({}) Adapter message options,
|
15
|
+
# some adapters require some another options
|
16
|
+
# @raise [AdapterOptionsInvalidError]
|
17
|
+
# @raise [AdapterTypeInvalidError]
|
18
|
+
# @raise [MessageBlankError]
|
9
19
|
def notify(token, type, message, **options)
|
10
20
|
adapter.notify(token, type, message, **options)
|
11
21
|
end
|
@@ -8,9 +8,9 @@ module ActiveNotifier
|
|
8
8
|
error_message = "适配器 type 暂时只支持:#{VALID_TYPES.join(', ')}"
|
9
9
|
raise ActiveNotifier::AdapterTypeInvalidError, error_message
|
10
10
|
end
|
11
|
-
raise ActiveNotifier::MessageBlankError, "
|
11
|
+
raise ActiveNotifier::MessageBlankError, "message can't be blank, please check template file" if message.empty?
|
12
12
|
title = options[:title].to_s
|
13
|
-
raise ActiveNotifier::AdapterOptionsInvalidError, "
|
13
|
+
raise ActiveNotifier::AdapterOptionsInvalidError, "Dingtalk adapter require other options: title" if title.empty?
|
14
14
|
|
15
15
|
body = {
|
16
16
|
'msgtype' => type,
|
@@ -1,75 +1,103 @@
|
|
1
1
|
module ActiveNotifier
|
2
2
|
module Configurable
|
3
|
-
|
4
|
-
base.extend ClassMethods
|
5
|
-
end
|
3
|
+
extend ActiveSupport::Concern
|
6
4
|
|
7
5
|
module ClassMethods
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
# Configure for ActiveNotifier
|
7
|
+
# @yield [config] Give a Configuration instance for settings
|
8
|
+
# @raise [ConfigureError]
|
9
|
+
# @example
|
10
|
+
# ActiveNotifier.configure do |config|
|
11
|
+
# config.const_name = :Message
|
12
|
+
# config.adapter = :slack
|
13
|
+
# # ...
|
14
|
+
# end
|
12
15
|
def configure
|
13
16
|
yield config
|
14
17
|
end
|
18
|
+
|
19
|
+
def config
|
20
|
+
@config ||= Configuration.new
|
21
|
+
end
|
15
22
|
end
|
16
23
|
|
17
24
|
class Configuration
|
18
|
-
#
|
25
|
+
# Alias const for ActiveNotifier
|
26
|
+
# @!attribute [r]
|
19
27
|
attr_reader :const_name
|
20
28
|
|
29
|
+
# Set alias const for ActiveNotifier
|
30
|
+
# @param const_name [#to_sym] (:Notifier) Alias const name
|
31
|
+
# @example
|
32
|
+
# ActiveNotifier.config.const_name = :Message
|
33
|
+
# ::Message == ::ActiveNotifier # => true
|
21
34
|
def const_name=(const_name)
|
22
35
|
const_name = const_name.to_sym
|
23
|
-
const_name =
|
24
|
-
|
36
|
+
const_name = :Notifier if const_name.empty?
|
37
|
+
error_message = "const_name is already defined, please set another value"
|
38
|
+
raise ActiveNotifier::ConfigureError, error_message if Kernel.const_defined?(const_name)
|
25
39
|
Kernel.const_set(const_name, ActiveNotifier)
|
26
40
|
@const_name = const_name
|
27
41
|
end
|
28
42
|
|
29
|
-
#
|
43
|
+
# Message Adapter
|
30
44
|
def adapter
|
31
45
|
@adapter || :dingtalk
|
32
46
|
end
|
33
47
|
|
48
|
+
# Set message Adapter
|
49
|
+
# @param adapter [#to_sym] (:dingtalk)
|
34
50
|
def adapter=(adapter)
|
35
51
|
adapter = adapter.to_sym
|
36
52
|
unless adapters_with_base_url.key?(adapter)
|
37
|
-
raise ActiveNotifier::ConfigureError, "adapter
|
53
|
+
raise ActiveNotifier::ConfigureError, "adapter only support: #{adapters_with_base_url.keys.join(', ')}"
|
38
54
|
end
|
39
55
|
@adapter = adapter
|
40
56
|
end
|
41
57
|
|
42
|
-
#
|
58
|
+
# The tokens of channel
|
43
59
|
def channel_tokens
|
44
60
|
@channel_tokens || {}
|
45
61
|
end
|
46
62
|
|
63
|
+
# Set the tokens of channel
|
64
|
+
# @param channel_tokens [Hash] ({})
|
65
|
+
# @example
|
66
|
+
# ActiveNotifier.config.channel_tokens = {
|
67
|
+
# default: "xxx",
|
68
|
+
# order: "xxx"
|
69
|
+
# }
|
47
70
|
def channel_tokens=(channel_tokens)
|
48
71
|
channel_tokens = channel_tokens.to_h.symbolize_keys
|
49
72
|
@channel_tokens = channel_tokens
|
50
73
|
end
|
51
74
|
|
52
|
-
#
|
75
|
+
# The template home directory
|
53
76
|
def template_home
|
54
77
|
@template_home || "#{File.expand_path(__dir__)}/templates"
|
55
78
|
end
|
56
79
|
|
80
|
+
# Set the template home directory
|
81
|
+
# @param template_home [String] (lib/active_notifier/templates)
|
57
82
|
def template_home=(template_home)
|
58
83
|
template_home = template_home.to_s
|
59
|
-
raise ActiveNotifier::ConfigureError, "
|
84
|
+
raise ActiveNotifier::ConfigureError, "template_home value can't be blank" if template_home.empty?
|
60
85
|
@template_home = template_home
|
61
86
|
end
|
62
87
|
|
63
|
-
#
|
88
|
+
# The priority type for message, if notifier execute without type option and there are multiple valid types,
|
89
|
+
# then ActiveNotifier will use the template with this type first
|
64
90
|
def priority_type
|
65
91
|
@priority_type || :markdown
|
66
92
|
end
|
67
93
|
|
94
|
+
# Set the priority type
|
95
|
+
# @param priority_type [#to_sym] (:markdown)
|
68
96
|
def priority_type=(priority_type)
|
69
97
|
@priority_type = priority_type.to_sym
|
70
98
|
end
|
71
99
|
|
72
|
-
#
|
100
|
+
# The adapters and its base url
|
73
101
|
def adapters_with_base_url
|
74
102
|
@adapters_with_base_url ||= {
|
75
103
|
dingtalk: "https://oapi.dingtalk.com/robot/send?access_token="
|
data/lib/active_notifier/core.rb
CHANGED
@@ -1,25 +1,38 @@
|
|
1
1
|
module ActiveNotifier
|
2
2
|
module Core
|
3
|
-
|
4
|
-
base.extend ClassMethods
|
5
|
-
end
|
3
|
+
extend ActiveSupport::Concern
|
6
4
|
|
7
5
|
module ClassMethods
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
6
|
+
# Message execute
|
7
|
+
# @param channel [#to_sym] Message channel, it will set template and token together
|
8
|
+
# @param options [Hash] ({})
|
9
|
+
# @option options [String] :template (nil) Message template, will use `options[:channel]` when this value is blank
|
10
|
+
# @option options [String, Symbol] :token (nil) Message webhook token,
|
11
|
+
# will use `options[:channel]` when this value is blank
|
12
|
+
# @option options [Symbol] :type (nil) Message type, it will use a specific template file, for example,
|
13
|
+
# when type is :text and template is order, then ActiveNotifier will choose `order.text.erb` for template file
|
14
|
+
# @option options [Hash] :data ({}) Message variable data for template, it can used in erb template file:
|
15
|
+
# ## #{data[:title]}
|
16
|
+
# > #{data[:order].to_json}
|
17
|
+
# @option options [Anything] Other options will used in adapter message execute,
|
18
|
+
# like Dingtalk require title for message, we can pass it here:
|
19
|
+
# `ActiveNotifer.exec(:default, title: "dingtalk title")`
|
20
|
+
# @raise [TemplateNotFoundError]
|
21
|
+
# @raise [UndefinedTokenError]
|
22
|
+
# @raise [AdapterOptionsInvalidError]
|
23
|
+
# @raise [AdapterTypeInvalidError]
|
24
|
+
# @raise [MessageBlankError]
|
25
|
+
# @example
|
26
|
+
# ActiveNotifer.config.channel_tokens = {
|
27
|
+
# default: "xxx"
|
28
|
+
# }
|
29
|
+
# ActiveNotifier.exec(:default)
|
17
30
|
def exec(channel, **options)
|
18
31
|
channel = channel&.to_sym
|
19
|
-
raise ArgumentError, "
|
32
|
+
raise ArgumentError, ":channel can't be blank" if channel.blank?
|
20
33
|
template = options[:template].to_s.presence || channel.to_s
|
21
34
|
token = get_token(channel, options[:token])
|
22
|
-
raise UndefinedTokenError
|
35
|
+
raise UndefinedTokenError if token.blank?
|
23
36
|
type = options[:type]&.to_sym.presence || guess_type_by_files(template)
|
24
37
|
data = options[:data].to_h
|
25
38
|
|
@@ -1,22 +1,9 @@
|
|
1
1
|
module ActiveNotifier
|
2
|
-
# 顶级错误
|
3
2
|
class Error < StandardError; end
|
4
|
-
|
5
|
-
# 配置错误
|
6
3
|
class ConfigureError < Error; end
|
7
|
-
|
8
|
-
# 模板未找到
|
9
4
|
class TemplateNotFoundError < Error; end
|
10
|
-
|
11
|
-
# 消息为空
|
12
5
|
class MessageBlankError < Error; end
|
13
|
-
|
14
|
-
# 适配器 type 无效
|
15
6
|
class AdapterTypeInvalidError < Error; end
|
16
|
-
|
17
|
-
# 适配器 options 无效
|
18
7
|
class AdapterOptionsInvalidError < Error; end
|
19
|
-
|
20
|
-
# Token 未定义
|
21
8
|
class UndefinedTokenError < Error; end
|
22
9
|
end
|
data/lib/active_notifier.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_notifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pine Wong
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|