unleash 0.1.6 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99d6b4f0efb9cf53a2f86930e2e26594d2de8c72aee68ee20e1374f6ac1826cc
4
- data.tar.gz: ac9905dee38bb80b0f94595907e74856be1842149bfbfc54ee0ff8390bd9dcda
3
+ metadata.gz: 8800b993fafadb56ff81782ccad56be4fa863d81133cde10486e5c38577ed21e
4
+ data.tar.gz: 194b63447aee5b7d6e12594d770ec7ffc693db4fcd3914907594b1074a7f46a5
5
5
  SHA512:
6
- metadata.gz: b0ce261b43d7325d3aac1ed62a60d7fdec14543d92e5da63f3e1e699ab6a79d5a529c9080f51dfaf641ce72e18518a61ac1f14395987b3e515503865ed1ef5bf
7
- data.tar.gz: 3b4cf6548b3c19be261f3794370a80868f0c07cec0988423aa1388f5a75349f880ef1b824652a2bd582814149c985f82e7e1b01772470e41fd119406272f3b42
6
+ metadata.gz: fa279122da638dba368039dc22ce101236f511ed8471a937daa8f934d180da7e3a3f709ce8869e6157c56d898c403137dd03edc450fffec2c2f1c1d2e0034da2
7
+ data.tar.gz: f35cd2146020c38866360ff044596a6f1858847711ee8ee4b2d98b4d5d6b61e47f547895cf273cc4d49b9f9120eb60f68888197790d4a4deed5fadcd927d503a
data/.gitignore CHANGED
@@ -12,4 +12,4 @@
12
12
  .rspec_status
13
13
 
14
14
  # Clone of the client-specification
15
- /client-specification/
15
+ /client-specification/
@@ -1,16 +1,64 @@
1
+ # inherit_from: .rubocop_todo.yml
2
+
1
3
  Naming/PredicateName:
2
4
  NameWhitelist:
3
5
  - is_enabled?
4
6
 
5
- Metrics/LineLength:
7
+ Metrics/ClassLength:
6
8
  Max: 120
9
+ Metrics/LineLength:
10
+ Max: 140
11
+ Metrics/MethodLength:
12
+ Max: 20
13
+ Metrics/BlockLength:
14
+ Max: 100
15
+ Exclude:
16
+ - 'spec/unleash/client_spec.rb'
17
+ - 'spec/unleash/feature_toggle_spec.rb'
7
18
 
8
- Style/RedundantSelf:
9
- Enabled: false
10
- Style/PreferredHashMethods:
19
+ Metrics/AbcSize:
20
+ Max: 25
21
+ Metrics/CyclomaticComplexity:
22
+ Max: 8
23
+ Metrics/PerceivedComplexity:
24
+ Max: 8
25
+
26
+ Style/Documentation:
11
27
  Enabled: false
28
+
12
29
  Style/StringLiterals:
13
30
  Enabled: false
31
+ Style/RedundantSelf:
32
+ Enabled: false
33
+
34
+ Style/SymbolArray:
35
+ EnforcedStyle: brackets
36
+ Style/WordArray:
37
+ EnforcedStyle: brackets
38
+ Style/PreferredHashMethods:
39
+ EnforcedStyle: verbose
40
+ Style/FrozenStringLiteralComment:
41
+ EnforcedStyle: never
42
+ Style/GuardClause:
43
+ MinBodyLength: 8
44
+
45
+ Style/BracesAroundHashParameters:
46
+ Exclude:
47
+ - 'spec/**/*.rb'
48
+
49
+ Style/IfInsideElse:
50
+ Exclude:
51
+ - 'bin/unleash-client'
52
+
53
+ Style/Next:
54
+ Exclude:
55
+ - 'lib/unleash/scheduled_executor.rb'
56
+
57
+ Layout/MultilineMethodCallIndentation:
58
+ EnforcedStyle: indented
14
59
 
15
60
  Layout/SpaceBeforeBlockBraces:
16
- Enabled: false
61
+ EnforcedStyle: no_space
62
+ Exclude:
63
+ - 'unleash-client.gemspec'
64
+ - 'spec/**/*.rb'
@@ -1,12 +1,16 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3
5
- - 2.4
6
- - 2.5
7
- - 2.6
8
- - jruby
4
+ - jruby
5
+ - 2.7
6
+ - 2.6
7
+ - 2.5
8
+ - 2.4
9
9
  before_install:
