Skip to content

Commit

Permalink
Refactor code as to make it more readable, maintanable and better fol…
Browse files Browse the repository at this point in the history
…low the ruby style guide
  • Loading branch information
rarruda committed Nov 26, 2019
1 parent 405dea7 commit 9421c0a
Show file tree
Hide file tree
Showing 43 changed files with 761 additions and 586 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
.rspec_status

# Clone of the client-specification
/client-specification/
/client-specification/
58 changes: 53 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,64 @@
# inherit_from: .rubocop_todo.yml

Naming/PredicateName:
NameWhitelist:
- is_enabled?

Metrics/LineLength:
Metrics/ClassLength:
Max: 120
Metrics/LineLength:
Max: 140
Metrics/MethodLength:
Max: 20
Metrics/BlockLength:
Max: 100
Exclude:
- 'spec/unleash/client_spec.rb'
- 'spec/unleash/feature_toggle_spec.rb'

Style/RedundantSelf:
Enabled: false
Style/PreferredHashMethods:
Metrics/AbcSize:
Max: 25
Metrics/CyclomaticComplexity:
Max: 8
Metrics/PerceivedComplexity:
Max: 8

Style/Documentation:
Enabled: false

Style/StringLiterals:
Enabled: false
Style/RedundantSelf:
Enabled: false

Style/SymbolArray:
EnforcedStyle: brackets
Style/WordArray:
EnforcedStyle: brackets
Style/PreferredHashMethods:
EnforcedStyle: verbose
Style/FrozenStringLiteralComment:
EnforcedStyle: never
Style/GuardClause:
MinBodyLength: 8

Style/BracesAroundHashParameters:
Exclude:
- 'spec/**/*.rb'

Style/IfInsideElse:
Exclude:
- 'bin/unleash-client'

Style/Next:
Exclude:
- 'lib/unleash/scheduled_executor.rb'

Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented

Layout/SpaceBeforeBlockBraces:
Enabled: false
EnforcedStyle: no_space
Exclude:
- 'unleash-client.gemspec'
- 'spec/**/*.rb'
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TODO
====

The Ruby client should be pretty stable now. But no warranty is given, and some work still remains.
The Ruby client should be pretty stable now, but no warranty is given.


Implement:
Expand Down
38 changes: 21 additions & 17 deletions bin/unleash-client
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ options = {
url: 'http://localhost:4242',
demo: false,
disable_metrics: true,
sleep: 0.1,
sleep: 0.1
}

OptionParser.new do |opts|
Expand Down Expand Up @@ -57,17 +57,26 @@ raise 'feature_name is required. see --help for usage.' unless feature_name

options[:verbose] = false if options[:quiet]

log_level = \
if options[:quiet]
Logger::ERROR
elsif options[:verbose]
Logger::DEBUG
else
Logger::WARN
end

@unleash = Unleash::Client.new(
url: options[:url],
app_name: 'unleash-client-ruby-cli',
disable_metrics: options[:metrics],
log_level: log_level = options[:quiet] ? Logger::ERROR : (options[:verbose] ? Logger::DEBUG : Logger::WARN),
log_level: log_level
)

context_params = ARGV.map{ |e| e.split("=") }.map{ |k,v| [k.to_sym, v] }.to_h
context_properties = context_params.reject{ |k,v| [:user_id, :session_id, :remote_address].include? k }
context_params.select!{ |k,v| [:user_id, :session_id, :remote_address].include? k }
context_params.merge!( properties: context_properties ) unless context_properties.nil?
context_params = ARGV.map{ |e| e.split("=") }.map{ |k, v| [k.to_sym, v] }.to_h
context_properties = context_params.reject{ |k, _v| [:user_id, :session_id, :remote_address].include? k }
context_params.select!{ |k, _v| [:user_id, :session_id, :remote_address].include? k }
context_params.merge!(properties: context_properties) unless context_properties.nil?
unleash_context = Unleash::Context.new(context_params)

if options[:verbose]
Expand All @@ -80,26 +89,21 @@ if options[:verbose]
puts ""
end



if options[:demo]
loop do
enabled = @unleash.is_enabled?(feature_name, unleash_context)
print enabled ? '.' : '|'
sleep options[:sleep]
end
elsif options[:variant]
variant = @unleash.get_variant(feature_name, unleash_context)
puts " For feature \'#{feature_name}\' got variant \'#{variant}\'"
else
if options[:variant]
variant = @unleash.get_variant(feature_name, unleash_context)
puts " For feature \'#{feature_name}\' got variant \'#{variant}\'"
if @unleash.is_enabled?(feature_name, unleash_context)
puts " \'#{feature_name}\' is enabled according to unleash"
else
if @unleash.is_enabled?(feature_name, unleash_context)
puts " \'#{feature_name}\' is enabled according to unleash"
else
puts " \'#{feature_name}\' is disabled according to unleash"
end
puts " \'#{feature_name}\' is disabled according to unleash"
end
end


@unleash.shutdown
8 changes: 4 additions & 4 deletions examples/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

# or:

@unleash = Unleash::Client.new( url: 'http://unleash.herokuapp.com/api', app_name: 'simple-test',
@unleash = Unleash::Client.new(
url: 'http://unleash.herokuapp.com/api',
app_name: 'simple-test',
instance_id: 'local-test-cli',
refresh_interval: 2,
metrics_interval: 2,
retry_limit: 2,
log_level: Logger::DEBUG,
log_level: Logger::DEBUG
)

# feature_name = "AwesomeFeature"
Expand Down Expand Up @@ -55,5 +57,3 @@
@unleash.shutdown

puts ">> END simple.rb"


15 changes: 7 additions & 8 deletions lib/unleash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
require 'unleash/client'
require 'logger'

Gem.find_files('unleash/strategy/**/*.rb').each { |path| require path }
Gem.find_files('unleash/strategy/**/*.rb').each{ |path| require path }

module Unleash
TIME_RESOLUTION = 3

STRATEGIES = Unleash::Strategy.constants
.select { |c| Unleash::Strategy.const_get(c).is_a? Class }
.select { |c| !['NotImplemented', 'Base'].include?(c.to_s) }
.map { |c|
.select{ |c| Unleash::Strategy.const_get(c).is_a? Class }
.reject{ |c| ['NotImplemented', 'Base'].include?(c.to_s) }
.map do |c|
lowered_c = c.to_s
lowered_c[0] = lowered_c[0].downcase
[lowered_c.to_sym, Object::const_get("Unleash::Strategy::#{c}").new]
}
[lowered_c.to_sym, Object.const_get("Unleash::Strategy::#{c}").new]
end
.to_h

class << self
Expand All @@ -30,12 +30,11 @@ def self.initialize
end

# Support for configuration via yield:
def self.configure()
def self.configure
self.configuration ||= Unleash::Configuration.new
yield(configuration)

self.configuration.validate!
self.configuration.refresh_backup_file!
end

end
Loading

0 comments on commit 9421c0a

Please sign in to comment.