Class: Unleash::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/unleash/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(*opts) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/unleash/client.rb', line 11

def initialize(*opts)
  Unleash.configuration ||= Unleash::Configuration.new(*opts)
  Unleash.configuration.validate!

  Unleash.logger = Unleash.configuration.logger
  Unleash.logger.level = Unleash.configuration.log_level

  unless Unleash.configuration.disable_client
    Unleash.toggle_fetcher = Unleash::ToggleFetcher.new
    register

    unless Unleash.configuration.disable_metrics
      Unleash.toggle_metrics = Unleash::Metrics.new
      Unleash.reporter = Unleash::MetricsReporter.new
      scheduledExecutor = Unleash::ScheduledExecutor.new('MetricsReporter', Unleash.configuration.metrics_interval)
      scheduledExecutor.run do
        Unleash.reporter.send
      end
    end
  else
    Unleash.logger.warn "Unleash::Client is disabled! Will only return default results!"
  end
end

Instance Method Details

#is_enabled?(feature, context = nil, default_value = false) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/unleash/client.rb', line 35

def is_enabled?(feature, context = nil, default_value = false)
    Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} with context #{context}"

    if Unleash.configuration.disable_client
      Unleash.logger.warn "unleash_client is disabled! Always returning #{default_value} for feature #{feature}!"
      return default_value
    end

    toggle_as_hash = Unleash.toggles.select{ |toggle| toggle['name'] == feature }.first if Unleash.toggles

    if toggle_as_hash.nil?
      Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
      return default_value
    end

    toggle = Unleash::FeatureToggle.new(toggle_as_hash)
    toggle_result = toggle.is_enabled?(context, default_value)

    return toggle_result
end