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_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

.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.class == value_2.class
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