Skip to content

Commit cbb6fd3

Browse files
oakbanimikeproeng37
authored andcommitted
Enforce Rubocop via Travis (#72)
1 parent 3c93241 commit cbb6fd3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1046
-972
lines changed

.rubocop.yml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
inherit_from: .rubocop_todo.yml
2+
3+
AllCops:
4+
TargetRubyVersion: 2.3
5+
16
Metrics/AbcSize:
27
Enabled: false
38

@@ -10,26 +15,29 @@ Metrics/PerceivedComplexity:
1015
Metrics/ClassLength:
1116
Enabled: false
1217

18+
Metrics/BlockLength:
19+
Enabled: false
20+
1321
Metrics/LineLength:
14-
Max: 120
22+
Enabled: false
1523

1624
Metrics/MethodLength:
1725
Enabled: false
1826

19-
Style/AccessorMethodName:
27+
Metrics/ModuleLength:
2028
Enabled: false
2129

2230
Style/Documentation:
2331
Enabled: false
2432

25-
Style/FormatString:
26-
Enabled: false
27-
2833
Style/SignalException:
2934
EnforcedStyle: only_raise
3035

31-
Style/SpaceInsideHashLiteralBraces:
36+
Layout/SpaceInsideHashLiteralBraces:
3237
EnforcedStyle: no_space
3338

34-
Style/WordArray:
39+
Naming/VariableNumber:
3540
Enabled: false
41+
42+
Lint/RescueWithoutErrorClass:
43+
Enabled: false

.rubocop_todo.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2017-12-06 17:48:00 +0500 using RuboCop version 0.51.0.
4+
# The point is for the user to remove these configuration records
5+
# one by one as the offenses are removed from the code base.
6+
# Note that changes in the inspected code, or installation of new
7+
# versions of RuboCop, may require this file to be generated again.
8+
9+
# Offense count: 1
10+
Lint/HandleExceptions:
11+
Exclude:
12+
- 'Rakefile'
13+
14+
# Offense count: 1
15+
Lint/LiteralAsCondition:
16+
Exclude:
17+
- 'lib/optimizely/project_config.rb'
18+
19+
# Offense count: 1
20+
# Configuration parameters: CountKeywordArgs.
21+
Metrics/ParameterLists:
22+
Max: 6
23+
24+
# Offense count: 1
25+
# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist, MethodDefinitionMacros.
26+
# NamePrefix: is_, has_, have_
27+
# NamePrefixBlacklist: is_, has_, have_
28+
# NameWhitelist: is_a?
29+
# MethodDefinitionMacros: define_method, define_singleton_method
30+
Naming/PredicateName:
31+
Exclude:
32+
- 'spec/**/*'
33+
- 'lib/optimizely.rb'

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ before_install: gem install bundler -v 1.10.6
55
install:
66
- bundle install
77
- bundle exec rake install
8+
before_script: "rubocop"
89
script: "rake spec"
910
after_success: "coveralls"

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

35
# Specify your gem's dependencies in optimizely-sdk.gemspec

Rakefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'bundler/gem_tasks'
24
require_relative 'spec/benchmarking/benchmark'
35

@@ -14,4 +16,4 @@ task :benchmark do
1416
OptimizelyBenchmark.run_tests
1517
end
1618

17-
task :default => :spec
19+
task default: :spec

lib/optimizely.rb

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
#
24
# Copyright 2016-2017, Optimizely and contributors
35
#
@@ -29,7 +31,6 @@
2931

3032
module Optimizely
3133
class Project
32-
3334
# Boolean representing if the instance represents a usable Optimizely Project
3435
attr_reader :is_valid
3536

@@ -148,9 +149,7 @@ def get_variation(experiment_key, user_id, attributes = nil)
148149

149150
unless variation_id.nil?
150151
variation = @config.get_variation_from_id(experiment_key, variation_id)
151-
if variation
152-
return variation['key']
153-
end
152+
return variation['key'] if variation
154153
end
155154
nil
156155
end
@@ -165,7 +164,7 @@ def set_forced_variation(experiment_key, user_id, variation_key)
165164
#
166165
# Returns - Boolean - indicates if the set completed successfully.
167166

168-
@config.set_forced_variation(experiment_key, user_id, variation_key);
167+
@config.set_forced_variation(experiment_key, user_id, variation_key)
169168
end
170169

171170
def get_forced_variation(experiment_key, user_id)
@@ -177,10 +176,8 @@ def get_forced_variation(experiment_key, user_id)
177176
# Returns String|nil The forced variation key.
178177

179178
forced_variation_key = nil
180-
forced_variation = @config.get_forced_variation(experiment_key, user_id);
181-
if forced_variation
182-
forced_variation_key = forced_variation['key']
183-
end
179+
forced_variation = @config.get_forced_variation(experiment_key, user_id)
180+
forced_variation_key = forced_variation['key'] if forced_variation
184181

185182
forced_variation_key
186183
end
@@ -199,7 +196,7 @@ def track(event_key, user_id, attributes = nil, event_tags = nil)
199196
return nil
200197
end
201198

202-
if event_tags and event_tags.is_a? Numeric
199+
if event_tags && event_tags.is_a?(Numeric)
203200
event_tags = {
204201
'revenue' => event_tags
205202
}
@@ -227,19 +224,18 @@ def track(event_key, user_id, attributes = nil, event_tags = nil)
227224
conversion_event = @event_builder.create_conversion_event(event_key, user_id, attributes,
228225
event_tags, experiment_variation_map)
229226
@logger.log(Logger::INFO,
230-
'Dispatching conversion event to URL %s with params %s.' % [conversion_event.url,
231-
conversion_event.params])
227+
"Dispatching conversion event to URL #{conversion_event.url} with params #{conversion_event.params}.")
232228
begin
233229
@event_dispatcher.dispatch_event(conversion_event)
234230
rescue => e
235231
@logger.log(Logger::ERROR, "Unable to dispatch conversion event. Error: #{e}")
236232
end
237233

