Module: BlackStack::QABot

Defined in:
lib/dbclasses.rb,
lib/qabot.rb

Overview

class Client

Defined Under Namespace

Modules: Flag Classes: Alert, BoolFlag, Category, FloatFlag, IntFlag

Constant Summary collapse

BOOL =
'bool'
INT =
'int'
FLOAT =
'float'
SS =
'ss'
MI =
'mi'
HH =
'hh'
DD =
'dd'
WW =
'ww'
MM =
'mm'
QQ =
'qq'
YY =
'yy'
LOWER_OR_EQUAL =
-2
LOWER =
-1
EQUAL =
0
GREATER =
1
GREATER_OR_EQUAL =
2
@@flags_descriptors =
[]

Class Method Summary collapse

Class Method Details

.add_flag(h) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/qabot.rb', line 201

def self.add_flag(h)
    # get the errors on all parameters except functions
    errors = BlackStack::QABot::validate_descriptor_parameters_except_functions(h)
    # VALIDATE: the `:value_function` is a procedure
    errors << "Invalid value_function. Must be procedure." unless h[:value_function].is_a?(Proc)
    # VALIDATE: the `:alert_comments_function` is a procedure
    errors << "Invalid alert_comments_function. Must be procedure." unless h[:alert_comments_function].is_a?(Proc)
    # raise an exception if `errors` has any element
    raise errors.join("\n") if errors.length > 0
    # add `h` to `@@flags_descriptors`
    @@flags_descriptors << h
    # return the array
    @@flags_descriptors
end

.create(client, h) ⇒ Object

create a new flag belonging this client, with this name



130
131
132
133
134
135
136
# File 'lib/qabot.rb', line 130

def self.create(client, h)
    o = nil
    o = BlackStack::QABot::BoolFlag.create(client, h) if h[:type] == BlackStack::QABot::BOOL
    o = BlackStack::QABot::IntFlag.create(client, h) if h[:type] == BlackStack::QABot::INT
    o = BlackStack::QABot::FloatFlag.create(client, h) if h[:type] == BlackStack::QABot::FLOAT
    o
end

.exists?(client, name) ⇒ Boolean

return ‘true` if exists a flag belonging this client, with this name

Returns:

  • (Boolean)


125
126
127
# File 'lib/qabot.rb', line 125

def self.exists?(client, name)
    !BlackStack::QABot::find(client, name).nil?
end

.exists_by_id?(client, id) ⇒ Boolean

return ‘true` if exists a flag belonging this client, with this name

Returns:

  • (Boolean)


161
162
163
164
165
166
167
168
# File 'lib/qabot.rb', line 161

def self.exists_by_id?(client, id)
    return false if client.nil? || id.nil?
    return false if client.flags.nil?
    client.flags.each do |flag|
        return true if flag.id == id
    end
    false
end

.exists_by_id_and_name?(client, id, name) ⇒ Boolean

return ‘true` if exists a flag belonging this client, with this name

Returns:

  • (Boolean)


171
172
173
174
175
176
177
178
# File 'lib/qabot.rb', line 171

def self.exists_by_id_and_name?(client, id, name)
    return false if client.nil? || id.nil? || name.nil?
    return false if client.flags.nil?
    client.flags.each do |flag|
        return true if flag.id == id && flag.name == name
    end
    false
end

.exists_by_name?(client, name) ⇒ Boolean

return ‘true` if exists a flag belonging this client, with this name

Returns:

  • (Boolean)


181
182
183
184
185
186
187
188
# File 'lib/qabot.rb', line 181

def self.exists_by_name?(client, name)
    return false if client.nil? || name.nil?
    return false if client.flags.nil?
    client.flags.each do |flag|
        return true if flag.name == name
    end
    false
end

.exists_by_name_and_id?(client, name, id) ⇒ Boolean

