Top Level Namespace
Defined Under Namespace
Modules: Kogno
Classes: Array, ChatLog, Hash, Logger, LongPayload, MatchedMessage, MessengerRecurringNotification, ScheduledMessage, Sequence, String, TelegramChatGroup, User, Wit
Instance Method Summary
collapse
-
#db_connect(db_configuration) ⇒ Object
-
#html_template(route, params = {}) ⇒ Object
-
#l(value, **format) ⇒ Object
-
#logger ⇒ Object
-
#reload!(print = true) ⇒ Object
-
#set_payload(payload, params = nil) ⇒ Object
-
#sql_query(query) ⇒ Object
-
#t(key, **interpolation) ⇒ Object
-
#tabulate(text, divider, max_chars, orientation = "left") ⇒ Object
-
#tabulateLeft(text, spacing) ⇒ Object
-
#tabulateMiddle(text, spacing) ⇒ Object
-
#tabulateRight(text, spacing) ⇒ Object
Instance Method Details
#db_connect(db_configuration) ⇒ Object
4
5
6
|
# File 'lib/core/db.rb', line 4
def db_connect(db_configuration)
ActiveRecord::Base.establish_connection(db_configuration)
end
|
#html_template(route, params = {}) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/core/global_methods.rb', line 40
def html_template(route, params={})
route_array = route.to_s.split("/")
if route_array.count == 2
action_group = route_array[0]
action = route_array[1]
elsif route_array.count == 1
if self.type == :context
action_group = self.name
action = route_array[0]
else
raise "Can't determine the context for template #{route}"
return false
end
else
raise "Wrong format on route. Expected:'context_name/template_name'"
return false
end
template = $context_html_templates[action_group.to_sym][action.to_sym]
if template.nil?
logger.write "Template bot/templates/#{action_group}/#{action}.rhtml not found.", :red
return ""
else
return template.render(self, params)
end
end
|
#l(value, **format) ⇒ Object
10
11
12
|
# File 'lib/core/global_methods.rb', line 10
def l(value, **format)
I18n.l(value, **format)
end
|
#logger ⇒ Object
68
69
70
|
# File 'lib/core/extensions/logger.rb', line 68
def logger
$logger
end
|
#reload!(print = true) ⇒ Object
#set_payload(payload, params = nil) ⇒ Object
14
15
16
17
18
19
20
21
|
# File 'lib/core/global_methods.rb', line 14
def set_payload(payload,params=nil)
unless params.nil?
output_payload = "#{payload}:#{params.to_json}"
else
output_payload = payload
end
return output_payload
end
|
#sql_query(query) ⇒ Object
30
31
32
33
34
35
36
37
38
|
# File 'lib/core/global_methods.rb', line 30
def sql_query(query)
results = ActiveRecord::Base.connection.select_all(query)
{
columns: results.columns,
rows: results.rows
}
results
end
|
#t(key, **interpolation) ⇒ Object
1
2
3
4
5
6
7
8
|
# File 'lib/core/global_methods.rb', line 1
def t(key, **interpolation)
translation = I18n.t(key, **interpolation)
if translation.class == Array
return translation[rand(translation.count)]
else
return translation
end
end
|
#tabulate(text, divider, max_chars, orientation = "left") ⇒ Object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/core/helpers/string.rb', line 1
def tabulate(text, divider, max_chars, orientation = "left")
amount = text.count(divider)
amount = amount + 1
spacing = max_chars / amount
text = text.split(divider)
text.each do |str|
str = str.strip
index = text.find_index(str)
if str[0] == " "
text[index] = str[1..-1]
elsif str[str.length - 1] == " "
text[index] = str.chop
end
end
new_text = []
case orientation
when "left"
new_text = tabulateLeft(text, spacing)
when "right"
new_text = tabulateRight(text, spacing)
when "middle"
new_text = tabulateMiddle(text, spacing)
end
return new_text.join(divider)
end
|
#tabulateLeft(text, spacing) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/core/helpers/string.rb', line 30
def tabulateLeft(text, spacing)
new_text = []
text.each do |str|
tab_str = str
spaces = spacing - str.length
if text.find_index(tab_str).even?
if spacing > tab_str.length && text.find_index(tab_str) != 0
tab_str.prepend(" ")
spaces -= 1
end
while spaces > 0 do
tab_str.concat(" ")
spaces -= 1
end
else
if spacing > tab_str.length
tab_str.prepend(" ")
spaces -= 1
end
while spaces > 0 do
tab_str.concat(" ")
spaces -= 1
end
end
new_text << tab_str
end
return new_text
end
|
#tabulateMiddle(text, spacing) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/core/helpers/string.rb', line 86
def tabulateMiddle(text, spacing)
new_text = []
text.each do |str|
tab_str = str
spaces = spacing - str.length
divided = spaces / 2
concat = false
while spaces > divided do
tab_str.prepend(" ")
spaces -= 1
end
while spaces > 0 do
tab_str.concat(" ")
spaces -= 1
end
new_text << tab_str
end
return new_text
end
|
#tabulateRight(text, spacing) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/core/helpers/string.rb', line 60
def tabulateRight(text, spacing)
new_text = []
text.each do |str|
tab_str = str
spaces = spacing - str.length
if text.find_index(tab_str).even?
if spacing > tab_str.length
tab_str.concat(" ")
spaces -= 1
end
while spaces > 0 do
tab_str.prepend(" ")
spaces -= 1
end
else
while spaces > 0 do
tab_str.prepend(" ")
spaces -= 1
end
end
new_text << tab_str
end
return new_text
end
|