Class: ConfigCat::User

Inherits:
Object
  • Object
show all
Defined in:
lib/configcat/user.rb

Overview

User Object. Contains user attributes which are used for evaluating targeting rules and percentage options.

Constant Summary collapse

PREDEFINED =
["Identifier", "Email", "Country"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, email: nil, country: nil, custom: nil) ⇒ User

Initialize a User object. Args:

identifier: The unique identifier of the user or session (e.g. email address, primary key, session ID, etc.)
email: Email address of the user.
country: Country of the user.
custom: Custom attributes of the user for advanced targeting rule definitions (e.g. role, subscription type, etc.)
All comparators support string values as User Object attribute (in some cases they need to be provided in a
specific format though, see below), but some of them also support other types of values. It depends on the
comparator how the values will be handled. The following rules apply:
Text-based comparators (EQUALS, IS_ONE_OF, etc.)
* accept string values,
* all other values are automatically converted to string
  (a warning will be logged but evaluation will continue as normal).
SemVer-based comparators (IS_ONE_OF_SEMVER, LESS_THAN_SEMVER, GREATER_THAN_SEMVER, etc.)
* accept string values containing a properly formatted, valid semver value,
* all other values are considered invalid
  (a warning will be logged and the currently evaluated targeting rule will be skipped).
Number-based comparators (EQUALS_NUMBER, LESS_THAN_NUMBER, GREATER_THAN_OR_EQUAL_NUMBER, etc.)
* accept float values and all other numeric values which can safely be converted to float,
* accept string values containing a properly formatted, valid float value,
* all other values are considered invalid
  (a warning will be logged and the currently evaluated targeting rule will be skipped).
Date time-based comparators (BEFORE_DATETIME / AFTER_DATETIME)
* accept datetime values, which are automatically converted to a second-based Unix timestamp
  (datetime values with naive timezone are considered to be in UTC),
* accept float values representing a second-based Unix timestamp
  and all other numeric values which can safely be converted to float,
* accept string values containing a properly formatted, valid float value,
* all other values are considered invalid
  (a warning will be logged and the currently evaluated targeting rule will be skipped).
String array-based comparators (ARRAY_CONTAINS_ANY_OF / ARRAY_NOT_CONTAINS_ANY_OF)
* accept arrays of strings,
* accept string values containing a valid JSON string which can be deserialized to an array of strings,
* all other values are considered invalid
  (a warning will be logged and the currently evaluated targeting rule will be skipped).


43
44
45
46
47
# File 'lib/configcat/user.rb', line 43

def initialize(identifier, email: nil, country: nil, custom: nil)
  @identifier = (!identifier.equal?(nil)) ? identifier : ""
  @data = { "Identifier" => identifier, "Email" => email, "Country" => country }
  @custom = custom
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



6
7
8
# File 'lib/configcat/user.rb', line 6

def identifier
  @identifier
end

Instance Method Details

#get_attribute(attribute) ⇒ Object



53
54
55
56
57
58
# File 'lib/configcat/user.rb', line 53

def get_attribute(attribute)
  attribute = attribute.to_s
  return @data[attribute] if PREDEFINED.include?(attribute)
  return @custom[attribute] if @custom
  return nil
end

#get_identifierObject



49
50
51
# File 'lib/configcat/user.rb', line 49

def get_identifier
  return @identifier
end

#to_sObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/configcat/user.rb', line 60

def to_s
  dump = {
    'Identifier': @identifier,
    'Email': @data['Email'],
    'Country': @data['Country']
  }
  dump.merge!(@custom) if @custom
  filtered_dump = dump.reject { |_, v| v.nil? }
  formatted_dump = filtered_dump.transform_values do |value|
    value.is_a?(DateTime) ? value.strftime('%Y-%m-%dT%H:%M:%S.%L%z') : value
  end
  return JSON.generate(formatted_dump, ascii_only: false, separators: %w[, :])
end