Skip to content

Commit fabd7fb

Browse files
ci: add rubocop (#32)
* Rubcop fixes * Add rubocop action * Update rubocop and fix new linting issues Update .rubocop_todo * Drop ruby <2.7 support * Exclude vendor files
1 parent 105e19e commit fabd7fb

File tree

12 files changed

+67
-60
lines changed

12 files changed

+67
-60
lines changed

.github/workflows/pull_request.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
runs-on: ubuntu-latest
3232
strategy:
3333
matrix:
34-
ruby-version: ['2.4', '2.5', '2.6', '2.7', '3.0', '3.1']
34+
ruby-version: ['2.7', '3.0', '3.1', '3.2']
3535

3636
steps:
3737
- uses: actions/checkout@v3
@@ -44,5 +44,7 @@ jobs:
4444
with:
4545
ruby-version: ${{ matrix.ruby-version }}
4646
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
47+
- name: Linting
48+
run: bundle exec rubocop
4749
- name: Run tests
4850
run: bundle exec rspec spec

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ AllCops:
99
Exclude:
1010
- 'spec/**/*'
1111
- 'example/**/*'
12+
- 'vendor/**/*'

.rubocop_todo.yml

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
# This configuration was generated by
2-
# `rubocop --auto-gen-config --auto-gen-only-exclude`
3-
# on 2020-12-30 22:56:14 UTC using RuboCop version 1.7.0.
2+
# `rubocop --auto-gen-config`
3+
# on 2023-07-20 10:26:52 UTC using RuboCop version 1.54.2.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 1
10-
# Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods, IgnoredMethods.
11-
# IgnoredMethods: refine
12-
Metrics/BlockLength:
9+
# Offense count: 6
10+
# Configuration parameters: EnforcedStyle, AllowedGems, Include.
11+
# SupportedStyles: Gemfile, gems.rb, gemspec
12+
# Include: **/*.gemspec, **/Gemfile, **/gems.rb
13+
Gemspec/DevelopmentDependencies:
1314
Exclude:
14-
- '**/*.gemspec'
15-
16-
# Offense count: 1
17-
# Configuration parameters: AllowedMethods.
18-
# AllowedMethods: respond_to_missing?
19-
Style/OptionalBooleanParameter:
20-
Exclude:
21-
- 'lib/flagsmith.rb'
15+
- 'flagsmith.gemspec'
2216

23-
# Offense count: 1
24-
# Cop supports --auto-correct.
25-
# Configuration parameters: AutoCorrect, Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
17+
# Offense count: 4
18+
# This cop supports safe autocorrection (--autocorrect).
19+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
2620
# URISchemes: http, https
2721
Layout/LineLength:
28-
Exclude:
29-
- 'flagsmith.gemspec'
22+
Max: 183

Gemfile.lock

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ PATH
1010
GEM
1111
remote: https://rubygems.org/
1212
specs:
13-
ast (2.4.1)
13+
ast (2.4.2)
1414
coderay (1.1.3)
1515
diff-lcs (1.4.4)
1616
faraday (1.10.0)
@@ -39,17 +39,21 @@ GEM
3939
faraday_middleware (1.2.0)
4040
faraday (~> 1.0)
4141
gem-release (2.2.0)
42+
json (2.6.3)
43+
language_server-protocol (3.17.0.3)
4244
method_source (1.0.0)
4345
multipart-post (2.2.3)
44-
parallel (1.20.1)
45-
parser (3.0.0.0)
46+
parallel (1.23.0)
47+
parser (3.2.2.3)
4648
ast (~> 2.4.1)
49+
racc
4750
pry (0.14.1)
4851
coderay (~> 1.1)
4952
method_source (~> 1.0)
50-
rainbow (3.0.0)
53+
racc (1.7.1)
54+
rainbow (3.1.1)
5155
rake (13.0.3)
52-
regexp_parser (2.0.3)
56+
regexp_parser (2.8.1)
5357
rexml (3.2.5)
5458
rspec (3.10.0)
5559
rspec-core (~> 3.10.0)
@@ -64,21 +68,23 @@ GEM
6468
diff-lcs (>= 1.2.0, < 2.0)
6569
rspec-support (~> 3.10.0)
6670
rspec-support (3.10.1)
67-
rubocop (1.7.0)
71+
rubocop (1.54.2)
72+
json (~> 2.3)
73+
language_server-protocol (>= 3.17.0)
6874
parallel (~> 1.10)
69-
parser (>= 2.7.1.5)
75+
parser (>= 3.2.2.3)
7076
rainbow (>= 2.2.2, < 4.0)
7177
regexp_parser (>= 1.8, < 3.0)
72-
rexml
73-
rubocop-ast (>= 1.2.0, < 2.0)
78+
rexml (>= 3.2.5, < 4.0)
79+
rubocop-ast (>= 1.28.0, < 2.0)
7480
ruby-progressbar (~> 1.7)
75-
unicode-display_width (>= 1.4.0, < 2.0)
76-
rubocop-ast (1.3.0)
77-
parser (>= 2.7.1.5)
78-
ruby-progressbar (1.10.1)
81+
unicode-display_width (>= 2.4.0, < 3.0)
82+
rubocop-ast (1.29.0)
83+
parser (>= 3.2.1.0)
84+
ruby-progressbar (1.13.0)
7985
ruby2_keywords (0.0.5)
8086
semantic (1.6.1)
81-
unicode-display_width (1.7.0)
87+
unicode-display_width (2.4.2)
8288

8389
PLATFORMS
8490
ruby

flagsmith.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ Gem::Specification.new do |spec|
3535
spec.add_dependency 'faraday_middleware'
3636
spec.add_dependency 'faraday-retry'
3737
spec.add_dependency 'semantic'
38+
spec.metadata['rubygems_mfa_required'] = 'true'
3839
end

lib/flagsmith.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# no-doc
2323
module Flagsmith
2424
# Ruby client for flagsmith.com
25-
class Client
25+
class Client # rubocop:disable Metrics/ClassLength
2626
extend Forwardable
2727
# A Flagsmith client.
2828
#
@@ -153,13 +153,13 @@ def get_value_for_identity(feature_name, user_id = nil, default: nil)
153153

154154
def get_identity_segments(identifier, traits = {})
155155
unless environment
156-
raise Flagsmith::ClientError,
157-
'Local evaluation required to obtain identity segments.'
156+
raise Flagsmith::ClientError,
157+
'Local evaluation required to obtain identity segments.'
158158
end
159159

160160
identity_model = build_identity_model(identifier, traits)
161161
segment_models = engine.get_identity_segments(environment, identity_model)
162-
return segment_models.map { |sm| Flagsmith::Segments::Segment.new(id: sm.id, name: sm.name) }.compact
162+
segment_models.map { |sm| Flagsmith::Segments::Segment.new(id: sm.id, name: sm.name) }.compact
163163
end
164164

165165
private

lib/flagsmith/engine/features/models.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ def multivariate_value(identity_id)
4040
multivariate_feature_state_values.sort.each do |multi_fs_value|
4141
limit = multi_fs_value.percentage_allocation + start_percentage
4242

43-
if start_percentage <= percentage_value && percentage_value < limit
44-
return multi_fs_value.multivariate_feature_option.value
45-
end
43+
return multi_fs_value.multivariate_feature_option.value if start_percentage <= percentage_value && percentage_value < limit
4644

4745
start_percentage = limit
4846
end
@@ -165,7 +163,7 @@ class Segment
165163
attr_reader :priority
166164

167165
def initialize(params = {})
168-
@priority = params[:priority].nil? ? nil : params[:priority].to_i
166+
@priority = params[:priority]&.to_i
169167
end
170168
end
171169
end

lib/flagsmith/engine/segments/evaluator.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def evaluate_identity_in_segment(identity, segment, override_traits = nil)
3636
end
3737
end
3838

39+
# rubocop:disable Metrics/MethodLength
3940
def traits_match_segment_rule(identity_traits, rule, segment_id, identity_id)
4041
matching_block = lambda { |condition|
4142
traits_match_segment_condition(identity_traits, condition, segment_id, identity_id)
@@ -44,23 +45,25 @@ def traits_match_segment_rule(identity_traits, rule, segment_id, identity_id)
4445
matches_conditions =
4546
if rule.conditions&.length&.positive?
4647
rule.conditions.send(rule.matching_function, &matching_block)
47-
else true
48+
else
49+
true
4850
end
4951

5052
matches_conditions &&
5153
rule.rules.all? { |r| traits_match_segment_rule(identity_traits, r, segment_id, identity_id) }
5254
end
55+
# rubocop:enable Metrics/MethodLength
5356

5457
def traits_match_segment_condition(identity_traits, condition, segment_id, identity_id)
5558
if condition.operator == PERCENTAGE_SPLIT
56-
return hashed_percentage_for_object_ids([segment_id, identity_id]) <= condition.value.to_f
59+
return hashed_percentage_for_object_ids([segment_id,
60+
identity_id]) <= condition.value.to_f
5761
end
5862

5963
trait = identity_traits.find { |t| t.key.to_s == condition.property }
6064

61-
if [IS_SET, IS_NOT_SET].include?(condition.operator)
62-
return handle_trait_existence_conditions(trait, condition.operator)
63-
end
65+
return handle_trait_existence_conditions(trait, condition.operator) if [IS_SET,
66+
IS_NOT_SET].include?(condition.operator)
6467

6568
return condition.match_trait_value?(trait.trait_value) if trait
6669

lib/flagsmith/engine/segments/models.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ def initialize(operator:, value:, property: nil)
5656

5757
def match_trait_value?(trait_value)
5858
# handle some exceptions
59-
if @value.is_a?(String) && @value.match?(/:semver$/)
60-
trait_value = Semantic::Version.new(trait_value.gsub(/:semver$/, ''))
61-
end
59+
trait_value = Semantic::Version.new(trait_value.gsub(/:semver$/, '')) if @value.is_a?(String) && @value.match?(/:semver$/)
6260

6361
return match_modulo_value(trait_value) if @operator == MODULO
6462

@@ -73,8 +71,8 @@ def format_to_type_of(input)
7371
{
7472
'String' => ->(v) { v.to_s },
7573
'Semantic::Version' => ->(v) { Semantic::Version.new(v.to_s.gsub(/:semver$/, '')) },
76-
'TrueClass' => ->(v) { ['True', 'true', 'TRUE', true, 1, '1'].include?(v) ? true : false },
77-
'FalseClass' => ->(v) { ['False', 'false', 'FALSE', false, 0, '0'].include?(v) ? false : true },
74+
'TrueClass' => ->(v) { ['True', 'true', 'TRUE', true, 1, '1'].include?(v) },
75+
'FalseClass' => ->(v) { !['False', 'false', 'FALSE', false, 0, '0'].include?(v) },
7876
'Integer' => ->(v) { v.to_i },
7977
'Float' => ->(v) { v.to_f }
8078
}[input.class.to_s]
@@ -83,7 +81,7 @@ def format_to_type_of(input)
8381

8482
def match_modulo_value(trait_value)
8583
divisor, remainder = @value.split('|')
86-
trait_value.is_a?(Numeric) && trait_value % divisor.to_f == remainder.to_f
84+
trait_value.is_a?(Numeric) && trait_value % divisor.to_f == remainder.to_f # rubocop:disable Lint/FloatComparison
8785
rescue StandardError
8886
false
8987
end

lib/flagsmith/sdk/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def build_config(options)
8484

8585
class << self
8686
def environment_key
87-
ENV['FLAGSMITH_ENVIRONMENT_KEY']
87+
ENV.fetch('FLAGSMITH_ENVIRONMENT_KEY', nil)
8888
end
8989
end
9090
end

0 commit comments

Comments
 (0)