Skip to content

Commit

Permalink
Merge branch 'master' into feature/misc_improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rarruda committed Dec 13, 2021
2 parents fe13654 + eb240c2 commit cc16b62
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 49 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ jobs:
run: bundle install
- name: Download test cases
run: git clone --depth 5 --branch v3.3.0 https://github.com/Unleash/client-specification.git client-specification
- name: rubocop
uses: reviewdog/action-rubocop@v2
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
rubocop_version: gemfile
rubocop_extensions: rubocop-rspec:gemfile
reporter: github-pr-review # Default is github-pr-check
- name: Run tests
run: bundle exec rake
env:
Expand Down
110 changes: 107 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ Layout/LineLength:
Metrics/MethodLength:
Max: 20
Metrics/BlockLength:
Max: 100
Max: 110
Exclude:
- 'spec/unleash/configuration_spec.rb'
- 'spec/unleash/client_spec.rb'
- 'spec/unleash/context_spec.rb'
- 'spec/unleash/feature_toggle_spec.rb'

Metrics/AbcSize:
Max: 25
Max: 28
Metrics/CyclomaticComplexity:
Max: 9
Metrics/PerceivedComplexity:
Max: 9
Max: 10

Style/Documentation:
Enabled: false
Expand All @@ -51,6 +53,9 @@ Style/HashTransformKeys:
Enabled: true
Style/HashTransformValues:
Enabled: true
Style/EmptyElse:
Exclude:
- 'lib/unleash/strategy/flexible_rollout.rb'

Style/DoubleNegation:
Enabled: false
Expand All @@ -63,6 +68,58 @@ Style/Next:
Exclude:
- 'lib/unleash/scheduled_executor.rb'


Style/AccessorGrouping:
Enabled: true
Style/BisectedAttrAccessor:
Enabled: true
Style/CaseLikeIf:
Enabled: true
#Style/ClassEqualityComparison:
# Enabled: true
Style/CombinableLoops:
Enabled: true
Style/ExplicitBlockArgument:
Enabled: true
Style/ExponentialNotation:
Enabled: true
#Style/GlobalStdStream:
# Enabled: true
Style/HashAsLastArrayItem:
Enabled: true
Style/HashLikeCase:
Enabled: true
Style/KeywordParametersOrder:
Enabled: true
#Style/OptionalBooleanParameter:
# Enabled: false
Style/RedundantAssignment:
Enabled: true
Style/RedundantFetchBlock:
Enabled: true
Style/RedundantFileExtensionInRequire:
Enabled: true
Style/RedundantRegexpCharacterClass:
Enabled: true
Style/RedundantRegexpEscape:
Enabled: true
Style/RedundantSelfAssignment:
Enabled: true
Style/SingleArgumentDig:
Enabled: true
Style/SlicingWithRange:
Enabled: true
Style/SoleNestedConditional:
Enabled: true
Style/StringConcatenation:
Enabled: false

Layout/BeginEndAlignment:
Enabled: true
Layout/EmptyLinesAroundAttributeAccessor:
Enabled: true
Layout/SpaceAroundMethodCallOperator:
Enabled: true
Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented

Expand All @@ -71,3 +128,50 @@ Layout/SpaceBeforeBlockBraces:
Exclude:
- 'unleash-client.gemspec'
- 'spec/**/*.rb'

