active_notifier 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/Gemfile +10 -0
- data/README.md +1 -1
- data/active_notifier.gemspec +1 -10
- data/lib/active_notifier/configurable.rb +5 -4
- data/lib/active_notifier/errors.rb +5 -3
- data/lib/active_notifier/{core.rb → notifiable.rb} +15 -4
- data/lib/active_notifier/notifier.rb +7 -0
- data/lib/active_notifier/notifier_adapters/abstract.rb +20 -0
- data/lib/active_notifier/notifier_adapters/dingtalk.rb +68 -0
- data/lib/active_notifier/version.rb +1 -1
- data/lib/active_notifier.rb +7 -13
- metadata +13 -131
- data/lib/active_notifier/adaptable.rb +0 -28
- data/lib/active_notifier/adapters/abstract_adapter.rb +0 -24
- data/lib/active_notifier/adapters/dingtalk_adapter.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd10b598192c1508228777489f96764cd880b15da9ab6978250abf8fa5ac19ca
|
4
|
+
data.tar.gz: 002bbc7be0c8a7cc89490a9b863978150d870ac3ce12afa4a641ca76bbeffbda
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14469a324d498c52244cb44053cf310e185f68053aeec83f8f286cffdf52ad2510ef9241fbef246354a9ba9cf597f19127eefa82cffa04b585c2521fbbc91c7c
|
7
|
+
data.tar.gz: 0aba370debb2b69d882165c7dbcc855ee1c6a5194facb0cd03007e0b23af4147996a3e18ef673a3f6f7d60d6d9f3397a1562652aadc1fefc984598c99eaa2bc3
|
data/.rubocop.yml
CHANGED
data/Gemfile
CHANGED
@@ -4,3 +4,13 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
|
4
4
|
|
5
5
|
# Specify your gem's dependencies in active_notifier.gemspec
|
6
6
|
gemspec
|
7
|
+
|
8
|
+
gem "bundler", "~> 1.16"
|
9
|
+
gem "byebug"
|
10
|
+
gem "codecov", ">= 0.1.10", require: false
|
11
|
+
gem "rake", "~> 10.0"
|
12
|
+
gem "rspec", "~> 3.7", require: false
|
13
|
+
gem "rubocop", "~> 0.58.1", require: false
|
14
|
+
gem "simplecov", require: false
|
15
|
+
gem "vcr", "~> 4.0", require: false
|
16
|
+
gem "webmock", "~> 3.4", ">= 3.4.2", require: false
|
data/README.md
CHANGED
@@ -103,7 +103,7 @@ Notifer.exec(...)
|
|
103
103
|
|
104
104
|
* [More Examples](https://github.com/pinewong/active_notifier/blob/master/spec/active_notifier_spec.rb)
|
105
105
|
* [RDoc](https://www.rubydoc.info/github/pinewong/active_notifier)
|
106
|
-
* [Gem RDoc](http://www.rubydoc.info/gems/active_notifier/0.
|
106
|
+
* [Gem RDoc](http://www.rubydoc.info/gems/active_notifier/0.4.0)
|
107
107
|
|
108
108
|
## Development
|
109
109
|
|
data/active_notifier.gemspec
CHANGED
@@ -30,15 +30,6 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
31
|
spec.require_paths = ["lib"]
|
32
32
|
|
33
|
+
spec.add_dependency "active_adapter", "~> 0.1.0"
|
33
34
|
spec.add_dependency "activesupport", "~> 5.0"
|
34
|
-
|
35
|
-
spec.add_development_dependency "bundler", "~> 1.16"
|
36
|
-
spec.add_development_dependency "byebug"
|
37
|
-
spec.add_development_dependency "codecov", ">= 0.1.10"
|
38
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
39
|
-
spec.add_development_dependency "rspec", "~> 3.7"
|
40
|
-
spec.add_development_dependency "rubocop", "~> 0.58.1"
|
41
|
-
spec.add_development_dependency "simplecov"
|
42
|
-
spec.add_development_dependency "vcr", "~> 4.0"
|
43
|
-
spec.add_development_dependency "webmock", "~> 3.4", ">= 3.4.2"
|
44
35
|
end
|
@@ -35,8 +35,9 @@ module ActiveNotifier
|
|
35
35
|
def const_name=(const_name)
|
36
36
|
const_name = const_name.to_sym
|
37
37
|
const_name = :Notifier if const_name.empty?
|
38
|
-
|
39
|
-
|
38
|
+
if Kernel.const_defined?(const_name)
|
39
|
+
raise ActiveNotifier::ConfigureError, "const_name is already defined, please set another value"
|
40
|
+
end
|
40
41
|
Kernel.const_set(const_name, ActiveNotifier)
|
41
42
|
@const_name = const_name
|
42
43
|
end
|
@@ -62,7 +63,7 @@ module ActiveNotifier
|
|
62
63
|
end
|
63
64
|
|
64
65
|
# Set the tokens of channel
|
65
|
-
# @param channel_tokens [
|
66
|
+
# @param channel_tokens [#to_h] ({})
|
66
67
|
# @example
|
67
68
|
# ActiveNotifier.config.channel_tokens = {
|
68
69
|
# default: "xxx",
|
@@ -79,7 +80,7 @@ module ActiveNotifier
|
|
79
80
|
end
|
80
81
|
|
81
82
|
# Set the template home directory
|
82
|
-
# @param template_home [
|
83
|
+
# @param template_home [#to_s] (lib/active_notifier/templates)
|
83
84
|
def template_home=(template_home)
|
84
85
|
template_home = template_home.to_s
|
85
86
|
raise ActiveNotifier::ConfigureError, "template_home value can't be blank" if template_home.empty?
|
@@ -1,9 +1,11 @@
|
|
1
1
|
module ActiveNotifier
|
2
2
|
class Error < StandardError; end
|
3
|
+
|
3
4
|
class ConfigureError < Error; end
|
5
|
+
|
4
6
|
class TemplateNotFoundError < Error; end
|
5
|
-
|
6
|
-
class AdapterTypeInvalidError < Error; end
|
7
|
-
class AdapterOptionsInvalidError < Error; end
|
7
|
+
|
8
8
|
class UndefinedTokenError < Error; end
|
9
|
+
|
10
|
+
class AdapterError < Error; end
|
9
11
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module ActiveNotifier
|
2
|
-
module
|
2
|
+
module Notifiable
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
@@ -49,11 +49,11 @@ module ActiveNotifier
|
|
49
49
|
data = options[:data].to_h
|
50
50
|
token = fetch_token(token, token_channel, channel)
|
51
51
|
template ||= channel
|
52
|
-
type ||= get_type_by_template(template)
|
52
|
+
type ||= get_type_by_template(template) || :text
|
53
53
|
message ||= get_message(template, type, data)
|
54
54
|
adapter_options = options.except(:token, :message, :token_channel, :template, :adapter, :type, :data)
|
55
55
|
adapter ||= ActiveNotifier.config.adapter.to_sym
|
56
|
-
|
56
|
+
notify(adapter, token, type, message, adapter_options)
|
57
57
|
end
|
58
58
|
|
59
59
|
private
|
@@ -89,7 +89,7 @@ module ActiveNotifier
|
|
89
89
|
# @param template [Symbol, nil]
|
90
90
|
# @param type [Symbol]
|
91
91
|
# @param data [Hash]
|
92
|
-
# @return [String
|
92
|
+
# @return [String]
|
93
93
|
def get_message(template, type, data)
|
94
94
|
return unless template
|
95
95
|
template_data = File.read("#{ActiveNotifier.config.template_home}/#{template}.#{type}.erb")
|
@@ -97,6 +97,17 @@ module ActiveNotifier
|
|
97
97
|
rescue Errno::ENOENT => e
|
98
98
|
raise ActiveNotifier::TemplateNotFoundError, e
|
99
99
|
end
|
100
|
+
|
101
|
+
# @param adapter [Symbol]
|
102
|
+
# @param token [String]
|
103
|
+
# @param type [Symbol]
|
104
|
+
# @param message [String]
|
105
|
+
# @param adapter_options [Hash]
|
106
|
+
# @return [Response]
|
107
|
+
def notify(adapter, token, type, message, adapter_options)
|
108
|
+
notifier = ActiveNotifier::Notifier.adapt(adapter)
|
109
|
+
notifier.notify(token, type, message, adapter_options)
|
110
|
+
end
|
100
111
|
end
|
101
112
|
end
|
102
113
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ActiveNotifier
|
2
|
+
module NotifierAdapters
|
3
|
+
class Abstract < ActiveAdapter::Abstract
|
4
|
+
# Notify message
|
5
|
+
#
|
6
|
+
# @abstract Implement through setting a real adapter, like :dingtalk
|
7
|
+
# @param token [String] Channel webhook token
|
8
|
+
# @param type [Symbol] Message type
|
9
|
+
# @param message [String] Message body
|
10
|
+
# @param options [Hash] Adapter message options, some adapters require some another options
|
11
|
+
# @raise [AdapterOptionsInvalidError]
|
12
|
+
# @raise [AdapterTypeInvalidError]
|
13
|
+
# @raise [MessageBlankError]
|
14
|
+
# @see ActiveNotitifier::exec for usage
|
15
|
+
def notify(token, type, message, **options)
|
16
|
+
adapter.notify(token, type, message, **options)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module ActiveNotifier
|
2
|
+
module NotifierAdapters
|
3
|
+
class Dingtalk < ActiveAdapter::Implement
|
4
|
+
VALID_TYPES = %i[text markdown].freeze
|
5
|
+
|
6
|
+
# (see NotifierAdapters::Abstract#notify)
|
7
|
+
# @param title [#to_s]
|
8
|
+
# @param at_mobiles [#to_a] ([])
|
9
|
+
# @param is_at_all [Boolean] (false)
|
10
|
+
def notify(token, type, message, **options)
|
11
|
+
webhook = "#{ActiveNotifier.config.adapters_with_base_url.fetch(:dingtalk)}#{token}"
|
12
|
+
unless VALID_TYPES.include?(type)
|
13
|
+
error_message = "The Dingtalk adapter only support types: #{VALID_TYPES.join(', ')}"
|
14
|
+
raise ActiveNotifier::AdapterError, error_message
|
15
|
+
end
|
16
|
+
if message.empty?
|
17
|
+
error_message = "Message of the Dingtalk adapter can't be blank, please check template file"
|
18
|
+
raise ActiveNotifier::AdapterError, error_message
|
19
|
+
end
|
20
|
+
at_mobiles = options[:at_mobiles].to_a
|
21
|
+
is_at_all = !!options[:is_at_all]
|
22
|
+
|
23
|
+
body = case type
|
24
|
+
when :text
|
25
|
+
get_text_body(message, at_mobiles, is_at_all)
|
26
|
+
when :markdown
|
27
|
+
title = options[:title].to_s
|
28
|
+
raise ActiveNotifier::AdapterError, "Dingtalk adapter require other options: title" if title.empty?
|
29
|
+
get_markdown_body(title, message, at_mobiles, is_at_all)
|
30
|
+
end
|
31
|
+
headers = {
|
32
|
+
'Content-Type' => 'application/json',
|
33
|
+
'Accept' => 'application/json'
|
34
|
+
}
|
35
|
+
Net::HTTP.post(URI(webhook), body.to_json, headers)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def get_text_body(content, at_mobiles, is_at_all)
|
41
|
+
{
|
42
|
+
msgtype: "text",
|
43
|
+
text: {
|
44
|
+
content: content
|
45
|
+
},
|
46
|
+
at: {
|
47
|
+
atMobiles: at_mobiles,
|
48
|
+
isAtAll: is_at_all
|
49
|
+
}
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_markdown_body(title, text, at_mobiles, is_at_all)
|
54
|
+
{
|
55
|
+
msgtype: "markdown",
|
56
|
+
markdown: {
|
57
|
+
title: title,
|
58
|
+
text: text
|
59
|
+
},
|
60
|
+
at: {
|
61
|
+
atMobiles: at_mobiles,
|
62
|
+
isAtAll: is_at_all
|
63
|
+
}
|
64
|
+
}
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/lib/active_notifier.rb
CHANGED
@@ -1,33 +1,27 @@
|
|
1
|
-
require "
|
1
|
+
require "net/http"
|
2
2
|
require "active_support/all"
|
3
|
+
require "active_adapter"
|
4
|
+
require "active_notifier/version"
|
5
|
+
require "active_notifier/notifier"
|
3
6
|
|
4
7
|
module ActiveNotifier
|
5
8
|
extend ActiveSupport::Autoload
|
6
9
|
|
7
10
|
autoload :Configurable
|
8
|
-
autoload :
|
9
|
-
autoload :Adaptable
|
11
|
+
autoload :Notifiable
|
10
12
|
|
11
13
|
eager_autoload do
|
12
14
|
autoload_at "active_notifier/errors" do
|
13
15
|
autoload :Error
|
14
16
|
autoload :ConfigureError
|
15
17
|
autoload :TemplateNotFoundError
|
16
|
-
autoload :MessageBlankError
|
17
|
-
autoload :AdapterTypeInvalidError
|
18
|
-
autoload :AdapterOptionsInvalidError
|
19
18
|
autoload :UndefinedTokenError
|
19
|
+
autoload :AdapterError
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
autoload_under "adapters" do
|
24
|
-
autoload :AbstractAdapter
|
25
|
-
autoload :DingtalkAdapter
|
26
|
-
end
|
27
|
-
|
28
23
|
include Configurable
|
29
|
-
include
|
30
|
-
include Adaptable
|
24
|
+
include Notifiable
|
31
25
|
|
32
26
|
ActiveSupport.on_load(:after_initialize) do
|
33
27
|
Kernel.const_set(ActiveNotifier.config.const_name || :Notifier, ActiveNotifier)
|
metadata
CHANGED
@@ -1,161 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_notifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.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-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: active_adapter
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.1.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
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.16'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '1.16'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: byebug
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: codecov
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 0.1.10
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: 0.1.10
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rake
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '10.0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - "~>"
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '10.0'
|
26
|
+
version: 0.1.0
|
83
27
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '3.7'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - "~>"
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '3.7'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: rubocop
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - "~>"
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: 0.58.1
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - "~>"
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: 0.58.1
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: simplecov
|
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'
|
125
|
-
- !ruby/object:Gem::Dependency
|
126
|
-
name: vcr
|
127
|
-
requirement: !ruby/object:Gem::Requirement
|
128
|
-
requirements:
|
129
|
-
- - "~>"
|
130
|
-
- !ruby/object:Gem::Version
|
131
|
-
version: '4.0'
|
132
|
-
type: :development
|
133
|
-
prerelease: false
|
134
|
-
version_requirements: !ruby/object:Gem::Requirement
|
135
|
-
requirements:
|
136
|
-
- - "~>"
|
137
|
-
- !ruby/object:Gem::Version
|
138
|
-
version: '4.0'
|
139
|
-
- !ruby/object:Gem::Dependency
|
140
|
-
name: webmock
|
28
|
+
name: activesupport
|
141
29
|
requirement: !ruby/object:Gem::Requirement
|
142
30
|
requirements:
|
143
31
|
- - "~>"
|
144
32
|
- !ruby/object:Gem::Version
|
145
|
-
version: '
|
146
|
-
|
147
|
-
- !ruby/object:Gem::Version
|
148
|
-
version: 3.4.2
|
149
|
-
type: :development
|
33
|
+
version: '5.0'
|
34
|
+
type: :runtime
|
150
35
|
prerelease: false
|
151
36
|
version_requirements: !ruby/object:Gem::Requirement
|
152
37
|
requirements:
|
153
38
|
- - "~>"
|
154
39
|
- !ruby/object:Gem::Version
|
155
|
-
version: '
|
156
|
-
- - ">="
|
157
|
-
- !ruby/object:Gem::Version
|
158
|
-
version: 3.4.2
|
40
|
+
version: '5.0'
|
159
41
|
description: Notify message through webhooks.
|
160
42
|
email:
|
161
43
|
- pinewong@163.com
|
@@ -174,12 +56,12 @@ files:
|
|
174
56
|
- bin/console
|
175
57
|
- bin/setup
|
176
58
|
- lib/active_notifier.rb
|
177
|
-
- lib/active_notifier/adaptable.rb
|
178
|
-
- lib/active_notifier/adapters/abstract_adapter.rb
|
179
|
-
- lib/active_notifier/adapters/dingtalk_adapter.rb
|
180
59
|
- lib/active_notifier/configurable.rb
|
181
|
-
- lib/active_notifier/core.rb
|
182
60
|
- lib/active_notifier/errors.rb
|
61
|
+
- lib/active_notifier/notifiable.rb
|
62
|
+
- lib/active_notifier/notifier.rb
|
63
|
+
- lib/active_notifier/notifier_adapters/abstract.rb
|
64
|
+
- lib/active_notifier/notifier_adapters/dingtalk.rb
|
183
65
|
- lib/active_notifier/templates/default.markdown.erb
|
184
66
|
- lib/active_notifier/templates/default.text.erb
|
185
67
|
- lib/active_notifier/version.rb
|
@@ -1,28 +0,0 @@
|
|
1
|
-
module ActiveNotifier
|
2
|
-
module Adaptable
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
def adapters
|
7
|
-
@adapters ||= {}
|
8
|
-
end
|
9
|
-
|
10
|
-
def adapt(adapter)
|
11
|
-
adapters[adapter.to_sym] ||= begin
|
12
|
-
adapter_class = get_adapter_class(adapter)
|
13
|
-
abstract_adapter_class.new(adapter_class)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def get_adapter_class(adapter)
|
20
|
-
"ActiveNotifier::#{adapter.to_s.camelize}Adapter".constantize
|
21
|
-
end
|
22
|
-
|
23
|
-
def abstract_adapter_class
|
24
|
-
"ActiveNotifier::AbstractAdapter".constantize
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module ActiveNotifier
|
2
|
-
class AbstractAdapter
|
3
|
-
attr_reader :adapter
|
4
|
-
|
5
|
-
def initialize(adapter_class)
|
6
|
-
@adapter = adapter_class.new
|
7
|
-
end
|
8
|
-
|
9
|
-
# Notify message
|
10
|
-
#
|
11
|
-
# @abstract Implement through setting a real adapter, like :dingtalk
|
12
|
-
# @param token [String] Channel webhook token
|
13
|
-
# @param type [Symbol] Message type
|
14
|
-
# @param message [String] Message body
|
15
|
-
# @param options [Hash] Adapter message options, some adapters require some another options
|
16
|
-
# @raise [AdapterOptionsInvalidError]
|
17
|
-
# @raise [AdapterTypeInvalidError]
|
18
|
-
# @raise [MessageBlankError]
|
19
|
-
# @see ActiveNotitifier::exec for usage
|
20
|
-
def notify(token, type, message, **options)
|
21
|
-
adapter.notify(token, type, message, **options)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module ActiveNotifier
|
2
|
-
class DingtalkAdapter
|
3
|
-
VALID_TYPES = %i[markdown].freeze
|
4
|
-
|
5
|
-
# (see AbstractAdapter#notify)
|
6
|
-
def notify(token, type, message, **options)
|
7
|
-
webhook = "#{ActiveNotifier.config.adapters_with_base_url.fetch(:dingtalk)}#{token}"
|
8
|
-
unless VALID_TYPES.include?(type)
|
9
|
-
error_message = "The Dingtalk adapter only support types: #{VALID_TYPES.join(', ')}"
|
10
|
-
raise ActiveNotifier::AdapterTypeInvalidError, error_message
|
11
|
-
end
|
12
|
-
raise ActiveNotifier::MessageBlankError, "message can't be blank, please check template file" if message.empty?
|
13
|
-
title = options[:title]&.to_s
|
14
|
-
raise ActiveNotifier::AdapterOptionsInvalidError, "Dingtalk adapter require other options: title" if title.empty?
|
15
|
-
|
16
|
-
body = {
|
17
|
-
'msgtype' => type,
|
18
|
-
type => {
|
19
|
-
'title': title,
|
20
|
-
'text': message
|
21
|
-
}
|
22
|
-
}
|
23
|
-
headers = {
|
24
|
-
'Content-Type' => 'application/json',
|
25
|
-
'Accept' => 'application/json'
|
26
|
-
}
|
27
|
-
Net::HTTP.post(URI(webhook), body.to_json, headers)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|