pechkin 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pechkin.rb +1 -0
- data/lib/pechkin/api.rb +4 -2
- data/lib/pechkin/channel.rb +28 -3
- data/lib/pechkin/connector.rb +12 -6
- data/lib/pechkin/substitute.rb +24 -0
- data/lib/pechkin/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4d225464fc987cff4a7df181e175e4756c60ccc9982582d7906b4f85b194f3d
|
4
|
+
data.tar.gz: 5ca5b0e4c4aacf29e38a51a12217494539f5ef4a31f1eedaf799fc0170f9eb40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f23b61d62cc9fbf3df9d4e578080147f9de3717f8e924d71329778674dcfa21a0bccd46b45e32d33c9ac30eb515d82b9cceeca2af34da1dea210414f20e6d8b3
|
7
|
+
data.tar.gz: ddc485f1678e2a4d4b6ab61f655dbaf69e7fbeb5e1dcc1ae338f2b039f6f77c4cdad6d7d1bfb828c7087c128d09a47e93cb40eb17b7c9a2aba455bef1ff5cdde
|
data/lib/pechkin.rb
CHANGED
data/lib/pechkin/api.rb
CHANGED
@@ -16,6 +16,9 @@ module Pechkin # :nodoc:
|
|
16
16
|
def create_chanels(chanels, bots)
|
17
17
|
chanels.each do |chanel_name, chanel_desc|
|
18
18
|
bot = bots[chanel_desc['bot']]
|
19
|
+
|
20
|
+
raise "'#{chanel_desc['bot']}' not found." unless bot
|
21
|
+
|
19
22
|
connector = create_connector(bot, chanel_name)
|
20
23
|
|
21
24
|
chat_ids = chanel_desc['chat_ids']
|
@@ -57,7 +60,6 @@ module Pechkin # :nodoc:
|
|
57
60
|
end
|
58
61
|
post message_name do
|
59
62
|
template = message_desc['template']
|
60
|
-
opts = message_desc['options'] || {}
|
61
63
|
# Some services will send json, but without correct content-type, then
|
62
64
|
# params will be parsed weirdely. So we try parse request body as json
|
63
65
|
params = ensure_json(request.body.read, params)
|
@@ -67,7 +69,7 @@ module Pechkin # :nodoc:
|
|
67
69
|
# received parameters.
|
68
70
|
params = (message_desc['variables'] || {}).merge(params)
|
69
71
|
|
70
|
-
channel.send_message(template, params,
|
72
|
+
channel.send_message(template, params, message_desc)
|
71
73
|
end
|
72
74
|
# rubocop:enable Metrics/AbcSize
|
73
75
|
end
|
data/lib/pechkin/channel.rb
CHANGED
@@ -10,15 +10,40 @@ module Pechkin
|
|
10
10
|
@logger = ::Logger.new(STDOUT)
|
11
11
|
end
|
12
12
|
|
13
|
-
def send_message(message, data,
|
14
|
-
text = Message.new(data).render(message)
|
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
|
+
|
15
18
|
logger.warn 'Resulting text is empty' if text.empty?
|
16
|
-
results = @channel_list.map
|
19
|
+
results = @channel_list.map do |id|
|
20
|
+
@connector.send_message(id, text, message_desc)
|
21
|
+
end
|
22
|
+
|
17
23
|
process_results(message, results)
|
18
24
|
end
|
19
25
|
|
20
26
|
private
|
21
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
|
+
|
22
47
|
def process_results(message, results)
|
23
48
|
success, error = results.partition { |_chat, code, _body| code < 400 }
|
24
49
|
error.each do |chat, code, body|
|
data/lib/pechkin/connector.rb
CHANGED
@@ -7,9 +7,10 @@ require 'cgi'
|
|
7
7
|
module Pechkin
|
8
8
|
# Base connector
|
9
9
|
class Connector
|
10
|
-
def send_message(chat, message,
|
10
|
+
def send_message(chat, message, message_desc); end
|
11
11
|
|
12
12
|
def post_data(url, data, headers: {})
|
13
|
+
puts data.inspect
|
13
14
|
uri = URI.parse(url)
|
14
15
|
headers = { 'Content-Type' => 'application/json' }.merge(headers)
|
15
16
|
http = Net::HTTP.new(uri.host, uri.port)
|
@@ -27,8 +28,8 @@ module Pechkin
|
|
27
28
|
@bot_token = bot_token
|
28
29
|
end
|
29
30
|
|
30
|
-
def send_message(chat_id, message,
|
31
|
-
options = {
|
31
|
+
def send_message(chat_id, message, message_desc)
|
32
|
+
options = { parse_mode: message_desc['telegram_parse_mode'] || 'HTML' }
|
32
33
|
params = options.update(chat_id: chat_id, text: message)
|
33
34
|
|
34
35
|
response = post_data(method_url('sendMessage'), params)
|
@@ -47,12 +48,17 @@ module Pechkin
|
|
47
48
|
@headers = { 'Authorization' => "Bearer #{bot_token}" }
|
48
49
|
end
|
49
50
|
|
50
|
-
def send_message(chat, message,
|
51
|
+
def send_message(chat, message, message_desc)
|
51
52
|
text = CGI.unescape_html(message)
|
52
53
|
|
53
|
-
|
54
|
+
attachments = message_desc['slack_attachments'] || {}
|
55
|
+
|
56
|
+
if text.strip.empty? && attachments.empty?
|
57
|
+
return [chat, 400, 'not sent: empty']
|
58
|
+
end
|
59
|
+
|
60
|
+
params = { channel: chat, text: text, attachments: attachments }
|
54
61
|
|
55
|
-
params = options.update(channel: chat, text: text)
|
56
62
|
url = 'https://slack.com/api/chat.postMessage'
|
57
63
|
response = post_data(url, params, headers: @headers)
|
58
64
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Pechkin # :nodoc:
|
2
|
+
# Replaces ${:varname:} patterns inside strings. All posible substitutions are
|
3
|
+
# provided through constructor.
|
4
|
+
#
|
5
|
+
# Complex templating and text fromatting (like float numbers formatting) is
|
6
|
+
# not a goal. We do not aim to implement new templating engine here. Just
|
7
|
+
# simple stuff.
|
8
|
+
class Substitute
|
9
|
+
# @param substitutions [Hash] hash of possible substitutions for replacement
|
10
|
+
def initialize(substitutions)
|
11
|
+
@substitutions = substitutions
|
12
|
+
end
|
13
|
+
|
14
|
+
def process(string)
|
15
|
+
string.gsub(/\$\{([A-Za-z0-9_]+)\}/) do |m|
|
16
|
+
key = m[2..-2]
|
17
|
+
|
18
|
+
value = @substitutions[key] || @substitutions[key.to_sym]
|
19
|
+
|
20
|
+
(value || m).to_s
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/pechkin/version.rb
CHANGED
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: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Arkhanhelsky
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-11-
|
11
|
+
date: 2019-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- lib/pechkin/config.rb
|
54
54
|
- lib/pechkin/connector.rb
|
55
55
|
- lib/pechkin/message.rb
|
56
|
+
- lib/pechkin/substitute.rb
|
56
57
|
- lib/pechkin/version.rb
|
57
58
|
homepage: https://github.com/iarkhanhelsky/pechkin
|
58
59
|
licenses:
|