Lint/BinaryOperatorWithIdenticalOperands:
Enabled: true
Lint/ConstantDefinitionInBlock:
Enabled: false
Lint/DeprecatedOpenSSLConstant:
Enabled: true
Lint/DuplicateElsifCondition:
Enabled: true
Lint/DuplicateRequire:
Enabled: true
Lint/DuplicateRescueException:
Enabled: true
Lint/EmptyConditionalBody:
Enabled: true
Lint/EmptyFile:
Enabled: true
Lint/FloatComparison:
Enabled: true
Lint/HashCompareByIdentity:
Enabled: true
Lint/IdentityComparison:
Enabled: true
Lint/MissingSuper:
Enabled: false
Lint/MixedRegexpCaptureTypes:
Enabled: true
Lint/OutOfRangeRegexpRef:
Enabled: true
Lint/RaiseException:
Enabled: true
Lint/RedundantSafeNavigation:
Enabled: true
Lint/SelfAssignment:
Enabled: true
Lint/StructNewOverride:
Enabled: true
Lint/TopLevelReturnWithArgument:
Enabled: true
Lint/TrailingCommaInAttributeDeclaration:
Enabled: true
Lint/UnreachableLoop:
Enabled: true
Lint/UselessMethodDefinition:
Enabled: true
Lint/UselessTimes:
Enabled: true
2 changes: 1 addition & 1 deletion bin/unleash-client
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ log_level = \
log_level: log_level
)

context_params = ARGV.map{ |e| e.split("=") }.map{ |k, v| [k.to_sym, v] }.to_h
context_params = ARGV.map{ |e| e.split("=") }.transform_keys(&:to_sym)
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?
Expand Down
7 changes: 3 additions & 4 deletions examples/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# Unleash.configure do |config|
# config.url = 'http://unleash.herokuapp.com/api'
# config.custom_http_headers = {'Authorization': '943ca9171e2c884c545c5d82417a655fb77cec970cc3b78a8ff87f4406b495d0'}
# config.custom_http_headers = { 'Authorization': '943ca9171e2c884c545c5d82417a655fb77cec970cc3b78a8ff87f4406b495d0' }
# config.app_name = 'simple-test'
# config.refresh_interval = 2
# config.metrics_interval = 2
Expand All @@ -19,13 +19,12 @@

@unleash = Unleash::Client.new(
url: 'http://unleash.herokuapp.com/api',
custom_http_headers: {'Authorization': '943ca9171e2c884c545c5d82417a655fb77cec970cc3b78a8ff87f4406b495d0'},
custom_http_headers: { 'Authorization': '943ca9171e2c884c545c5d82417a655fb77cec970cc3b78a8ff87f4406b495d0' },
app_name: 'simple-test',
instance_id: 'local-test-cli',
refresh_interval: 2,
metrics_interval: 2,
retry_limit: 2,
custom_http_headers: {'Authorization': '943ca9171e2c884c545c5d82417a655fb77cec970cc3b78a8ff87f4406b495d0'},
retry_limit: 2
)

# feature_name = "AwesomeFeature"
Expand Down
10 changes: 5 additions & 5 deletions lib/unleash/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def initialize(*opts)
def is_enabled?(feature, context = nil, default_value_param = false, &fallback_blk)
Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} with context #{context}"

if block_given?
default_value = default_value_param || !!fallback_blk.call(feature, context)
else
default_value = default_value_param
end
default_value = if block_given?
default_value_param || !!fallback_blk.call(feature, context)
else
default_value_param
end

if Unleash.configuration.disable_client
Unleash.logger.warn "unleash_client is disabled! Always returning #{default_value} for feature #{feature}!"
Expand Down
2 changes: 1 addition & 1 deletion lib/unleash/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_by_name(name)
if ATTRS.include? normalized_name
self.send(normalized_name)
else
self.properties.fetch(normalized_name)
self.properties.fetch(normalized_name, nil) || self.properties.fetch(name.to_sym)
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/unleash/strategy/application_hostname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Unleash
module Strategy
class ApplicationHostname < Base
attr_accessor :hostname

PARAM = 'hostnames'.freeze

def initialize
Expand Down
11 changes: 6 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
config.single_report_path = 'coverage/lcov.info'
end

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::LcovFormatter
])
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new(
[
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::LcovFormatter
]
)

SimpleCov.start

Expand All @@ -19,7 +21,6 @@

require 'webmock/rspec'


