pechkin 1.4.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pechkin/{configuration/model.rb → bot.rb} +0 -1
- data/lib/pechkin/channel.rb +1 -57
- data/lib/pechkin/configuration/configuration_loader_channels.rb +42 -3
- data/lib/pechkin/configuration.rb +0 -1
- data/lib/pechkin/handler.rb +2 -7
- data/lib/pechkin/message.rb +64 -0
- data/lib/pechkin/version.rb +1 -1
- data/lib/pechkin.rb +3 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c18e97f32b4fe42a3bd649c43563abc81a13fe49d5c2332f54129b1babc61767
|
4
|
+
data.tar.gz: bcc9dcdbccc1041ed9377b1e4bbce4113a6820ca798efceeefc9476b30eb8499
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d567c46d62df87a39e701f086c51a753616cb3e1acc104829675a1d091a9907110ed1e807f0253782b125a55017918c2ddedf3716ad58af77b0a96296c26a4e
|
7
|
+
data.tar.gz: b7add41faa2da882200c5a47fe4a847a97ea3df7a8c9cb69796e200405e7a3a19fd49cf0e21776484a46dc713ca8312bfce86ab2230c003c9325c4d86ddab526
|
data/lib/pechkin/channel.rb
CHANGED
@@ -1,59 +1,3 @@
|
|
1
1
|
module Pechkin
|
2
|
-
|
3
|
-
class Chanel
|
4
|
-
attr_accessor :logger
|
5
|
-
|
6
|
-
def initialize(connector, channel_list, logger = ::Logger.new($stdout))
|
7
|
-
@connector = connector
|
8
|
-
@channel_list = channel_list
|
9
|
-
@channel_list = [channel_list] unless channel_list.is_a?(Array)
|
10
|
-
@logger = logger
|
11
|
-
end
|
12
|
-
|
13
|
-
def send_message(message, data, message_desc)
|
14
|
-
text = message.nil? ? '' : Message.new(data).render(message)
|
15
|
-
|
16
|
-
message_desc = substitute(data, message_desc)
|
17
|
-
|
18
|
-
logger.warn 'Resulting text is empty' if text.empty?
|
19
|
-
results = @channel_list.map do |id|
|
20
|
-
@connector.send_message(id, text, message_desc)
|
21
|
-
end
|
22
|
-
|
23
|
-
process_results(message, results)
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def substitute(data, message_desc)
|
29
|
-
substitute_recursive(Substitute.new(data), message_desc)
|
30
|
-
end
|
31
|
-
|
32
|
-
def substitute_recursive(substitutions, object)
|
33
|
-
case object
|
34
|
-
when String
|
35
|
-
substitutions.process(object)
|
36
|
-
when Array
|
37
|
-
object.map { |o| substitute_recursive(substitutions, o) }
|
38
|
-
when Hash
|
39
|
-
r = {}
|
40
|
-
object.each { |k, v| r[k] = substitute_recursive(substitutions, v) }
|
41
|
-
r
|
42
|
-
else
|
43
|
-
object
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def process_results(message, results)
|
48
|
-
success, error = results.partition { |_chat, code, _body| code < 400 }
|
49
|
-
error.each do |chat, code, body|
|
50
|
-
logger.error "#{message} => #{chat}[HTTP #{code}]: #{body}"
|
51
|
-
end
|
52
|
-
|
53
|
-
{
|
54
|
-
successful: success.map(&:first),
|
55
|
-
errors: error
|
56
|
-
}
|
57
|
-
end
|
58
|
-
end
|
2
|
+
Channel = Struct.new(:chat_ids, :connector, :messages, keyword_init: true)
|
59
3
|
end
|
@@ -61,14 +61,53 @@ module Pechkin
|
|
61
61
|
message_config = YAML.safe_load(IO.read(file))
|
62
62
|
name = File.basename(file, '.yml')
|
63
63
|
|
64
|
-
|
65
|
-
|
66
|
-
|
64
|
+
# Dirty workaround. I need to recursively load templates. When doing it
|
65
|
+
# we looking for {'template': '...path to template..' } objects. But we
|
66
|
+
# don't want to force user write something like:
|
67
|
+
# text:
|
68
|
+
# template: '... path to main template...'
|
69
|
+
# because it's too mouthful for such common case.
|
70
|
+
#
|
71
|
+
# So now we pull main template out, then load everyting else. Then put
|
72
|
+
# it back.
|
73
|
+
template = nil
|
74
|
+
if message_config.key?('template')
|
75
|
+
template = get_template(message_config['template'])
|
76
|
+
message_config.delete('template')
|
77
|
+
end
|
78
|
+
|
79
|
+
message_config = load_templates(message_config)
|
80
|
+
message_config['template'] = template unless template.nil?
|
81
|
+
|
82
|
+
messages[name] = Message.new(message_config)
|
67
83
|
end
|
68
84
|
|
69
85
|
messages
|
70
86
|
end
|
71
87
|
|
88
|
+
def load_templates(object)
|
89
|
+
case object
|
90
|
+
when String
|
91
|
+
object
|
92
|
+
when Array
|
93
|
+
object.map { |o| load_templates(o) }
|
94
|
+
when Hash
|
95
|
+
if object.key?('template')
|
96
|
+
msg = 'When using template only 1 KV pair allowed'
|
97
|
+
raise ConfigurationError, msg unless object.size == 1
|
98
|
+
|
99
|
+
# Replace whole object with created template.
|
100
|
+
get_template(object['template'])
|
101
|
+
else
|
102
|
+
r = {}
|
103
|
+
object.each { |k, v| r[k] = load_templates(v) }
|
104
|
+
r
|
105
|
+
end
|
106
|
+
else
|
107
|
+
object
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
72
111
|
def get_template(path)
|
73
112
|
msg = "Can't find template: #{path}"
|
74
113
|
raise ConfigurationError, msg unless @views.key?(path)
|
data/lib/pechkin/handler.rb
CHANGED
@@ -95,13 +95,8 @@ module Pechkin
|
|
95
95
|
def prepare_message(channel_id, msg_id, data)
|
96
96
|
channel_config = fetch_channel(channel_id)
|
97
97
|
# Find message and try substitute values to message parameters.
|
98
|
-
|
99
|
-
|
100
|
-
data = (message_config['variables'] || {}).merge(data)
|
101
|
-
template = message_config['template']
|
102
|
-
|
103
|
-
text = ''
|
104
|
-
text = template.render(data) unless template.nil?
|
98
|
+
message = fetch_message(channel_config, msg_id)
|
99
|
+
message_config, text = message.prepare(data)
|
105
100
|
|
106
101
|
[channel_config, message_config, text]
|
107
102
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Pechkin
|
2
|
+
# Message object
|
3
|
+
#
|
4
|
+
# TBD
|
5
|
+
class Message
|
6
|
+
def initialize(message)
|
7
|
+
@message = message
|
8
|
+
end
|
9
|
+
|
10
|
+
def prepare(data)
|
11
|
+
data = (@message['variables'] || {}).merge(data)
|
12
|
+
# Find message and try substitute values to message parameters.
|
13
|
+
message_config = render(data, substitute(data, @message))
|
14
|
+
text = ''
|
15
|
+
text = message_config.delete('template') if message_config.key?('template')
|
16
|
+
|
17
|
+
[message_config, text]
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_h
|
21
|
+
Marshal.load(Marshal.dump(@message))
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def substitute(data, message_desc)
|
27
|
+
substitute_recursive(Substitute.new(data), message_desc)
|
28
|
+
end
|
29
|
+
|
30
|
+
def substitute_recursive(substitutions, object)
|
31
|
+
case object
|
32
|
+
when String
|
33
|
+
substitutions.process(object)
|
34
|
+
when Array
|
35
|
+
object.map { |o| substitute_recursive(substitutions, o) }
|
36
|
+
when Hash
|
37
|
+
r = {}
|
38
|
+
object.each { |k, v| r[k] = substitute_recursive(substitutions, v) }
|
39
|
+
r
|
40
|
+
else
|
41
|
+
object
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def render(data, message_desc)
|
46
|
+
render_recursive(data, message_desc)
|
47
|
+
end
|
48
|
+
|
49
|
+
def render_recursive(data, object)
|
50
|
+
case object
|
51
|
+
when MessageTemplate
|
52
|
+
object.render(data)
|
53
|
+
when Array
|
54
|
+
object.map { |o| render_recursive(data, o) }
|
55
|
+
when Hash
|
56
|
+
r = {}
|
57
|
+
object.each { |k, v| r[k] = render_recursive(data, v) }
|
58
|
+
r
|
59
|
+
else
|
60
|
+
object
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/pechkin/version.rb
CHANGED
data/lib/pechkin.rb
CHANGED
@@ -8,13 +8,15 @@ require 'htauth'
|
|
8
8
|
require 'base64'
|
9
9
|
|
10
10
|
require_relative 'pechkin/cli'
|
11
|
+
require_relative 'pechkin/bot'
|
12
|
+
require_relative 'pechkin/channel'
|
13
|
+
require_relative 'pechkin/message'
|
11
14
|
require_relative 'pechkin/command'
|
12
15
|
require_relative 'pechkin/exceptions'
|
13
16
|
require_relative 'pechkin/handler'
|
14
17
|
require_relative 'pechkin/message_matcher'
|
15
18
|
require_relative 'pechkin/message_template'
|
16
19
|
require_relative 'pechkin/connector'
|
17
|
-
require_relative 'pechkin/channel'
|
18
20
|
require_relative 'pechkin/configuration'
|
19
21
|
require_relative 'pechkin/substitute'
|
20
22
|
require_relative 'pechkin/prometheus_utils'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pechkin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Arkhanhelsky
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-10-
|
11
|
+
date: 2021-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: htauth
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- lib/pechkin/app/app_error.rb
|
82
82
|
- lib/pechkin/app/request_handler.rb
|
83
83
|
- lib/pechkin/auth.rb
|
84
|
+
- lib/pechkin/bot.rb
|
84
85
|
- lib/pechkin/channel.rb
|
85
86
|
- lib/pechkin/cli.rb
|
86
87
|
- lib/pechkin/command.rb
|
@@ -95,13 +96,13 @@ files:
|
|
95
96
|
- lib/pechkin/configuration/configuration_loader_bots.rb
|
96
97
|
- lib/pechkin/configuration/configuration_loader_channels.rb
|
97
98
|
- lib/pechkin/configuration/configuration_loader_views.rb
|
98
|
-
- lib/pechkin/configuration/model.rb
|
99
99
|
- lib/pechkin/connector.rb
|
100
100
|
- lib/pechkin/connector/base.rb
|
101
101
|
- lib/pechkin/connector/slack.rb
|
102
102
|
- lib/pechkin/connector/telegram.rb
|
103
103
|
- lib/pechkin/exceptions.rb
|
104
104
|
- lib/pechkin/handler.rb
|
105
|
+
- lib/pechkin/message.rb
|
105
106
|
- lib/pechkin/message_matcher.rb
|
106
107
|
- lib/pechkin/message_template.rb
|
107
108
|
- lib/pechkin/prometheus_utils.rb
|