Module: Toggly

Defined in:
lib/toggly.rb,
lib/toggly/client.rb,
lib/toggly/config.rb,
lib/toggly/errors.rb,
lib/toggly/context.rb,
lib/toggly/version.rb,
lib/toggly/registry.rb,
lib/toggly/entity_context.rb,
lib/toggly/evaluators/base.rb,
lib/toggly/evaluation_engine.rb,
lib/toggly/feature_definition.rb,
lib/toggly/definitions_provider.rb,
lib/toggly/evaluators/always_on.rb,
lib/toggly/evaluators/targeting.rb,
lib/toggly/evaluators/always_off.rb,
lib/toggly/evaluators/percentage.rb,
lib/toggly/evaluators/time_window.rb,
lib/toggly/snapshot_providers/base.rb,
lib/toggly/snapshot_providers/file.rb,
lib/toggly/snapshot_providers/memory.rb,
lib/toggly/evaluators/context_property.rb,
lib/toggly/evaluators/contextual_targeting.rb

Overview

Entity context registry, mappers, and startup catalog PUT.

Defined Under Namespace

Modules: Evaluators, SnapshotProviders Classes: Client, Config, ConfigError, Context, DefinitionsError, DefinitionsProvider, EntityContext, EntityContextPropertySchema, EntityContextSchemaRegistration, Error, EvaluationEngine, EvaluationError, EvaluationResult, FeatureDefinition, FeatureNotFoundError, NetworkError, NotInitializedError, Registry, SignatureError, SnapshotError

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientObject

Global client instance



58
59
60
# File 'lib/toggly.rb', line 58

def client
  @client
end

Class Method Details

.clear_entity_context_registrationsObject



58
59
60
61
62
63
# File 'lib/toggly/entity_context.rb', line 58

def clear_entity_context_registrations
  @entity_mutex.synchronize do
    @entity_mappers.clear
    @entity_schemas.clear
  end
end

.configure {|config| ... } ⇒ Client

Configure and initialize the global client

Yields:

  • (config)

    Configuration block

Returns:

  • (Client)

    The configured client



64
65
66
67
68
# File 'lib/toggly.rb', line 64

def configure
  config = Config.new
  yield(config) if block_given?
  @client = Client.new(config)
end

.disabled?(feature_key, context: nil) ⇒ Boolean

Check if a feature is disabled using the global client

Parameters:

  • feature_key (String, Symbol)

    The feature key

  • context (Context, nil) (defaults to: nil)

    Optional evaluation context

Returns:

  • (Boolean)


86
87
88
# File 'lib/toggly.rb', line 86

def disabled?(feature_key, context: nil)
  !enabled?(feature_key, context: context)
end

.enabled?(feature_key, context: nil) ⇒ Boolean

Check if a feature is enabled using the global client

Parameters:

  • feature_key (String, Symbol)

    The feature key

  • context (Context, nil) (defaults to: nil)

    Optional evaluation context

Returns:

  • (Boolean)

Raises:



75
76
77
78
79
# File 'lib/toggly.rb', line 75

def enabled?(feature_key, context: nil)
  raise Error, "Toggly not configured. Call Toggly.configure first." unless @client

  @client.enabled?(feature_key, context: context)
end

.entity_schemasObject



54
55
56
# File 'lib/toggly/entity_context.rb', line 54

def entity_schemas
  @entity_mutex.synchronize { @entity_schemas.values.dup }
end

.map_entity(kind, entity) ⇒ Object



47
48
49
50
51
52
# File 'lib/toggly/entity_context.rb', line 47

def map_entity(kind, entity)
  mapper = @entity_mutex.synchronize { @entity_mappers[kind] }
  return nil unless mapper

  mapper.call(entity)
end

.register_context(kind, schema: nil, &mapper) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/toggly/entity_context.rb', line 36

def register_context(kind, schema: nil, &mapper)
  @entity_mutex.synchronize do
    @entity_mappers[kind] = mapper if mapper
    if schema
      schema.kind = kind
      schema.display_name ||= kind
      @entity_schemas[kind] = schema
    end
  end
end

.register_entity_contexts_at_startup(config) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/toggly/entity_context.rb', line 65

def register_entity_contexts_at_startup(config)
  return if config.respond_to?(:disable_entity_context_registration) && config.disable_entity_context_registration
  return if config.app_key.nil? || config.app_key.empty?

  regs = entity_schemas
  return if regs.empty?

  payload = {
    contexts: regs.map do |r|
      {
        kind: r.kind,
        keyProperty: r.key_property,
        displayName: r.display_name || r.kind,
        properties: Array(r.properties).map { |p| { name: p.name, type: p.type } }
      }
    end
  }
  uri = URI.join(config.base_url.end_with?("/") ? config.base_url : "#{config.base_url}/", "sdk/#{config.app_key}/contexts")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = config.http_timeout || 10
  http.read_timeout = config.http_timeout || 10
  req = Net::HTTP::Put.new(uri)
  req["Content-Type"] = "application/json"
  req.body = JSON.generate(payload)
  http.request(req)
rescue StandardError
  nil
end

.reset!Object

Reset the global client (mainly for testing)



91
92
93
94
95
# File 'lib/toggly.rb', line 91

def reset!
  @client&.close
  @client = nil
  clear_entity_context_registrations if respond_to?(:clear_entity_context_registrations)
end