238234
@notification_center.send_notifications(
239-
NotificationCenter::NOTIFICATION_TYPES[:TRACK],
240-
event_key, user_id, attributes, event_tags, conversion_event
235+
NotificationCenter::NOTIFICATION_TYPES[:TRACK],
236+
event_key, user_id, attributes, event_tags, conversion_event
241237
)
242-
return nil
238+
nil
243239
end
244240

245241
def is_feature_enabled(feature_flag_key, user_id, attributes = nil)
@@ -282,7 +278,7 @@ def is_feature_enabled(feature_flag_key, user_id, attributes = nil)
282278
end
283279

284280
@logger.log(Logger::INFO,
285-
"Feature '#{feature_flag_key}' is not enabled for user '#{user_id}'.")
281+
"Feature '#{feature_flag_key}' is not enabled for user '#{user_id}'.")
286282
false
287283
end
288284

@@ -300,12 +296,12 @@ def get_feature_variable_string(feature_flag_key, variable_key, user_id, attribu
300296
variable_value = get_feature_variable_for_type(
301297
feature_flag_key,
302298
variable_key,
303-
Optimizely::Helpers::Constants::VARIABLE_TYPES["STRING"],
299+
Optimizely::Helpers::Constants::VARIABLE_TYPES['STRING'],
304300
user_id,
305301
attributes
306302
)
307303

308-
return variable_value
304+
variable_value
309305
end
310306

311307
def get_feature_variable_boolean(feature_flag_key, variable_key, user_id, attributes = nil)
@@ -322,12 +318,12 @@ def get_feature_variable_boolean(feature_flag_key, variable_key, user_id, attrib
322318
variable_value = get_feature_variable_for_type(
323319
feature_flag_key,
324320
variable_key,
325-
Optimizely::Helpers::Constants::VARIABLE_TYPES["BOOLEAN"],
321+
Optimizely::Helpers::Constants::VARIABLE_TYPES['BOOLEAN'],
326322
user_id,
327323
attributes
328324
)
329325

330-
return variable_value
326+
variable_value
331327
end
332328

333329
def get_feature_variable_double(feature_flag_key, variable_key, user_id, attributes = nil)
@@ -344,12 +340,12 @@ def get_feature_variable_double(feature_flag_key, variable_key, user_id, attribu
344340
variable_value = get_feature_variable_for_type(
345341
feature_flag_key,
346342
variable_key,
347-
Optimizely::Helpers::Constants::VARIABLE_TYPES["DOUBLE"],
343+
Optimizely::Helpers::Constants::VARIABLE_TYPES['DOUBLE'],
348344
user_id,
349345
attributes
350346
)
351347

352-
return variable_value
348+
variable_value
353349
end
354350

355351
def get_feature_variable_integer(feature_flag_key, variable_key, user_id, attributes = nil)
@@ -366,12 +362,12 @@ def get_feature_variable_integer(feature_flag_key, variable_key, user_id, attrib
366362
variable_value = get_feature_variable_for_type(
367363
feature_flag_key,
368364
variable_key,
369-
Optimizely::Helpers::Constants::VARIABLE_TYPES["INTEGER"],
365+
Optimizely::Helpers::Constants::VARIABLE_TYPES['INTEGER'],
370366
user_id,
371367
attributes
372368
)
373369

374-
return variable_value
370+
variable_value
375371
end
376372

377373
private
@@ -391,17 +387,17 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
391387
# in case of variable type mismatch
392388

393389
unless feature_flag_key
394-
@logger.log(Logger::ERROR, "Feature flag key cannot be empty.")
390+
@logger.log(Logger::ERROR, 'Feature flag key cannot be empty.')
395391
return nil
396392
end
397393

398394
unless variable_key
399-
@logger.log(Logger::ERROR, "Variable key cannot be empty.")
395+
@logger.log(Logger::ERROR, 'Variable key cannot be empty.')
400396
return nil
401397
end
402398

403399
unless user_id
404-
@logger.log(Logger::ERROR, "User ID cannot be empty.")
400+
@logger.log(Logger::ERROR, 'User ID cannot be empty.')
405401
return nil
406402
end
407403

@@ -419,7 +415,7 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
419415
# Returns nil if type differs
420416
if variable['type'] != variable_type
421417
@logger.log(Logger::WARN,
422-
"Requested variable as type '#{variable_type}' but variable '#{variable_key}' is of type '#{variable['type']}'.")
418+
"Requested variable as type '#{variable_type}' but variable '#{variable_key}' is of type '#{variable['type']}'.")
423419
return nil
424420
else
425421
decision = @decision_service.get_variation_for_feature(feature_flag, user_id, attributes)
@@ -428,17 +424,17 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
428424
variation = decision['variation']
429425
variation_variable_usages = @config.variation_id_to_variable_usage_map[variation['id']]
430426
variable_id = variable['id']
431-
if variation_variable_usages and variation_variable_usages.key?(variable_id)
427+
if variation_variable_usages&.key?(variable_id)
432428
variable_value = variation_variable_usages[variable_id]['value']
433429
@logger.log(Logger::INFO,
434-
"Got variable value '#{variable_value}' for variable '#{variable_key}' of feature flag '#{feature_flag_key}'.")
430+
"Got variable value '#{variable_value}' for variable '#{variable_key}' of feature flag '#{feature_flag_key}'.")
435431
else
436432
@logger.log(Logger::DEBUG,
437-
"Variable '#{variable_key}' is not used in variation '#{variation['key']}'. Returning the default variable value '#{variable_value}'.")
433+
"Variable '#{variable_key}' is not used in variation '#{variation['key']}'. Returning the default variable value '#{variable_value}'.")
438434
end
439435
else
440436
@logger.log(Logger::INFO,
441-
"User '#{user_id}' was not bucketed into any variation for feature flag '#{feature_flag_key}'. Returning the default variable value '#{variable_value}'.")
437+
"User '#{user_id}' was not bucketed into any variation for feature flag '#{feature_flag_key}'. Returning the default variable value '#{variable_value}'.")
442438
end
443439
end
444440