return ‘true` if exists a flag belonging this client, with this name

Returns:

  • (Boolean)


191
192
193
194
195
196
197
198
# File 'lib/qabot.rb', line 191

def self.exists_by_name_and_id?(client, name, id)
    return false if client.nil? || name.nil? || id.nil?
    return false if client.flags.nil?
    client.flags.each do |flag|
        return true if flag.name == name && flag.id == id
    end
    false
end

.find(client, name) ⇒ Object

return a ‘flag` object belonging this client, with this name



115
116
117
118
119
120
121
122
# File 'lib/qabot.rb', line 115

def self.find(client, name)
    return nil if client.nil? || name.nil?
    return nil if client.flags.nil?
    client.flags.each do |flag|
        return flag if flag.name == name
    end
    nil
end

.flag_descriptorsObject



31
32
33
# File 'lib/qabot.rb', line 31

def self.flag_descriptors()
    @@flags_descriptors
end

.parse(client, h) ⇒ Object

update the flag belonging this client, with this name, with the values in the hash descriptor



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/qabot.rb', line 139

def self.parse(client, h)
    o = BlackStack::QABot::find(client, h[:name])
    return nil if o.nil?
    h[:id] = o.id

    g = client.categories.select { |g| g.name == h[:category] }.first
    if g.nil?
        g = BlackStack::QABot::Category.new
        g.id = guid()
        g.create_time = now()
        g.id_user = u.id
        g.name = h[:category]
        g.save
    end # g.nil?
    h[:id_qacategory] = g.id
    h[:id_user] = client.users.first.id # TODO: I should receive the email of the email in the hash descriptor, and validate the email is belonging the client.
    h[:html_description] = h[:description]
    o.parse(h)
    o
end

.periodsObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/qabot.rb', line 43

def self.periods()
    [
        BlackStack::QABot::SS, 
        BlackStack::QABot::MI, 
        BlackStack::QABot::HH, 
        BlackStack::QABot::DD, 
        BlackStack::QABot::WW, 
        BlackStack::QABot::MM, 
        BlackStack::QABot::QQ, 
        BlackStack::QABot::YY,
    ]
end

.require_db_classesObject



26
27
28
29
# File 'lib/qabot.rb', line 26

def self.require_db_classes()
    # CRM classes
    require_relative '../lib/dbclasses.rb'
end

.run(h) ⇒ Object



222
223
224
225
226
# File 'lib/qabot.rb', line 222

def self.run(h)
    h[:value] = h[:value_function].call()
    h[:alert_comments] = h[:alert_comments_function].call()
    BlackStack::QABot::Flag.push(h)
end

.run_allObject

def self.add_flag



216
217
218
219
220
# File 'lib/qabot.rb', line 216

def self.run_all()
    @@flags_descriptors.each do |h|
        BlackStack::QABot.run(h)
    end
end

.trigger_red_valuesObject



56
57
58
59
60
61
62
63
64
# File 'lib/qabot.rb', line 56

def self.trigger_red_values()
    [
        BlackStack::QABot::LOWER_OR_EQUAL, 
        BlackStack::QABot::LOWER, 
        BlackStack::QABot::EQUAL, 
        BlackStack::QABot::GREATER_OR_EQUAL, 
        BlackStack::QABot::GREATER,
    ]
end

.typesObject



35
36
37
38
39
40
41
# File 'lib/qabot.rb', line 35

def self.types()
    [
        BlackStack::QABot::BOOL,
        BlackStack::QABot::INT,
        BlackStack::QABot::FLOAT,
    ]
end

.validate_descriptor_parameters_except_functions(h) ⇒ Object

