module Optimizely::Helpers::Validator

Public Instance Methods

attribute_valid?(attribute_key, attribute_value) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/optimizely/helpers/validator.rb, line 108
def string_numeric?(str)
  !Float(str).nil?
rescue
  false
end