@@ -483,13 +479,9 @@ def user_inputs_valid?(attributes = nil, event_tags = nil)
483479
#
484480
# Returns boolean True if inputs are valid. False otherwise.
485481

486-
if !attributes.nil? && !attributes_valid?(attributes)
487-
return false
488-
end
482+
return false if !attributes.nil? && !attributes_valid?(attributes)
489483

490-
if !event_tags.nil? && !event_tags_valid?(event_tags)
491-
return false
492-
end
484+
return false if !event_tags.nil? && !event_tags_valid?(event_tags)
493485

494486
true
495487
end
@@ -514,30 +506,29 @@ def event_tags_valid?(event_tags)
514506

515507
def validate_instantiation_options(datafile, skip_json_validation)
516508
unless skip_json_validation
517-
raise InvalidInputError.new('datafile') unless Helpers::Validator.datafile_valid?(datafile)
509+
raise InvalidInputError, 'datafile' unless Helpers::Validator.datafile_valid?(datafile)
518510
end
519511

520-
raise InvalidInputError.new('logger') unless Helpers::Validator.logger_valid?(@logger)
521-
raise InvalidInputError.new('error_handler') unless Helpers::Validator.error_handler_valid?(@error_handler)
522-
raise InvalidInputError.new('event_dispatcher') unless Helpers::Validator.event_dispatcher_valid?(@event_dispatcher)
512+
raise InvalidInputError, 'logger' unless Helpers::Validator.logger_valid?(@logger)
513+
raise InvalidInputError, 'error_handler' unless Helpers::Validator.error_handler_valid?(@error_handler)
514+
raise InvalidInputError, 'event_dispatcher' unless Helpers::Validator.event_dispatcher_valid?(@event_dispatcher)
523515
end
524516

