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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3fa8abd425ad6a776104aabeeb6916d4b8b4d77ae0f0d7e24b75f102bb70b016
4
- data.tar.gz: 1b5b43910b029f8553f54ddfa5d70a7195b2db86d35a38ba1fcffc37521b7d4b
3
+ metadata.gz: c18e97f32b4fe42a3bd649c43563abc81a13fe49d5c2332f54129b1babc61767
4
+ data.tar.gz: bcc9dcdbccc1041ed9377b1e4bbce4113a6820ca798efceeefc9476b30eb8499
5
5
  SHA512:
6
- metadata.gz: d636b920cb207c03ce1f96ceeb47ec478214743e0ef4f49895c07982fcf68d2b724d4006058ef00de6903c986c6f40e8678ebbb7ccdaa681d6b552cfb70111a1
7
- data.tar.gz: 5254cb1f323cb46af16b889d82ffd565a9a0f30e5665cc6c5487e59926f25e66b8a573e004ffcf4c2a34b8f17187b0e67443ef5bf5ffec3b8e2a68e079557def
6
+ metadata.gz: 7d567c46d62df87a39e701f086c51a753616cb3e1acc104829675a1d091a9907110ed1e807f0253782b125a55017918c2ddedf3716ad58af77b0a96296c26a4e
7
+ data.tar.gz: b7add41faa2da882200c5a47fe4a847a97ea3df7a8c9cb69796e200405e7a3a19fd49cf0e21776484a46dc713ca8312bfce86ab2230c003c9325c4d86ddab526
@@ -1,4 +1,3 @@
1
1
  module Pechkin
2
2
  Bot = Struct.new(:token, :connector, :name, keyword_init: true)
3
- Channel = Struct.new(:chat_ids, :connector, :messages, keyword_init: true)
4
3
  end
@@ -1,59 +1,3 @@
1
1
  module Pechkin
2
- # Creates object which can send messages to assigned chanels
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
- message_config['template'] = get_template(message_config['template']) if message_config.key?('template')
65
-
66
- messages[name] = message_config
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)
@@ -1,6 +1,5 @@
1
1
  require 'yaml'
2
2
 
3
- require_relative 'configuration/model'
4
3
  require_relative 'configuration/configuration_loader'
5
4
  require_relative 'configuration/configuration_loader_bots'
6
5
  require_relative 'configuration/configuration_loader_channels'
@@ -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
- message_config = substitute(data, fetch_message(channel_config, msg_id))
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
@@ -1,7 +1,7 @@
1
1
  module Pechkin
2
2
  # Keeps actual version
3
3
  module Version
4
- VERSION = [1, 4, 0].freeze
4
+ VERSION = [1, 5, 1].freeze
5
5
  class << self
6
6
  def version_string
7
7
  VERSION.join('.')
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.0
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-16 00:00:00.000000000 Z
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