WebMock.disable_net_connect!(allow_localhost: false)

RSpec.configure do |config|
Expand Down
56 changes: 29 additions & 27 deletions spec/unleash/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@
let(:client) { Unleash::Client.new }
let(:feature) { 'awesome-feature' }
let(:fallback_variant) { Unleash::Variant.new(name: 'default', enabled: true) }
let(:variants) {
let(:variants) do
[
{
name: "a",
Expand All @@ -380,10 +380,10 @@
type: "string",
value: ""
}
},
}
]
}
let(:body) {
end
let(:body) do
{
version: 1,
features: [
Expand All @@ -397,34 +397,36 @@
}
]
}.to_json
}
end

before do
WebMock.stub_request(:post, "http://test-url/client/register")
.with(
headers: {
'Accept' => '*/*',
'Content-Type' => 'application/json',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent' => 'Ruby',
'X-Api-Key' => '123'
}
)
.to_return(status: 200, body: '', headers: {})
.with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => 'Ruby',
'X-Api-Key' => '123'
}
)
.to_return(status: 200, body: '', headers: {})

WebMock.stub_request(:get, "http://test-url/client/features")
.with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => 'Ruby',
'X-Api-Key' => '123'
}
)
.to_return(status: 200, body: body, headers: {})
.with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => 'Ruby',
'X-Api-Key' => '123'
}
)
.to_return(status: 200, body: body, headers: {})

Unleash.toggles = []
Unleash.configure do |config|
Expand Down
18 changes: 16 additions & 2 deletions spec/unleash/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
userId: '123',
session_id: 'verylongsesssionid',
properties: {
fancy: 'polarbear'
fancy: 'polarbear',
countryCode: 'DK'
}
}
context = Unleash::Context.new(params)
Expand All @@ -97,14 +98,21 @@
expect(context.get_by_name(:fancy)).to eq('polarbear')
expect(context.get_by_name('fancy')).to eq('polarbear')
expect(context.get_by_name('Fancy')).to eq('polarbear')
expect(context.get_by_name('countryCode')).to eq('DK')
expect(context.get_by_name(:countryCode)).to eq('DK')
expect{ context.get_by_name(:country_code) }.to raise_error(KeyError)
expect{ context.get_by_name('country_code') }.to raise_error(KeyError)
expect{ context.get_by_name('CountryCode') }.to raise_error(KeyError)
expect{ context.get_by_name(:CountryCode) }.to raise_error(KeyError)
end

it "when using get_by_name with keys as strings" do
params = {
'user_id' => '123',
'sessionId' => 'verylongsesssionid',
'properties' => {
'fancy' => 'polarbear'
'fancy' => 'polarbear',
'country_code' => 'UK'
}
}
context = Unleash::Context.new(params)
Expand All @@ -119,5 +127,11 @@
expect(context.get_by_name(:fancy)).to eq('polarbear')
expect(context.get_by_name('fancy')).to eq('polarbear')
expect(context.get_by_name('Fancy')).to eq('polarbear')
expect(context.get_by_name('country_code')).to eq('UK')
expect(context.get_by_name(:country_code)).to eq('UK')
expect(context.get_by_name('countryCode')).to eq('UK')
expect(context.get_by_name(:countryCode)).to eq('UK')
expect(context.get_by_name('CountryCode')).to eq('UK')
expect(context.get_by_name(:CountryCode)).to eq('UK')
end
end
2 changes: 1 addition & 1 deletion unleash-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec-json_expectations", "~> 2.2"
spec.add_development_dependency "webmock", "~> 3.8"

spec.add_development_dependency "rubocop", "~> 0.80"
spec.add_development_dependency "simplecov", "~> 0.21.2"
spec.add_development_dependency "simplecov-lcov", "~> 0.8.0"
spec.add_development_dependency "rubocop", "~> 0.80"
end

0 comments on commit cc16b62

Please sign in to comment.