Module: Optimizely::Helpers::Validator

Defined in:
lib/optimizely/helpers/validator.rb

Class Method Summary collapse

Class Method Details

.attribute_valid?(attribute_key, attribute_value) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/optimizely/helpers/validator.rb', line 37

def attribute_valid?(attribute_key, attribute_value)
  # Determines if provided attribute_key and attribute_value are valid.
  #
  # attribute_key - Variable which needs to be validated.
  # attribute_value - Variable which needs to be validated.
  #
  # Returns boolean depending on validity of attribute_key and attribute_value.

  return false unless attribute_key.is_a?(String) || attribute_key.is_a?(Symbol)

  return true if (boolean? attribute_value) || (attribute_value.is_a? String)

  finite_number?(attribute_value)
end

.attributes_valid?(attributes) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
# File 'lib/optimizely/helpers/validator.rb', line 27

def attributes_valid?(attributes)
  # Determines if provided attributes are valid.
  #
  # attributes - User attributes to be validated.
  #
  # Returns boolean depending on validity of attributes.

  attributes.is_a?(Hash)
end

.boolean?(value) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
158
159
160
# File 'lib/optimizely/helpers/validator.rb', line 155

def boolean?(value)
  # Returns true if given value type is boolean.
  #         false otherwise.

  value.is_a?(TrueClass) || value.is_a?(FalseClass)
end

.datafile_valid?(datafile) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/optimizely/helpers/validator.rb', line 62

def datafile_valid?(datafile)
  # Determines if a given datafile is valid.
  #
  # datafile - String JSON representing the project.
  #
  # Returns boolean depending on validity of datafile.

  begin
    datafile = JSON.parse(datafile)
  rescue
    return false
  end

  JSON::Validator.validate(Helpers::Constants::JSON_SCHEMA_V2, datafile)
end

.error_handler_valid?(error_handler) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
# File 'lib/optimizely/helpers/validator.rb', line 78

def error_handler_valid?(error_handler)
  # Determines if a given error handler is valid.
  #
  # error_handler - error_handler to be validated.
  #
  # Returns boolean depending on whether error_handler has a handle_error method.

  error_handler.respond_to?(:handle_error)
end

.event_dispatcher_valid?(event_dispatcher) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
# File 'lib/optimizely/helpers/validator.rb', line 88

def event_dispatcher_valid?(event_dispatcher)
  # Determines if a given event dispatcher is valid.
  #
  # event_dispatcher - event_dispatcher to be validated.
  #
  # Returns boolean depending on whether event_dispatcher has a dispatch_event method.

  event_dispatcher.respond_to?(:dispatch_event)
end

.event_manager_valid?(event_manager) ⇒ Boolean

Returns:

  • (Boolean)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/optimizely/helpers/validator.rb', line 216

def event_manager_valid?(event_manager)
  # Determines if a given event_manager is valid.
  #
  # event_manager - custom manager to be validated.
  #
  # Returns boolean depending on whether manager has required method and parameters.
  return false unless
    event_manager.respond_to?(:send_event) &&
    event_manager.respond_to?(:start!) &&
    (event_manager.method(:start!)&.parameters&.length || 0) >= 1 &&
    event_manager.respond_to?(:update_config) &&
    event_manager.respond_to?(:stop!)

  required_parameters = Set[%i[keyreq type], %i[keyreq action], %i[keyreq identifiers], %i[keyreq data]]
  existing_parameters = event_manager.method(:send_event).parameters.to_set

  existing_parameters & required_parameters == required_parameters
end

.event_tags_valid?(event_tags) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
# File 'lib/optimizely/helpers/validator.rb', line 52

def event_tags_valid?(event_tags)
  # Determines if provided event tags are valid.
  #
  # event_tags - Event tags to be validated.
  #
  # Returns boolean depending on validity of event tags.

  event_tags.is_a?(Hash)
end

.finite_number?(value) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
177
178
179
180
# File 'lib/optimizely/helpers/validator.rb', line 174

def finite_number?(value)
  # Returns true if the given value is a number, enforces
  #   absolute limit of 2^53 and restricts NaN, Infinity, -Infinity.
  #   false otherwise.

  value.is_a?(Numeric) && value.to_f.finite? && value.abs <= Constants::FINITE_NUMBER_LIMIT
