Module: LaunchDarkly::Impl::Context

Defined in:
lib/ldclient-rb/impl/context.rb

Overview

Since:

  • 5.5.0

Constant Summary collapse

ERR_KIND_NON_STRING =

Since:

  • 5.5.0

'context kind must be a string'
ERR_KIND_CANNOT_BE_KIND =

Since:

  • 5.5.0

'"kind" is not a valid context kind'
ERR_KIND_CANNOT_BE_MULTI =

Since:

  • 5.5.0

'"multi" is not a valid context kind'
ERR_KIND_INVALID_CHARS =

Since:

  • 5.5.0

'context kind contains disallowed characters'
ERR_KEY_NON_STRING =

Since:

  • 5.5.0

'context key must be a string'
ERR_KEY_EMPTY =

Since:

  • 5.5.0

'context key must not be empty'
ERR_NAME_NON_STRING =

Since:

  • 5.5.0

'context name must be a string'
ERR_ANONYMOUS_NON_BOOLEAN =

Since:

  • 5.5.0

'context anonymous must be a boolean'

Class Method Summary collapse

Class Method Details

.canonicalize_key_for_kind(kind, key) ⇒ String

Parameters:

  • kind (String)
  • key (String)

Returns:

  • (String)

Since:

  • 5.5.0



86
87
88
89
90
91
92
93
# File 'lib/ldclient-rb/impl/context.rb', line 86

def self.canonicalize_key_for_kind(kind, key)
  # When building a FullyQualifiedKey, ':' and '%' are percent-escaped;
  # we do not use a full URL-encoding function because implementations of
  # this are inconsistent across platforms.
  encoded = key.gsub("%", "%25").gsub(":", "%3A")

  "#{kind}:#{encoded}"
end

.make_context(context) ⇒ LDContext

We allow consumers of this SDK to provide us with either a Hash or an instance of an LDContext. This is convenient for them but not as much for us. To make the conversion slightly more convenient for us, we have created this method.

Parameters:

Returns:

Since:

  • 5.5.0



27
28
29
30
31
# File 'lib/ldclient-rb/impl/context.rb', line 27

def self.make_context(context)
  return context if context.is_a?(LDContext)

  LDContext.create(context)
end

.validate_anonymous(anonymous, allow_nil) ⇒ String?

Returns an error message if anonymous is invalid; nil otherwise.

Parameters:

  • anonymous (any)
  • allow_nil (Boolean)

Returns:

  • (String, nil)

Since:

  • 5.5.0



74
75
76
77
78
79
# File 'lib/ldclient-rb/impl/context.rb', line 74

def self.validate_anonymous(anonymous, allow_nil)
  return nil if anonymous.nil? && allow_nil
  return nil if [true, false].include? anonymous

  ERR_ANONYMOUS_NON_BOOLEAN
end

.validate_key(key) ⇒ String?

Returns an error message if the key is invalid; nil otherwise.

Parameters:

  • key (any)

Returns:

  • (String, nil)

Since:

  • 5.5.0



52
53
54
55
# File 'lib/ldclient-rb/impl/context.rb', line 52

def self.validate_key(key)
  return ERR_KEY_NON_STRING unless key.is_a?(String)
  ERR_KEY_EMPTY if key == ""
end

.validate_kind(kind) ⇒ String?

Returns an error message if the kind is invalid; nil otherwise.

Parameters:

  • kind (any)

Returns:

  • (String, nil)

Since:

  • 5.5.0



39
40
41
42
43
44
# File 'lib/ldclient-rb/impl/context.rb', line 39

def self.validate_kind(kind)
  return ERR_KIND_NON_STRING unless kind.is_a?(String)
  return ERR_KIND_CANNOT_BE_KIND if kind == "kind"
  return ERR_KIND_CANNOT_BE_MULTI if kind == "multi"
  ERR_KIND_INVALID_CHARS unless kind.match?(/^[\w.-]+$/)
end

.validate_name(name) ⇒ String?

Returns an error message if the name is invalid; nil otherwise.

Parameters:

  • name (any)

Returns:

  • (String, nil)

Since:

  • 5.5.0



63
64
65
# File 'lib/ldclient-rb/impl/context.rb', line 63

def self.validate_name(name)
  ERR_NAME_NON_STRING unless name.nil? || name.is_a?(String)
end