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 =
[]
@@qabot_api_url =
nil

Class Method Summary collapse

Class Method Details

.add_flag(h) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/qabot.rb', line 210

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



139
140
141
142
143
144
145
# File 'lib/qabot.rb', line 139

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)


134
135
136
# File 'lib/qabot.rb', line 134

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)


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

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)


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

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)


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

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)


200
201
202
203
204
205
206
207
# File 'lib/qabot.rb', line 200

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



124
125
126
127
128
129
130
131
# File 'lib/qabot.rb', line 124

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



40
41
42
# File 'lib/qabot.rb', line 40

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/qabot.rb', line 148

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



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/qabot.rb', line 52

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

.qabot_api_urlObject



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

def self.qabot_api_url()
    @@qabot_api_url
end

.require_db_classesObject



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

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

.run(h) ⇒ Object



234
235
236
237
238
# File 'lib/qabot.rb', line 234

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_all(logger = nil) ⇒ Object

def self.add_flag



225
226
227
228
229
230
231
232
# File 'lib/qabot.rb', line 225

def self.run_all(logger=nil)
    logger = BlackStack::DummyLogger.new(nil) if logger.nil?
    @@flags_descriptors.each do |h|
        logger.logs "Running flag: #{h[:name]}..."
        BlackStack::QABot.run(h)
        logger.done
    end
end

.set_qabot_api_url(url) ⇒ Object



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

def self.set_qabot_api_url(url)
    @@qabot_api_url = url
end

.trigger_red_valuesObject



65
66
67
68
69
70
71
72
73
# File 'lib/qabot.rb', line 65

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



44
45
46
47
48
49
50
# File 'lib/qabot.rb', line 44

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/qabot.rb', line 89

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



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/qabot.rb', line 76

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