end

.inputs_valid?(variables, logger = NoOpLogger.new, level = Logger::ERROR) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/optimizely/helpers/validator.rb', line 114

def inputs_valid?(variables, logger = NoOpLogger.new, level = Logger::ERROR)
  # Determines if values of variables in given array are non empty string.
  #
  # variables - array values to validate.
  #
  # logger - logger.
  #
  # Returns boolean True if all of the values are valid, False otherwise.

  return false unless variables.respond_to?(:each) && !variables.empty?

  is_valid = true
  if variables.include? :user_id
    # Empty str is a valid user ID.
    unless variables[:user_id].is_a?(String)
      is_valid = false
      logger.log(level, "#{Constants::INPUT_VARIABLES['USER_ID']} is invalid")
    end
    variables.delete :user_id
  end

  if variables.include? :variable_type
    # Empty variable_type is a valid user ID.
    unless variables[:variable_type].is_a?(String) || !variables[:variable_type]
      is_valid = false
      logger.log(level, "#{Constants::INPUT_VARIABLES['VARIABLE_TYPE']} is invalid")
    end
    variables.delete :variable_type
  end

  variables.each do |key, value|
    next if value.is_a?(String) && !value.empty?

    is_valid = false
    next unless logger_valid?(logger) && level

    logger.log(level, "#{Constants::INPUT_VARIABLES[key.to_s.upcase]} is invalid")
  end
  is_valid
end

.logger_valid?(logger) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
# File 'lib/optimizely/helpers/validator.rb', line 98

def logger_valid?(logger)
  # Determines if a given logger is valid.
  #
  # logger - logger to be validated.
  #
  # Returns boolean depending on whether logger has a log method.

  logger.respond_to?(:log)
end

.odp_data_types_valid?(data) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
185
# File 'lib/optimizely/helpers/validator.rb', line 182

def odp_data_types_valid?(data)
  valid_types = [String, Float, Integer, TrueClass, FalseClass, NilClass]
  data&.values&.all? { |e| valid_types.member? e.class }
end

.same_types?(value_1, value_2) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
165
166
167
168
169
170
171
172
# File 'lib/optimizely/helpers/validator.rb', line 162

def same_types?(value_1, value_2)
  # Returns true if given values are of same types.
  #         false otherwise.
  # Numeric values are considered as same type.

  return true if value_1.is_a?(Numeric) && value_2.is_a?(Numeric)

  return true if boolean?(value_1) && boolean?(value_2)

  value_1.instance_of?(value_2.class)
end

.segment_manager_valid?(segment_manager) ⇒ Boolean

Returns:

  • (Boolean)


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

def segment_manager_valid?(segment_manager)
  # Determines if a given segment_manager is valid.
  #
  # segment_manager - custom manager to be validated.
  #
  # Returns boolean depending on whether manager has required methods.

  segment_manager.respond_to?(:odp_config) &&
    segment_manager.respond_to?(:reset) &&
    segment_manager.method(:reset)&.parameters&.empty? &&
    segment_manager.respond_to?(:fetch_qualified_segments) &&
    (segment_manager.method(:fetch_qualified_segments)&.parameters&.length || 0) >= 3
end

.segments_cache_valid?(segments_cache) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/optimizely/helpers/validator.rb', line 187

def segments_cache_valid?(segments_cache)
  # Determines if a given segments_cache is valid.
  #
  # segments_cache - custom cache to be validated.
  #
  # Returns boolean depending on whether cache has required methods.

  segments_cache.respond_to?(:reset) &&
    segments_cache.method(:reset)&.parameters&.empty? &&
    segments_cache.respond_to?(:lookup) &&
    segments_cache.method(:lookup)&.parameters&.length&.positive? &&
    segments_cache.respond_to?(:save) &&
    segments_cache.method(:save)&.parameters&.length&.positive?
end

.string_numeric?(str) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
# File 'lib/optimizely/helpers/validator.rb', line 108

def string_numeric?(str)
  !Float(str).nil?
rescue
  false
end