525517
def send_impression(experiment, variation_key, user_id, attributes = nil)
526518
experiment_key = experiment['key']
527519
variation_id = @config.get_variation_id_from_key(experiment_key, variation_key)
528520
impression_event = @event_builder.create_impression_event(experiment, variation_id, user_id, attributes)
529521
@logger.log(Logger::INFO,
530-
'Dispatching impression event to URL %s with params %s.' % [impression_event.url,
531-
impression_event.params])
522+
"Dispatching impression event to URL #{impression_event.url} with params #{impression_event.params}.")
532523
begin
533524
@event_dispatcher.dispatch_event(impression_event)
534525
rescue => e
535526
@logger.log(Logger::ERROR, "Unable to dispatch impression event. Error: #{e}")
536527
end
537528
variation = @config.get_variation_from_id(experiment_key, variation_id)
538529
@notification_center.send_notifications(
539-
NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE],
540-
experiment,user_id, attributes, variation, impression_event
530+
NotificationCenter::NOTIFICATION_TYPES[:ACTIVATE],
531+
experiment, user_id, attributes, variation, impression_event
541532
)
542533
end
543534
end

lib/optimizely/audience.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# frozen_string_literal: true
2+
13
#
2-
# Copyright 2016, Optimizely and contributors
4+
# Copyright 2016-2017, Optimizely and contributors
35
#
46
# Licensed under the Apache License, Version 2.0 (the "License");
57
# you may not use this file except in compliance with the License.
@@ -43,7 +45,7 @@ def user_in_experiment?(config, experiment, attributes)
4345
audience_ids.each do |audience_id|
4446
audience = config.get_audience_from_id(audience_id)
4547
audience_conditions = audience['conditions']
46-
audience_conditions = JSON.load(audience_conditions)
48+
audience_conditions = JSON.parse(audience_conditions)
4749
return true if @condition_evaluator.evaluate(audience_conditions)
4850
end
4951

0 commit comments

Comments
 (0)