Validate the values of the hash, prior executing the ‘:value_function` procedure



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/qabot.rb', line 80

def self.validate_descriptor_parameters_except_functions(h)
    errors = []
    # VALIDATE: the `:type` is a valid value
    errors << "Invalid type: #{h[:type]}" unless BlackStack::QABot.types().include?(h[:type])
    # VALIDATE: the ':category' is a string no longer than 500 chars
    errors << "Invalid category: #{h[:category]}. Must be string lower than 500 chars." unless h[:category].is_a?(String) && h[:category].length <= 500
    # VALIDATE: the `:name` is a string no longer than 500 chars
    errors << "Invalid name: #{h[:name]}. Must be string lower than 500 chars." unless h[:name].is_a?(String) && h[:name].length <= 500
    # VALIDATE: the `:description` is a string no longer than 8000 chars
    errors << "Invalid description: #{h[:description]}. Must be string lower than 8000 chars." unless h[:description].is_a?(String) && h[:description].length <= 8000
    # VALIDATE: the ':trace_fraquency_period` is a valid value
    errors << "Invalid trace_frequency_period: #{h[:trace_frequency_period]}" unless BlackStack::QABot.periods().include?(h[:trace_frequency_period])
    # VALIDATE: the `:trace_frequency_units` is an integer higher than 0
    errors << "Invalid trace_frequency_units: #{h[:trace_frequency_units]}. Must be integer higher than 0." unless h[:trace_frequency_units].is_a?(Integer) && h[:trace_frequency_units] > 0
    # VALIDATE: the `:show_as_flag` is a boolean
    errors << "Invalid show_as_flag: #{h[:show_as_flag]}. Must be boolean." unless h[:show_as_flag].is_a?(TrueClass) || h[:show_as_flag].is_a?(FalseClass)
    # VALIDATE: the `:show_as_timeline` is a boolean
    errors << "Invalid show_as_timeline: #{h[:show_as_timeline]}. Must be boolean." unless h[:show_as_timeline].is_a?(TrueClass) || h[:show_as_timeline].is_a?(FalseClass)
    # VALIDATE: the `:public` is a boolean
    errors << "Invalid public: #{h[:public]}. Must be boolean." unless h[:public].is_a?(TrueClass) || h[:public].is_a?(FalseClass)
    # VALIDATE: the `:share` is a boolean
    errors << "Invalid share: #{h[:share]}. Must be boolean." unless h[:share].is_a?(TrueClass) || h[:share].is_a?(FalseClass)
    # VALIDATE: if `:type` is `BOOL`, then `:trigger_red` must be a boolean
    errors << "Invalid trigger_red: #{h[:trigger_red]}. Must be boolean." if h[:type] == BlackStack::QABot::BOOL && !(h[:trigger_red].is_a?(TrueClass) || h[:trigger_red].is_a?(FalseClass))
    # VALIDATE: if `:type` is `INT` OR `FLOAT`, then `:trigger_red` must be beloning `trigger_red_values`
    errors << "Invalid trigger_red: #{h[:trigger_red]}. Must be one of #{trigger_red_values.to_s}" if (h[:type] == BlackStack::QABot::INT || h[:type] == BlackStack::QABot::FLOAT) && !trigger_red_values.include?(h[:trigger_red])
    # VALIDATE: if `:type` is 'INT', then `:value_threshold` must ba an integer.
    errors << "Invalid value_threshold: #{h[:value_threshold]}. Must be integer." if h[:type] == BlackStack::QABot::INT && !h[:value_threshold].is_a?(Integer)
    # VALIDATE: if `:ty[e` is `FLOAT`, then `:trigger_red` must be a float.
    errors << "Invalid value_threshold: #{h[:value_threshold]}. Must be float." if h[:type] == BlackStack::QABot::FLOAT && !h[:value_threshold].is_a?(Float)
    # return errors
    errors
end

.validate_descriptor_values(h) ⇒ Object

Validate the values of the hash, after executing the ‘:value_function` procedure



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/qabot.rb', line 67

def self.validate_descriptor_values(h)
    errors = []
    # VALIDATE: if `:type` is `BOOL`, `:value` must be boolean.
    errors << "`:value` must be boolean" if h[:type] == BlackStack::QABot::BOOL && ![true, false].include?(h[:value])
    # VALIDATE: if `:type` is `INT`, `:value` must be integer.
    errors << "`:value` must be integer" if h[:type] == BlackStack::QABot::INT && !h[:value].is_a?(Integer)
    # VALIDATE: if `:type` is `FLOAT`, `:value` must be float.
    errors << "`:value` must be float" if h[:type] == BlackStack::QABot::FLOAT && !h[:value].is_a?(Float)
    # return 
    errors
end