Skip to content

Commit 9ab7ff9

Browse files
committed
Fix Rubocop failures
1 parent 356d65c commit 9ab7ff9

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

lib/flagsmith.rb

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
2-
require "pry"
3-
require "pry-byebug"
2+
3+
require 'pry'
4+
require 'pry-byebug'
45

56
require 'faraday'
67
require 'faraday/retry'
@@ -70,14 +71,14 @@ def initialize(config)
7071
end
7172

7273
def validate_offline_mode!
73-
if @config.offline_mode? and not @config.offline_handler
74+
if @config.offline_mode? && !@config.offline_handler
7475
raise Flagsmith::ClientError,
7576
'The offline_mode config param requires a matching offline_handler.'
7677
end
77-
if @config.offline_handler and @config.default_flag_handler
78-
raise Flagsmith::ClientError,
79-
'Cannot use offline_handler and default_flag_handler at the same time.'
80-
end
78+
return unless @config.offline_handler && @config.default_flag_handler
79+
80+
raise Flagsmith::ClientError,
81+
'Cannot use offline_handler and default_flag_handler at the same time.'
8182
end
8283

8384
def api_client
@@ -126,9 +127,7 @@ def environment_from_api
126127
# Get all the default for flags for the current environment.
127128
# @returns Flags object holding all the flags for the current environment.
128129
def get_environment_flags # rubocop:disable Naming/AccessorMethodName
129-
if @config.local_evaluation? or @config.offline_mode
130-
return environment_flags_from_document
131-
end
130+
return environment_flags_from_document if @config.local_evaluation? || @config.offline_mode
132131

133132
environment_flags_from_api
134133
end
@@ -194,7 +193,7 @@ def environment_flags_from_document
194193
engine.get_environment_feature_states(environment),
195194
analytics_processor: analytics_processor,
196195
default_flag_handler: default_flag_handler,
197-
offline_handler: offline_handler,
196+
offline_handler: offline_handler
198197
)
199198
end
200199

@@ -205,23 +204,25 @@ def get_identity_flags_from_document(identifier, traits = {})
205204
engine.get_identity_feature_states(environment, identity_model),
206205
analytics_processor: analytics_processor,
207206
default_flag_handler: default_flag_handler,
208-
offline_handler: offline_handler,
207+
offline_handler: offline_handler
209208
)
210209
end
211210

211+
# rubocop:disable Metrics/MethodLength
212212
def environment_flags_from_api
213213
if offline_handler
214214
begin
215-
return process_environment_flags_from_api
216-
rescue
217-
return environment_flags_from_document
215+
process_environment_flags_from_api
216+
rescue StandardError
217+
environment_flags_from_document
218218
end
219219
else
220220
rescue_with_default_handler do
221221
return process_environment_flags_from_api
222222
end
223223
end
224224
end
225+
# rubocop:enable Metrics/MethodLength
225226

226227
def process_environment_flags_from_api
227228
api_flags = api_client.get(@config.environment_flags_url).body
@@ -234,19 +235,21 @@ def process_environment_flags_from_api
234235
)
235236
end
236237

238+
# rubocop:disable Metrics/MethodLength
237239
def get_identity_flags_from_api(identifier, traits = {})
238240
if offline_handler
239241
begin
240-
return process_identity_flags_from_api(identifier, traits)
241-
rescue
242-
return get_identity_flags_from_document(identifier, traits)
242+
process_identity_flags_from_api(identifier, traits)
243+
rescue StandardError
244+
get_identity_flags_from_document(identifier, traits)
243245
end
244246
else
245247
rescue_with_default_handler do
246248
return process_identity_flags_from_api(identifier, traits)
247249
end
248250
end
249251
end
252+
# rubocop:enable Metrics/MethodLength
250253

251254
def process_identity_flags_from_api(identifier, traits = {})
252255
data = generate_identities_data(identifier, traits)

lib/flagsmith/sdk/models/flags.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ def feature_value(feature_name)
120120
end
121121
alias get_feature_value feature_value
122122

123+
def get_flag_from_offline_handler(key)
124+
@offline_handler.environment.feature_states.each do |feature_state|
125+
return Flag.from_feature_state_model(feature_state, nil) if key == Flagsmith::Flags::Collection.normalize_key(feature_state.feature.name)
126+
end
127+
raise Flagsmith::Flags::NotFound,
128+
"Feature does not exist: #{key}, offline_handler did not find a flag in this case."
129+
end
130+
123131
# Get a specific flag given the feature name.
124132
# :param feature_name: the name of the feature to retrieve the flag for.
125133
# :return: BaseFlag object.
@@ -131,16 +139,7 @@ def get_flag(feature_name)
131139
@analytics_processor.track_feature(flag.feature_name) if @analytics_processor && flag.feature_id
132140
flag
133141
rescue KeyError
134-
if @offline_handler
135-
@offline_handler.environment.feature_states.each do |feature_state|
136-
if key == Flagsmith::Flags::Collection.normalize_key(feature_state.feature.name)
137-
return Flag.from_feature_state_model(feature_state, nil)
138-
end
139-
end
140-
141-
raise Flagsmith::Flags::NotFound,
142-
"Feature does not exist: #{key}, offline_handler did not find a flag in this case."
143-
end
142+
return get_flag_from_offline_handler(key) if @offline_handler
144143

145144
return @default_flag_handler.call(feature_name) if @default_flag_handler
146145

lib/flagsmith/sdk/offline_handlers.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
module Flagsmith
44
module OfflineHandlers
5+
# Provides the offline_handler to the Flagsmith::Client.
56
class LocalFileHandler
6-
77
attr_reader :environment
88

99
def initialize(environment_document_path)
@@ -12,7 +12,6 @@ def initialize(environment_document_path)
1212
data = JSON.parse(environment_file.read, symbolize_names: true)
1313
@environment = Flagsmith::Engine::Environment.build(data)
1414
end
15-
1615
end
1716
end
1817
end

0 commit comments

Comments
 (0)