• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Unleash / unleash-client-ruby / 11031869423

25 Sep 2024 11:14AM UTC coverage: 95.57% (-1.7%) from 97.25%
11031869423

Pull #204

github

web-flow
Merge 4cf325450 into d890ae8d3
Pull Request #204: docs: migration guide for v6

73 of 74 new or added lines in 8 files covered. (98.65%)

6 existing lines in 3 files now uncovered.

453 of 474 relevant lines covered (95.57%)

1377.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.76
/lib/unleash/client.rb
1
require 'unleash/configuration'
16✔
2
require 'unleash/toggle_fetcher'
16✔
3
require 'unleash/metrics_reporter'
16✔
4
require 'unleash/scheduled_executor'
16✔
5
require 'unleash/variant'
16✔
6
require 'unleash/util/http'
16✔
7
require 'logger'
16✔
8
require 'time'
16✔
9

10
module Unleash
16✔
11
  class Client
16✔
12
    attr_accessor :fetcher_scheduled_executor, :metrics_scheduled_executor
16✔
13

14
    # rubocop:disable Metrics/AbcSize
15
    def initialize(*opts)
16✔
16
      Unleash.configuration = Unleash::Configuration.new(*opts) unless opts.empty?
3,808✔
17
      Unleash.configuration.validate!
3,792✔
18

19
      Unleash.logger = Unleash.configuration.logger.clone
3,792✔
20
      Unleash.logger.level = Unleash.configuration.log_level
3,792✔
21
      Unleash.engine = YggdrasilEngine.new
3,792✔
22
      Unleash.engine.register_custom_strategies(Unleash.configuration.strategies.custom_strategies)
3,792✔
23

24
      Unleash.toggle_fetcher = Unleash::ToggleFetcher.new Unleash.engine
3,792✔
25
      if Unleash.configuration.disable_client
3,792✔
26
        Unleash.logger.warn "Unleash::Client is disabled! Will only return default (or bootstrapped if available) results!"
3,648✔
27
        Unleash.logger.warn "Unleash::Client is disabled! Metrics and MetricsReporter are also disabled!"
3,648✔
28
        Unleash.configuration.disable_metrics = true
3,648✔
29
        return
3,648✔
30
      end
31

32
      register
144✔
33
      start_toggle_fetcher
144✔
34
      start_metrics unless Unleash.configuration.disable_metrics
144✔
35
    end
36
    # rubocop:enable Metrics/AbcSize
37

38
    def is_enabled?(feature, context = nil, default_value_param = false, &fallback_blk)
16✔
39
      Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} with context #{context}"
3,312✔
40

41
      default_value = if block_given?
3,312✔
42
                        default_value_param || !!fallback_blk.call(feature, context)
304✔
43
                      else
44
                        default_value_param
3,008✔
45
                      end
46

47
      toggle_enabled = Unleash.engine.enabled?(feature, context)
3,312✔
48
      if toggle_enabled.nil?
3,312✔
49
        Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
608✔
50
        Unleash.engine.count_toggle(feature, false)
608✔
51
        return default_value
608✔
52
      end
53

54
      Unleash.engine.count_toggle(feature, toggle_enabled)
2,704✔
55

56
      toggle_enabled
2,704✔
57
    end
58

59
    def is_disabled?(feature, context = nil, default_value_param = true, &fallback_blk)
16✔
60
      !is_enabled?(feature, context, !default_value_param, &fallback_blk)
80✔
61
    end
62

63
    # enabled? is a more ruby idiomatic method name than is_enabled?
64
    alias enabled? is_enabled?
16✔
65
    # disabled? is a more ruby idiomatic method name than is_disabled?
66
    alias disabled? is_disabled?
16✔
67

68
    # execute a code block (passed as a parameter), if is_enabled? is true.
69
    def if_enabled(feature, context = nil, default_value = false, &blk)
16✔
70
      yield(blk) if is_enabled?(feature, context, default_value)
×
71
    end
72

73
    # execute a code block (passed as a parameter), if is_disabled? is true.
74
    def if_disabled(feature, context = nil, default_value = true, &blk)
16✔
75
      yield(blk) if is_disabled?(feature, context, default_value)