10
- - gem install bundler -v 2.0.2
11
- # install client spec from official repository:
12
- - git clone --depth 5 https://github.com/Unleash/client-specification.git client-specification
10
+ - gem install bundler -v 2.0.2
11
+ - git clone --depth 5 --branch v3.2.0 https://github.com/Unleash/client-specification.git
12
+ client-specification
13
+
14
+ notifications:
15
+ slack:
16
+ secure: x593zOjdl2yVB8uP54v8CmuCOat8GFHnK99NPvPHKvif5U7PGe0YOgYh4DC1+Jc9vfjn1ke+0++m+Gif4quowpeOaA/t45xpB494lyziXsBulYml245jRp9yzoUmIIt7KxHhv4rlo3Q1ztMJgh6a5yDCornKHW2bKTkLsvqVTwxBRatLOrt6K9O8FivO/NaqgcoXl7Rw0fOx/bsZtx2IAFueTCH19NoqW1mk9KFEZ96YqJSvuqmfDC0AO7siq03WKlB++nPlKe1QcrlPalCrcsSzrYNhYJ3akBTt/ZbE1v6YJv2L+zUqRnAPTY2H+qp8WejFQtdhIjfeJ/SWox0iWv/Wy/mTFfj+EhFO9Aq+xhMjJ1OOLtNAPoYJyatEVgJkILb6M26igTFcuI60xBbGNmh5ZYeyRdn5/xFb7G2zyJ2Swc3PvN1uLzMHfTF0R7WzGq4CRNGIOjrHTGncyB3IGAONOdJdM3iT9XKY6cdlRK0VkQjEsEMe0eNv2fxxLVSGna4sdJoTND6LhJ6qCfuS9DEDXwoRdLxAXxefycCh9VNp7gloMJx8IbHYxOW0BFZqc3hxNU9X2SwOj6j72DZMrdYDg2aPAW69HG0iMontQ37Di87JEW2F2Cpgb49+4twByrQNIx+st+DGNce1vpc0DN+KuJVdIcmha654lT7Ffe8=
data/README.md CHANGED
@@ -10,10 +10,10 @@ Leverage the [Unleash Server](https://github.com/Unleash/unleash) for powerful f
10
10
 
11
11
  ## Supported Ruby Interpreters
12
12
 
13
- * MRI 2.3
14
- * MRI 2.4
15
- * MRI 2.5
13
+ * MRI 2.7
16
14
  * MRI 2.6
15
+ * MRI 2.5
16
+ * MRI 2.4
17
17
  * jruby
18
18
 
19
19
  ## Installation
@@ -21,7 +21,7 @@ Leverage the [Unleash Server](https://github.com/Unleash/unleash) for powerful f
21
21
  Add this line to your application's Gemfile:
22
22
 
23
23
  ```ruby
24
- gem 'unleash', '~> 0.1.6'
24
+ gem 'unleash', '~> 3.2.0'
25
25
  ```
26
26
 
27
27
  And then execute:
@@ -242,8 +242,6 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
242
242
 
243
243
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
244
244
 
245
- See [TODO.md](TODO.md) for known limitations, and feature roadmap.
246
-
247
245
 
248
246
  ## Contributing
249
247
 
data/Rakefile CHANGED
@@ -3,4 +3,4 @@ require "rspec/core/rake_task"
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -12,7 +12,7 @@ options = {
12
12
  url: 'http://localhost:4242',
13
13
  demo: false,
14
14
  disable_metrics: true,
15
- sleep: 0.1,
15
+ sleep: 0.1
16
16
  }
17
17
 
18
18
  OptionParser.new do |opts|
@@ -57,17 +57,26 @@ raise 'feature_name is required. see --help for usage.' unless feature_name
57
57
 
58
58
  options[:verbose] = false if options[:quiet]
59
59
 
60
+ log_level = \
61
+ if options[:quiet]
62
+ Logger::ERROR
63
+ elsif options[:verbose]
64
+ Logger::DEBUG
65
+ else
66
+ Logger::WARN
67
+ end
68
+
60
69
  @unleash = Unleash::Client.new(
61
70
  url: options[:url],
62
71
  app_name: 'unleash-client-ruby-cli',
63
72
  disable_metrics: options[:metrics],
64
- log_level: log_level = options[:quiet] ? Logger::ERROR : (options[:verbose] ? Logger::DEBUG : Logger::WARN),
73
+ log_level: log_level
65
74
  )
66
75
 
67
- context_params = ARGV.map{ |e| e.split("=") }.map{ |k,v| [k.to_sym, v] }.to_h
68
- context_properties = context_params.reject{ |k,v| [:user_id, :session_id, :remote_address].include? k }
69
- context_params.select!{ |k,v| [:user_id, :session_id, :remote_address].include? k }
70
- context_params.merge!( properties: context_properties ) unless context_properties.nil?
76
+ context_params = ARGV.map{ |e| e.split("=") }.map{ |k, v| [k.to_sym, v] }.to_h
77
+ context_properties = context_params.reject{ |k, _v| [:user_id, :session_id, :remote_address].include? k }
78
+ context_params.select!{ |k, _v| [:user_id, :session_id, :remote_address].include? k }
79
+ context_params.merge!(properties: context_properties) unless context_properties.nil?
71
80
  unleash_context = Unleash::Context.new(context_params)
72
81
 
73
82
  if options[:verbose]
@@ -80,26 +89,21 @@ if options[:verbose]
80
89
  puts ""
81
90
  end
82
91
 
83
-
84
-
85
92
  if options[:demo]
86
93
  loop do
87
94
  enabled = @unleash.is_enabled?(feature_name, unleash_context)
88
95
  print enabled ? '.' : '|'
89
96
  sleep options[:sleep]
90
97
  end
98
+ elsif options[:variant]
99
+ variant = @unleash.get_variant(feature_name, unleash_context)
100
+ puts " For feature \'#{feature_name}\' got variant \'#{variant}\'"
91
101
  else
92
- if options[:variant]
93
- variant = @unleash.get_variant(feature_name, unleash_context)
94
- puts " For feature \'#{feature_name}\' got variant \'#{variant}\'"
102
+ if @unleash.is_enabled?(feature_name, unleash_context)
103
+ puts " \'#{feature_name}\' is enabled according to unleash"
95
104
  else
96
- if @unleash.is_enabled?(feature_name, unleash_context)
97
- puts " \'#{feature_name}\' is enabled according to unleash"
98
- else
99
- puts " \'#{feature_name}\' is disabled according to unleash"
100
- end
105
+ puts " \'#{feature_name}\' is disabled according to unleash"
101
106
  end
102
107
  end
103
108
 
104
-
105
109
  @unleash.shutdown
@@ -16,12 +16,14 @@ puts ">> START simple.rb"
16
16
 
17
17
  # or:
18
18
 
19
- @unleash = Unleash::Client.new( url: 'http://unleash.herokuapp.com/api', app_name: 'simple-test',
19
+ @unleash = Unleash::Client.new(
20
+ url: 'http://unleash.herokuapp.com/api',
21
+ app_name: 'simple-test',
20
22
  instance_id: 'local-test-cli',
21
23
  refresh_interval: 2,
22
24
  metrics_interval: 2,
23
25
  retry_limit: 2,
24
- log_level: Logger::DEBUG,
26
+ log_level: Logger::DEBUG
25
27
  )
26
28
 
27
29
  # feature_name = "AwesomeFeature"
@@ -55,5 +57,3 @@ puts "> shutting down client..."
55
57
  @unleash.shutdown
56
58
 
57
59
  puts ">> END simple.rb"
58
-
59
-
@@ -5,19 +5,19 @@ require 'unleash/context'
5
5
  require 'unleash/client'
6
6
  require 'logger'
7
7
 
8
- Gem.find_files('unleash/strategy/**/*.rb').each { |path| require path }
8
+ Gem.find_files('unleash/strategy/**/*.rb').each{ |path| require path }
9
9
 
10
10
  module Unleash
11
11
  TIME_RESOLUTION = 3
12
12
 
13
13
  STRATEGIES = Unleash::Strategy.constants
14
- .select { |c| Unleash::Strategy.const_get(c).is_a? Class }
15
- .select { |c| !['NotImplemented', 'Base'].include?(c.to_s) }
16
- .map { |c|
14
+ .select{ |c| Unleash::Strategy.const_get(c).is_a? Class }
15
+ .reject{ |c| ['NotImplemented', 'Base'].include?(c.to_s) }
16
+ .map do |c|
17
17
  lowered_c = c.to_s
18
18
  lowered_c[0] = lowered_c[0].downcase
19
- [lowered_c.to_sym, Object::const_get("Unleash::Strategy::#{c}").new]
20
- }
19
+ [lowered_c.to_sym, Object.const_get("Unleash::Strategy::#{c}").new]
20
+ end
21
21
  .to_h
22
22
 
23
23
  class << self
@@ -30,12 +30,11 @@ module Unleash
30
30
  end
31
31
 
32
32
  # Support for configuration via yield:
33
- def self.configure()
33
+ def self.configure
34
34
  self.configuration ||= Unleash::Configuration.new
35
35
  yield(configuration)
36
36
 
37
37
  self.configuration.validate!
38
38
  self.configuration.refresh_backup_file!
39
39
  end
40
-
41
40
  end
@@ -3,11 +3,11 @@ require 'unleash/toggle_fetcher'
3
3
  require 'unleash/metrics_reporter'
4
4
  require 'unleash/scheduled_executor'
5
5
  require 'unleash/feature_toggle'
6
+ require 'unleash/util/http'
6
7
  require 'logger'
7
8
  require 'time'
8
9
 
9
10
  module Unleash
10
-
11
11
  class Client
12
12
  attr_accessor :fetcher_scheduled_executor, :metrics_scheduled_executor
13
13
 
@@ -18,71 +18,56 @@ module Unleash
18
18
  Unleash.logger = Unleash.configuration.logger.clone
19
19
  Unleash.logger.level = Unleash.configuration.log_level
20
20
 
21
- unless Unleash.configuration.disable_client
22
- Unleash.toggle_fetcher = Unleash::ToggleFetcher.new
23
-
24
- register
25
-
26
- self.fetcher_scheduled_executor = Unleash::ScheduledExecutor.new('ToggleFetcher', Unleash.configuration.refresh_interval)
27
- self.fetcher_scheduled_executor.run do
28
- Unleash.toggle_fetcher.fetch
29
- end
30
-
31
- unless Unleash.configuration.disable_metrics
32
- Unleash.toggle_metrics = Unleash::Metrics.new
33
- Unleash.reporter = Unleash::MetricsReporter.new
34
- self.metrics_scheduled_executor = Unleash::ScheduledExecutor.new('MetricsReporter', Unleash.configuration.metrics_interval)
35
- self.metrics_scheduled_executor.run do
36
- Unleash.reporter.send
37
- end
38
- end
39
- else
21
+ if Unleash.configuration.disable_client
40
22
  Unleash.logger.warn "Unleash::Client is disabled! Will only return default results!"
23
+ return
41
24
  end
25
+
26
+ register
27
+ start_toggle_fetcher
28
+ start_metrics unless Unleash.configuration.disable_metrics
42
29
  end
43
30
 
44
31
  def is_enabled?(feature, context = nil, default_value = false)
45
- Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} with context #{context}"
32
+ Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} with context #{context}"
46
33
 
47
- if Unleash.configuration.disable_client
48
- Unleash.logger.warn "unleash_client is disabled! Always returning #{default_value} for feature #{feature}!"
49
- return default_value
50
- end
34
+ if Unleash.configuration.disable_client
35
+ Unleash.logger.warn "unleash_client is disabled! Always returning #{default_value} for feature #{feature}!"
36
+ return default_value
37
+ end
51
38
 
52
- toggle_as_hash = Unleash.toggles.select{ |toggle| toggle['name'] == feature }.first if Unleash.toggles
39
+ toggle_as_hash = Unleash&.toggles&.select{ |toggle| toggle['name'] == feature }&.first
53
40
 
54
- if toggle_as_hash.nil?
55
- Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
56
- return default_value
57
- end
41
+ if toggle_as_hash.nil?
42
+ Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
43
+ return default_value
44
+ end
58
45
 
59
- toggle = Unleash::FeatureToggle.new(toggle_as_hash)
60
- toggle_result = toggle.is_enabled?(context, default_value)
46
+ toggle = Unleash::FeatureToggle.new(toggle_as_hash)
61
47
 
62
- return toggle_result
48
+ toggle.is_enabled?(context, default_value)
63
49
  end
64
50
 
65
51
  # enabled? is a more ruby idiomatic method name than is_enabled?
66
- alias_method :enabled?, :is_enabled?
52
+ alias enabled? is_enabled?
67
53
 
68
54
  # execute a code block (passed as a parameter), if is_enabled? is true.
69
55
  def if_enabled(feature, context = nil, default_value = false, &blk)
70
- yield if is_enabled?(feature, context, default_value)
56
+ yield(blk) if is_enabled?(feature, context, default_value)
71
57
  end
72
58
 
73
-
74
- def get_variant(feature, context = nil, fallback_variant = false)
59
+ def get_variant(feature, context = nil, fallback_variant = nil)
75
60
  Unleash.logger.debug "Unleash::Client.get_variant for feature: #{feature} with context #{context}"
76
61
 
77
62
  if Unleash.configuration.disable_client
78
- Unleash.logger.warn "unleash_client is disabled! Always returning #{default_variant} for feature #{feature}!"
63
+ Unleash.logger.debug "unleash_client is disabled! Always returning #{default_variant} for feature #{feature}!"
79
64
  return fallback_variant || Unleash::FeatureToggle.disabled_variant
80
65
  end
81
66
 
82
- toggle_as_hash = Unleash.toggles.select{ |toggle| toggle['name'] == feature }.first if Unleash.toggles
67
+ toggle_as_hash = Unleash&.toggles&.select{ |toggle| toggle['name'] == feature }&.first
83
68
 
84
69
  if toggle_as_hash.nil?
85
- Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
70
+ Unleash.logger.debug "Unleash::Client.get_variant feature: #{feature} not found"
86
71
  return fallback_variant || Unleash::FeatureToggle.disabled_variant
87
72
  end
88
73
 
@@ -96,7 +81,7 @@ module Unleash
96
81
 
97
82
  # TODO: Add to README: name, payload, enabled (bool)
98
83
 
99
- return variant
84
+ variant
100
85
  end
101
86
 
102
87
  # safe shutdown: also flush metrics to server and toggles to disk
@@ -117,9 +102,10 @@ module Unleash
117
102
  end
118
103
 
119
104
  private
105
+
120
106
  def info
121
- return {
122
- 'appName': Unleash.configuration.app_name,
107
+ {
108
+ 'appName': Unleash.configuration.app_name,
123
109
  'instanceId': Unleash.configuration.instance_id,
124
110
  'sdkVersion': "unleash-client-ruby:" + Unleash::VERSION,
125
111
  'strategies': Unleash::STRATEGIES.keys,
@@ -128,28 +114,34 @@ module Unleash
128
114
  }
129
115
  end
130
116
 
131
- def register
132
- Unleash.logger.debug "register()"
133
-
134
- uri = URI(Unleash.configuration.client_register_url)
135
- http = Net::HTTP.new(uri.host, uri.port)
136
- http.use_ssl = true if uri.scheme == 'https'
137
- http.open_timeout = Unleash.configuration.timeout # in seconds
138
- http.read_timeout = Unleash.configuration.timeout # in seconds
117
+ def start_toggle_fetcher
118
+ Unleash.toggle_fetcher = Unleash::ToggleFetcher.new
119
+ self.fetcher_scheduled_executor = Unleash::ScheduledExecutor.new('ToggleFetcher', Unleash.configuration.refresh_interval)
120
+ self.fetcher_scheduled_executor.run do
121
+ Unleash.toggle_fetcher.fetch
122
+ end
123
+ end
139
124
 
140
- headers = (Unleash.configuration.get_http_headers || {}).dup
141
- headers['Content-Type'] = 'application/json'
125
+ def start_metrics
126
+ Unleash.toggle_metrics = Unleash::Metrics.new
127
+ Unleash.reporter = Unleash::MetricsReporter.new
128
+ self.metrics_scheduled_executor = Unleash::ScheduledExecutor.new('MetricsReporter', Unleash.configuration.metrics_interval)
129
+ self.metrics_scheduled_executor.run do
130
+ Unleash.reporter.send
131
+ end
132
+ end
142
133
 
143
- request = Net::HTTP::Post.new(uri.request_uri, headers)
144
- request.body = info.to_json
134
+ def register
135
+ Unleash.logger.debug "register()"
145
136
 
146
137
  # Send the request, if possible
147
138
  begin
148
- response = http.request(request)
149
- rescue Exception => e
139
+ response = Unleash::Util::Http.post(Unleash.configuration.client_register_url, info.to_json)
140
+ rescue StandardError => e
150
141
  Unleash.logger.error "unable to register client with unleash server due to exception #{e.class}:'#{e}'."
151
142
  Unleash.logger.error "stacktrace: #{e.backtrace}"
152
143
  end
144
+ Unleash.logger.debug "client registered: #{response}"
153
145
  end
154
146
  end
155
147
  end