×
76
    end
77

78
    def get_variant(feature, context = Unleash::Context.new, fallback_variant = disabled_variant)
26✔
79
      variant = Unleash.engine.get_variant(feature, context)
960✔
80

81
      if variant.nil?
960✔
82
        Unleash.logger.debug "Unleash::Client.get_variant variants for feature: #{feature} not found"
80✔
83
        Unleash.engine.count_toggle(feature, false)
80✔
84
        return fallback_variant
80✔
85
      end
86

87
      variant = Variant.new(variant)
880✔
88

89
      Unleash.engine.count_variant(feature, variant.name)
880✔
90
      Unleash.engine.count_toggle(feature, variant.feature_enabled)
880✔
91

92
      # TODO: Add to README: name, payload, enabled (bool)
93

94
      variant
880✔
95
    end
96

97
    # safe shutdown: also flush metrics to server and toggles to disk
98
    def shutdown
16✔
99
      unless Unleash.configuration.disable_client
×
100
        Unleash.reporter.post unless Unleash.configuration.disable_metrics
×
101
        shutdown!
×
102
      end
103
    end
104

105
    # quick shutdown: just kill running threads
106
    def shutdown!
16✔
107
      unless Unleash.configuration.disable_client
×
108
        self.fetcher_scheduled_executor.exit
×
109
        self.metrics_scheduled_executor.exit unless Unleash.configuration.disable_metrics
×
110
      end
111
    end
112

113
    private
16✔
114

115
    def info
16✔
116
      {
117
        'appName': Unleash.configuration.app_name,
142✔
118
        'instanceId': Unleash.configuration.instance_id,
16✔
119
        'sdkVersion': "unleash-client-ruby:" + Unleash::VERSION,
16✔
120
        'strategies': Unleash.strategies.known_strategies,
16✔
121
        'started': Time.now.iso8601(Unleash::TIME_RESOLUTION),
16✔
122
        'interval': Unleash.configuration.metrics_interval_in_millis,
16✔
123
        'platformName': RUBY_ENGINE,
124
        'platformVersion': RUBY_VERSION,
125
        'yggdrasilVersion': nil,
126
        'specVersion': Unleash::CLIENT_SPECIFICATION_VERSION
127
      }
128
    end
129

130
    def start_toggle_fetcher
16✔
131
      self.fetcher_scheduled_executor = Unleash::ScheduledExecutor.new(
162✔
132
        'ToggleFetcher',
133
        Unleash.configuration.refresh_interval,
16✔
134
        Unleash.configuration.retry_limit,
16✔
135
        first_fetch_is_eager
16✔
136
      )
137
      self.fetcher_scheduled_executor.run do
144✔
UNCOV
138
        Unleash.toggle_fetcher.fetch
×
139
      end
140
    end
141

142
    def start_metrics
16✔
143
      Unleash.reporter = Unleash::MetricsReporter.new
32✔
144
      self.metrics_scheduled_executor = Unleash::ScheduledExecutor.new(
36✔
145
        'MetricsReporter',
146
        Unleash.configuration.metrics_interval,
2✔
147
        Unleash.configuration.retry_limit
2✔
148
      )
149
      self.metrics_scheduled_executor.run do
32✔
150
        Unleash.reporter.post
×
151
      end
152
    end
153

154
    def register
16✔
155
      Unleash.logger.debug "register()"
144✔
156

157
      # Send the request, if possible
158
      begin
34✔
159
        response = Unleash::Util::Http.post(Unleash.configuration.client_register_uri, info.to_json)
144✔
160
      rescue StandardError => e
UNCOV
161
        Unleash.logger.error "unable to register client with unleash server due to exception #{e.class}:'#{e}'."
×
UNCOV
162
        Unleash.logger.error "stacktrace: #{e.backtrace}"
×
163
      end
164
      Unleash.logger.debug "client registered: #{response}"
144✔
165
    end
166

167
    def disabled_variant
16✔
168
      @disabled_variant ||= Unleash::Variant.disabled_variant
912✔
169
    end
170

171
    def first_fetch_is_eager
16✔
172
      Unleash.configuration.use_bootstrap?
144✔
173
    end
174
  end
175
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc