From fbabc670f6f42c0ef055af6b69465d98d4edcbe4 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 15:37:13 +0530 Subject: [PATCH 01/37] Add rubocop (style linter), run as part of default rake task --- Rakefile | 17 ++++++++++++++++- analytics-ruby.gemspec | 4 ++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 8c4fe80..cc4f987 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,22 @@ require 'rspec/core/rake_task' +default_tasks = [] + RSpec::Core::RakeTask.new(:spec) do |spec| spec.pattern = 'spec/**/*_spec.rb' end -task :default => :spec +default_tasks << :spec + +# Rubocop doesn't support < 2.1 +if RUBY_VERSION >= "2.1" + require 'rubocop/rake_task' + + RuboCop::RakeTask.new(:rubocop) do |task| + task.patterns = ['lib/**/*.rb','spec/**/*.rb',] + end + + default_tasks << :rubocop +end + +task :default => default_tasks diff --git a/analytics-ruby.gemspec b/analytics-ruby.gemspec index d00fe5a..0fc6467 100644 --- a/analytics-ruby.gemspec +++ b/analytics-ruby.gemspec @@ -22,4 +22,8 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rspec', '~> 3.0' spec.add_development_dependency 'tzinfo', '1.2.1' spec.add_development_dependency 'activesupport', '~> 4.1.11' + + if RUBY_VERSION >= "2.1" + spec.add_development_dependency 'rubocop', '~> 0.51.0' + end end From 2ed4fc55b7829248e253dbfea2b65334df600686 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 15:42:31 +0530 Subject: [PATCH 02/37] Generate rubocop_todo.yml --- .rubocop.yml | 1 + .rubocop_todo.yml | 356 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 357 insertions(+) create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..cc32da4 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1 @@ +inherit_from: .rubocop_todo.yml diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..c586513 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,356 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2017-11-22 15:40:32 +0530 using RuboCop version 0.51.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowAdjacentOneLineDefs, NumberOfEmptyLines. +Layout/EmptyLineBetweenDefs: + Exclude: + - 'lib/segment/analytics/client.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Layout/EmptyLines: + Exclude: + - 'spec/segment/analytics/client_spec.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment. +Layout/ExtraSpacing: + Exclude: + - 'lib/segment/analytics/client.rb' + - 'spec/spec_helper.rb' + +# Offense count: 28 +# Cop supports --auto-correct. +# Configuration parameters: SupportedStyles, IndentationWidth. +# SupportedStyles: special_inside_parentheses, consistent, align_braces +Layout/IndentHash: + EnforcedStyle: consistent + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: Width, IgnoredPatterns. +Layout/IndentationWidth: + Exclude: + - 'lib/segment/analytics/utils.rb' + +# Offense count: 16 +# Cop supports --auto-correct. +Layout/SpaceAfterComma: + Exclude: + - 'lib/segment/analytics/utils.rb' + - 'spec/segment/analytics/client_spec.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: AllowForAlignment. +Layout/SpaceAroundOperators: + Exclude: + - 'lib/segment/analytics/client.rb' + - 'lib/segment/analytics/request.rb' + - 'spec/spec_helper.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: final_newline, final_blank_line +Layout/TrailingBlankLines: + Exclude: + - 'lib/segment/analytics/response.rb' + +# Offense count: 1 +# Configuration parameters: AllowSafeAssignment. +Lint/AssignmentInCondition: + Exclude: + - 'lib/segment/analytics/client.rb' + +# Offense count: 1 +Lint/DuplicateMethods: + Exclude: + - 'lib/segment/analytics/request.rb' + +# Offense count: 1 +Lint/HandleExceptions: + Exclude: + - 'spec/spec_helper.rb' + +# Offense count: 1 +Lint/RescueException: + Exclude: + - 'lib/segment/analytics/request.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Lint/UnneededRequireStatement: + Exclude: + - 'lib/segment/analytics/client.rb' + +# Offense count: 9 +Metrics/AbcSize: + Max: 32 + +# Offense count: 13 +# Configuration parameters: CountComments, ExcludedMethods. +Metrics/BlockLength: + Max: 253 + +# Offense count: 3 +# Configuration parameters: CountComments. +Metrics/ClassLength: + Max: 255 + +# Offense count: 1 +Metrics/CyclomaticComplexity: + Max: 8 + +# Offense count: 71 +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Metrics/LineLength: + Max: 223 + +# Offense count: 10 +# Configuration parameters: CountComments. +Metrics/MethodLength: + Max: 29 + +# Offense count: 1 +Metrics/PerceivedComplexity: + Max: 8 + +# Offense count: 1 +# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist, MethodDefinitionMacros. +# NamePrefix: is_, has_, have_ +# NamePrefixBlacklist: is_, has_, have_ +# NameWhitelist: is_a? +# MethodDefinitionMacros: define_method, define_singleton_method +Naming/PredicateName: + Exclude: + - 'spec/**/*' + - 'lib/segment/analytics/worker.rb' + +# Offense count: 1 +Style/AsciiComments: + Exclude: + - 'spec/segment/analytics/worker_spec.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles, ProceduralMethods, FunctionalMethods, IgnoredMethods. +# SupportedStyles: line_count_based, semantic, braces_for_chaining +# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object +# FunctionalMethods: let, let!, subject, watch +# IgnoredMethods: lambda, proc, it +Style/BlockDelimiters: + Exclude: + - 'lib/segment/analytics/utils.rb' + - 'spec/segment/analytics/client_spec.rb' + +# Offense count: 14 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: braces, no_braces, context_dependent +Style/BracesAroundHashParameters: + Exclude: + - 'lib/segment/analytics/client.rb' + - 'spec/segment/analytics/client_spec.rb' + - 'spec/spec_helper.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/ColonMethodCall: + Exclude: + - 'spec/segment/analytics/worker_spec.rb' + +# Offense count: 3 +Style/DateTime: + Exclude: + - 'spec/segment/analytics/client_spec.rb' + +# Offense count: 7 +Style/Documentation: + Exclude: + - 'spec/**/*' + - 'test/**/*' + - 'lib/segment/analytics.rb' + - 'lib/segment/analytics/client.rb' + - 'lib/segment/analytics/logging.rb' + - 'lib/segment/analytics/request.rb' + - 'lib/segment/analytics/response.rb' + - 'lib/segment/analytics/utils.rb' + - 'lib/segment/analytics/worker.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +Style/EachWithObject: + Exclude: + - 'lib/segment/analytics/utils.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: format, sprintf, percent +Style/FormatString: + Exclude: + - 'lib/segment/analytics/utils.rb' + +# Offense count: 16 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: when_needed, always, never +Style/FrozenStringLiteralComment: + Enabled: false + +# Offense count: 1 +# Configuration parameters: MinBodyLength. +Style/GuardClause: + Exclude: + - 'lib/segment/analytics/client.rb' + +# Offense count: 167 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. +# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys +Style/HashSyntax: + Exclude: + - 'lib/segment/analytics/client.rb' + - 'lib/segment/analytics/defaults.rb' + - 'lib/segment/analytics/request.rb' + - 'spec/segment/analytics/client_spec.rb' + - 'spec/segment/analytics/worker_spec.rb' + - 'spec/segment/analytics_spec.rb' + - 'spec/spec_helper.rb' + +# Offense count: 13 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: require_parentheses, require_no_parentheses, require_no_parentheses_except_multiline +Style/MethodDefParentheses: + Exclude: + - 'lib/segment/analytics.rb' + - 'lib/segment/analytics/client.rb' + - 'lib/segment/analytics/logging.rb' + - 'lib/segment/analytics/utils.rb' + +# Offense count: 1 +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: module_function, extend_self +Style/ModuleFunction: + Exclude: + - 'lib/segment/analytics/utils.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/MultilineIfModifier: + Exclude: + - 'spec/segment/analytics/client_spec.rb' + +# Offense count: 12 +# Cop supports --auto-correct. +Style/MutableConstant: + Exclude: + - 'lib/segment/analytics/defaults.rb' + - 'lib/segment/analytics/utils.rb' + - 'lib/segment/analytics/version.rb' + - 'spec/spec_helper.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: Strict. +Style/NumericLiterals: + MinDigits: 6 + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: AutoCorrect, EnforcedStyle, SupportedStyles. +# SupportedStyles: predicate, comparison +Style/NumericPredicate: + Exclude: + - 'spec/**/*' + - 'lib/segment/analytics/utils.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +Style/ParallelAssignment: + Exclude: + - 'lib/segment/analytics/request.rb' + - 'spec/segment/analytics/worker_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: short, verbose +Style/PreferredHashMethods: + Exclude: + - 'lib/segment/analytics.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +Style/Proc: + Exclude: + - 'lib/segment/analytics/worker.rb' + - 'spec/segment/analytics/worker_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: ConvertCodeThatCanStartToReturnNil. +Style/SafeNavigation: + Exclude: + - 'lib/segment/analytics/client.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: AllowAsExpressionSeparator. +Style/Semicolon: + Exclude: + - 'lib/segment/analytics/utils.rb' + +# Offense count: 10 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: only_raise, only_fail, semantic +Style/SignalException: + Exclude: + - 'lib/segment/analytics/client.rb' + +# Offense count: 13 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline. +# SupportedStyles: single_quotes, double_quotes +Style/StringLiterals: + Exclude: + - 'lib/segment/analytics/client.rb' + - 'lib/segment/analytics/request.rb' + - 'lib/segment/analytics/utils.rb' + - 'spec/segment/analytics/client_spec.rb' + - 'spec/segment/analytics/worker_spec.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: single_quotes, double_quotes +Style/StringLiteralsInInterpolation: + Exclude: + - 'lib/segment/analytics/utils.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: MinSize, SupportedStyles. +# SupportedStyles: percent, brackets +Style/SymbolArray: + EnforcedStyle: brackets + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, IgnoreClassMethods, Whitelist. +# Whitelist: to_ary, to_a, to_c, to_enum, to_h, to_hash, to_i, to_int, to_io, to_open, to_path, to_proc, to_r, to_regexp, to_str, to_s, to_sym +Style/TrivialAccessors: + Exclude: + - 'lib/segment/analytics/logging.rb' From a8f0e6e5de1e73768778a244c68971c266c286b8 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 15:49:56 +0530 Subject: [PATCH 03/37] Autocorrect simple cops --- .rubocop_todo.yml | 21 --------------------- lib/segment/analytics/client.rb | 3 ++- spec/segment/analytics/client_spec.rb | 1 - spec/spec_helper.rb | 2 +- 4 files changed, 3 insertions(+), 24 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index c586513..50489ff 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,27 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: AllowAdjacentOneLineDefs, NumberOfEmptyLines. -Layout/EmptyLineBetweenDefs: - Exclude: - - 'lib/segment/analytics/client.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -Layout/EmptyLines: - Exclude: - - 'spec/segment/analytics/client_spec.rb' - -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment. -Layout/ExtraSpacing: - Exclude: - - 'lib/segment/analytics/client.rb' - - 'spec/spec_helper.rb' - # Offense count: 28 # Cop supports --auto-correct. # Configuration parameters: SupportedStyles, IndentationWidth. diff --git a/lib/segment/analytics/client.rb b/lib/segment/analytics/client.rb index 138f819..f43b033 100644 --- a/lib/segment/analytics/client.rb +++ b/lib/segment/analytics/client.rb @@ -248,6 +248,7 @@ def page(attrs) :type => 'page' }) end + # public: Records a screen view (for a mobile app) # # attrs - Hash @@ -328,7 +329,7 @@ def check_presence!(obj, name) # # context - Hash of call context def add_context(context) - context[:library] = { :name => "analytics-ruby", :version => Segment::Analytics::VERSION.to_s } + context[:library] = { :name => "analytics-ruby", :version => Segment::Analytics::VERSION.to_s } end # private: Checks that the write_key is properly initialized diff --git a/spec/segment/analytics/client_spec.rb b/spec/segment/analytics/client_spec.rb index 89cde66..73c18a9 100644 --- a/spec/segment/analytics/client_spec.rb +++ b/spec/segment/analytics/client_spec.rb @@ -99,7 +99,6 @@ class Analytics end end - describe '#identify' do it 'errors without any user id' do expect { client.identify({}) }.to raise_error(ArgumentError) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e67ca5d..65c2ef7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,7 +18,7 @@ class Analytics } } - IDENTIFY = { + IDENTIFY = { :traits => { :likes_animals => true, :instrument => 'Guitar', From 426f7fbd0aea4f4159e5f355a1d084c2e4e80684 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 15:55:34 +0530 Subject: [PATCH 04/37] Use consistent style for indenting hashes --- .rubocop_todo.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 50489ff..9736226 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,10 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 28 -# Cop supports --auto-correct. -# Configuration parameters: SupportedStyles, IndentationWidth. -# SupportedStyles: special_inside_parentheses, consistent, align_braces Layout/IndentHash: EnforcedStyle: consistent From 6ae23a3e75840310778d0bcc2355fccac59764bc Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 15:56:14 +0530 Subject: [PATCH 05/37] Auto-correct cops --- .rubocop_todo.yml | 31 --------------------------- lib/segment/analytics/request.rb | 2 +- lib/segment/analytics/response.rb | 1 - lib/segment/analytics/utils.rb | 8 +++---- spec/segment/analytics/client_spec.rb | 14 ++++++------ 5 files changed, 12 insertions(+), 44 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9736226..dc55fdd 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -9,37 +9,6 @@ Layout/IndentHash: EnforcedStyle: consistent -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: Width, IgnoredPatterns. -Layout/IndentationWidth: - Exclude: - - 'lib/segment/analytics/utils.rb' - -# Offense count: 16 -# Cop supports --auto-correct. -Layout/SpaceAfterComma: - Exclude: - - 'lib/segment/analytics/utils.rb' - - 'spec/segment/analytics/client_spec.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: AllowForAlignment. -Layout/SpaceAroundOperators: - Exclude: - - 'lib/segment/analytics/client.rb' - - 'lib/segment/analytics/request.rb' - - 'spec/spec_helper.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: final_newline, final_blank_line -Layout/TrailingBlankLines: - Exclude: - - 'lib/segment/analytics/response.rb' - # Offense count: 1 # Configuration parameters: AllowSafeAssignment. Lint/AssignmentInCondition: diff --git a/lib/segment/analytics/request.rb b/lib/segment/analytics/request.rb index 99df829..b6c0848 100644 --- a/lib/segment/analytics/request.rb +++ b/lib/segment/analytics/request.rb @@ -56,7 +56,7 @@ def post(write_key, batch) error = body["error"] end rescue Exception => e - unless (remaining_retries -=1).zero? + unless (remaining_retries -= 1).zero? sleep(backoff) retry end diff --git a/lib/segment/analytics/response.rb b/lib/segment/analytics/response.rb index ebee226..7306ac0 100644 --- a/lib/segment/analytics/response.rb +++ b/lib/segment/analytics/response.rb @@ -13,4 +13,3 @@ def initialize(status = 200, error = nil) end end end - diff --git a/lib/segment/analytics/utils.rb b/lib/segment/analytics/utils.rb index db96405..73234ff 100644 --- a/lib/segment/analytics/utils.rb +++ b/lib/segment/analytics/utils.rb @@ -8,7 +8,7 @@ module Utils # public: Return a new hash with keys converted from strings to symbols # def symbolize_keys(hash) - hash.inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo } + hash.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo } end # public: Convert hash keys from strings to symbols in place @@ -20,7 +20,7 @@ def symbolize_keys!(hash) # public: Return a new hash with keys as strings # def stringify_keys(hash) - hash.inject({}) { |memo, (k,v)| memo[k.to_s] = v; memo } + hash.inject({}) { |memo, (k, v)| memo[k.to_s] = v; memo } end # public: Returns a new hash with all the date values in the into iso8601 @@ -51,9 +51,9 @@ def uid def datetime_in_iso8601 datetime case datetime when Time - time_in_iso8601 datetime + time_in_iso8601 datetime when DateTime - time_in_iso8601 datetime.to_time + time_in_iso8601 datetime.to_time when Date date_in_iso8601 datetime else diff --git a/spec/segment/analytics/client_spec.rb b/spec/segment/analytics/client_spec.rb index 73c18a9..c658f77 100644 --- a/spec/segment/analytics/client_spec.rb +++ b/spec/segment/analytics/client_spec.rb @@ -43,7 +43,7 @@ class Analytics client.track({ :user_id => 'user', :event => 'Event', - :properties => [1,2,3] + :properties => [1, 2, 3] }) }.to raise_error(ArgumentError) end @@ -83,8 +83,8 @@ class Analytics :properties => { :time => Time.utc(2013), :time_with_zone => Time.zone.parse('2013-01-01'), - :date_time => DateTime.new(2013,1,1), - :date => Date.new(2013,1,1), + :date_time => DateTime.new(2013, 1, 1), + :date => Date.new(2013, 1, 1), :nottime => 'x' } }) @@ -124,8 +124,8 @@ class Analytics :traits => { :time => Time.utc(2013), :time_with_zone => Time.zone.parse('2013-01-01'), - :date_time => DateTime.new(2013,1,1), - :date => Date.new(2013,1,1), + :date_time => DateTime.new(2013, 1, 1), + :date => Date.new(2013, 1, 1), :nottime => 'x' } }) @@ -185,8 +185,8 @@ class Analytics :traits => { :time => Time.utc(2013), :time_with_zone => Time.zone.parse('2013-01-01'), - :date_time => DateTime.new(2013,1,1), - :date => Date.new(2013,1,1), + :date_time => DateTime.new(2013, 1, 1), + :date => Date.new(2013, 1, 1), :nottime => 'x' } }) From b62e415c9c1319821a097dc43bbdc79dbc19830a Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 15:59:49 +0530 Subject: [PATCH 06/37] Avoid redefining #stub via attr_accessor --- .rubocop_todo.yml | 5 ----- lib/segment/analytics/request.rb | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index dc55fdd..3af9c72 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -15,11 +15,6 @@ Lint/AssignmentInCondition: Exclude: - 'lib/segment/analytics/client.rb' -# Offense count: 1 -Lint/DuplicateMethods: - Exclude: - - 'lib/segment/analytics/request.rb' - # Offense count: 1 Lint/HandleExceptions: Exclude: diff --git a/lib/segment/analytics/request.rb b/lib/segment/analytics/request.rb index b6c0848..b5a11a9 100644 --- a/lib/segment/analytics/request.rb +++ b/lib/segment/analytics/request.rb @@ -71,7 +71,7 @@ def post(write_key, batch) end class << self - attr_accessor :stub + attr_writer :stub def stub @stub || ENV['STUB'] From 0053fd9c428a9e1475a6d03d1a99dc99b1958b37 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:02:43 +0530 Subject: [PATCH 07/37] Avoid assignment within condition --- .rubocop_todo.yml | 6 ------ lib/segment/analytics/client.rb | 8 ++++++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 3af9c72..4abc8e1 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -9,12 +9,6 @@ Layout/IndentHash: EnforcedStyle: consistent -# Offense count: 1 -# Configuration parameters: AllowSafeAssignment. -Lint/AssignmentInCondition: - Exclude: - - 'lib/segment/analytics/client.rb' - # Offense count: 1 Lint/HandleExceptions: Exclude: diff --git a/lib/segment/analytics/client.rb b/lib/segment/analytics/client.rb index f43b033..12f5153 100644 --- a/lib/segment/analytics/client.rb +++ b/lib/segment/analytics/client.rb @@ -307,11 +307,15 @@ def queued_messages def enqueue(action) # add our request id for tracing purposes action[:messageId] ||= uid - unless queue_full = @queue.length >= @max_queue_size + + if @queue.length < @max_queue_size ensure_worker_running @queue << action + + true + else + false # Queue is full end - !queue_full end # private: Ensures that a string is non-empty From dcc4be3dfcc6c8da20311903885ec456903c4328 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:05:46 +0530 Subject: [PATCH 08/37] Avoid suppressing exception in spec_helper.rb --- .rubocop_todo.yml | 5 ----- spec/spec_helper.rb | 6 +++--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4abc8e1..4c46763 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -9,11 +9,6 @@ Layout/IndentHash: EnforcedStyle: consistent -# Offense count: 1 -Lint/HandleExceptions: - Exclude: - - 'spec/spec_helper.rb' - # Offense count: 1 Lint/RescueException: Exclude: diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 65c2ef7..33cf320 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -97,11 +97,11 @@ def eventually(options = {}) loop do begin yield + return rescue RSpec::Expectations::ExpectationNotMetError => error + raise error if Time.now >= time_limit + sleep interval end - return if error.nil? - raise error if Time.now >= time_limit - sleep interval end end end From 730e2833a42deeb215f358fbcbcb43aa99b4f0fb Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:11:52 +0530 Subject: [PATCH 09/37] Auto-correct cops --- .rubocop_todo.yml | 8 -------- lib/segment/analytics/logging.rb | 4 +--- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4c46763..bb8c4b0 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -274,11 +274,3 @@ Style/StringLiteralsInInterpolation: # SupportedStyles: percent, brackets Style/SymbolArray: EnforcedStyle: brackets - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, IgnoreClassMethods, Whitelist. -# Whitelist: to_ary, to_a, to_c, to_enum, to_h, to_hash, to_i, to_int, to_io, to_open, to_path, to_proc, to_r, to_regexp, to_str, to_s, to_sym -Style/TrivialAccessors: - Exclude: - - 'lib/segment/analytics/logging.rb' diff --git a/lib/segment/analytics/logging.rb b/lib/segment/analytics/logging.rb index e7f5229..4b48ebf 100644 --- a/lib/segment/analytics/logging.rb +++ b/lib/segment/analytics/logging.rb @@ -14,9 +14,7 @@ def logger end end - def logger= logger - @logger = logger - end + attr_writer :logger end def self.included base From c9178ff5b8c36a0c9da368d58c4b97d5f5b7894f Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:12:47 +0530 Subject: [PATCH 10/37] Autocorrect symbol arrays --- .rubocop_todo.yml | 7 ------- spec/segment/analytics/client_spec.rb | 6 +++--- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index bb8c4b0..b26040c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -267,10 +267,3 @@ Style/StringLiterals: Style/StringLiteralsInInterpolation: Exclude: - 'lib/segment/analytics/utils.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: MinSize, SupportedStyles. -# SupportedStyles: percent, brackets -Style/SymbolArray: - EnforcedStyle: brackets diff --git a/spec/segment/analytics/client_spec.rb b/spec/segment/analytics/client_spec.rb index c658f77..86e57a4 100644 --- a/spec/segment/analytics/client_spec.rb +++ b/spec/segment/analytics/client_spec.rb @@ -264,7 +264,7 @@ class Analytics let(:data) { { :user_id => 1, :group_id => 2, :previous_id => 3, :anonymous_id => 4, :message_id => 5, :event => "coco barked", :name => "coco" } } it 'does not convert ids given as fixnums to strings' do - [:track, :screen, :page, :identify].each do |s| + %i[track screen page identify].each do |s| client.send(s, data) message = queue.pop(true) @@ -274,7 +274,7 @@ class Analytics end it 'converts message id to string' do - [:track, :screen, :page, :group, :identify, :alias].each do |s| + %i[track screen page group identify alias].each do |s| client.send(s, data) message = queue.pop(true) @@ -303,7 +303,7 @@ class Analytics end it 'sends integrations' do - [:track, :screen, :page, :group, :identify, :alias].each do |s| + %i[track screen page group identify alias].each do |s| client.send s, :integrations => { :All => true, :Salesforce => false }, :user_id => 1, :group_id => 2, :previous_id => 3, :anonymous_id => 4, :event => "coco barked", :name => "coco" message = queue.pop(true) expect(message[:integrations][:All]).to eq(true) From 28eacfe88e6fa6c97aa425f7712c19326e4eee64 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:15:19 +0530 Subject: [PATCH 11/37] Auto-correct string literals --- .rubocop_todo.yml | 12 ------------ lib/segment/analytics/client.rb | 2 +- lib/segment/analytics/request.rb | 2 +- lib/segment/analytics/utils.rb | 8 ++++---- spec/segment/analytics/client_spec.rb | 6 +++--- spec/segment/analytics/worker_spec.rb | 4 ++-- 6 files changed, 11 insertions(+), 23 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b26040c..344ba28 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -248,18 +248,6 @@ Style/SignalException: Exclude: - 'lib/segment/analytics/client.rb' -# Offense count: 13 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline. -# SupportedStyles: single_quotes, double_quotes -Style/StringLiterals: - Exclude: - - 'lib/segment/analytics/client.rb' - - 'lib/segment/analytics/request.rb' - - 'lib/segment/analytics/utils.rb' - - 'spec/segment/analytics/client_spec.rb' - - 'spec/segment/analytics/worker_spec.rb' - # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, SupportedStyles. diff --git a/lib/segment/analytics/client.rb b/lib/segment/analytics/client.rb index 12f5153..25a02e0 100644 --- a/lib/segment/analytics/client.rb +++ b/lib/segment/analytics/client.rb @@ -333,7 +333,7 @@ def check_presence!(obj, name) # # context - Hash of call context def add_context(context) - context[:library] = { :name => "analytics-ruby", :version => Segment::Analytics::VERSION.to_s } + context[:library] = { :name => 'analytics-ruby', :version => Segment::Analytics::VERSION.to_s } end # private: Checks that the write_key is properly initialized diff --git a/lib/segment/analytics/request.rb b/lib/segment/analytics/request.rb index b5a11a9..0a490f1 100644 --- a/lib/segment/analytics/request.rb +++ b/lib/segment/analytics/request.rb @@ -53,7 +53,7 @@ def post(write_key, batch) res = @http.request(request, payload) status = res.code.to_i body = JSON.parse(res.body) - error = body["error"] + error = body['error'] end rescue Exception => e unless (remaining_retries -= 1).zero? diff --git a/lib/segment/analytics/utils.rb b/lib/segment/analytics/utils.rb index 73234ff..fe5fbe6 100644 --- a/lib/segment/analytics/utils.rb +++ b/lib/segment/analytics/utils.rb @@ -42,10 +42,10 @@ def isoify_dates!(hash) # public: Returns a uid string # def uid - arr = SecureRandom.random_bytes(16).unpack("NnnnnN") + arr = SecureRandom.random_bytes(16).unpack('NnnnnN') arr[2] = (arr[2] & 0x0fff) | 0x4000 arr[3] = (arr[3] & 0x3fff) | 0x8000 - "%08x-%04x-%04x-%04x-%04x%08x" % arr + '%08x-%04x-%04x-%04x-%04x%08x' % arr end def datetime_in_iso8601 datetime @@ -63,14 +63,14 @@ def datetime_in_iso8601 datetime def time_in_iso8601 time, fraction_digits = 3 fraction = if fraction_digits > 0 - (".%06i" % time.usec)[0, fraction_digits + 1] + ('.%06i' % time.usec)[0, fraction_digits + 1] end "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{fraction}#{formatted_offset(time, true, 'Z')}" end def date_in_iso8601 date - date.strftime("%F") + date.strftime('%F') end def formatted_offset time, colon = true, alternate_utc_string = nil diff --git a/spec/segment/analytics/client_spec.rb b/spec/segment/analytics/client_spec.rb index 86e57a4..dd90eb0 100644 --- a/spec/segment/analytics/client_spec.rb +++ b/spec/segment/analytics/client_spec.rb @@ -49,7 +49,7 @@ class Analytics end it 'uses the timestamp given' do - time = Time.parse("1990-07-16 13:30:00.123 UTC") + time = Time.parse('1990-07-16 13:30:00.123 UTC') client.track({ :event => 'testing the timestamp', @@ -261,7 +261,7 @@ class Analytics context 'common' do check_property = proc { |msg, k, v| msg[k] && msg[k] == v } - let(:data) { { :user_id => 1, :group_id => 2, :previous_id => 3, :anonymous_id => 4, :message_id => 5, :event => "coco barked", :name => "coco" } } + let(:data) { { :user_id => 1, :group_id => 2, :previous_id => 3, :anonymous_id => 4, :message_id => 5, :event => 'coco barked', :name => 'coco' } } it 'does not convert ids given as fixnums to strings' do %i[track screen page identify].each do |s| @@ -304,7 +304,7 @@ class Analytics it 'sends integrations' do %i[track screen page group identify alias].each do |s| - client.send s, :integrations => { :All => true, :Salesforce => false }, :user_id => 1, :group_id => 2, :previous_id => 3, :anonymous_id => 4, :event => "coco barked", :name => "coco" + client.send s, :integrations => { :All => true, :Salesforce => false }, :user_id => 1, :group_id => 2, :previous_id => 3, :anonymous_id => 4, :event => 'coco barked', :name => 'coco' message = queue.pop(true) expect(message[:integrations][:All]).to eq(true) expect(message[:integrations][:Salesforce]).to eq(false) diff --git a/spec/segment/analytics/worker_spec.rb b/spec/segment/analytics/worker_spec.rb index 1accd22..c94c18a 100644 --- a/spec/segment/analytics/worker_spec.rb +++ b/spec/segment/analytics/worker_spec.rb @@ -3,7 +3,7 @@ module Segment class Analytics describe Worker do - describe "#init" do + describe '#init' do it 'accepts string keys' do queue = Queue.new worker = Segment::Analytics::Worker.new(queue, 'secret', 'batch_size' => 100) @@ -36,7 +36,7 @@ class Analytics end it 'executes the error handler, before the request phase ends, if the request is invalid' do - Segment::Analytics::Request.any_instance.stub(:post).and_return(Segment::Analytics::Response.new(400, "Some error")) + Segment::Analytics::Request.any_instance.stub(:post).and_return(Segment::Analytics::Response.new(400, 'Some error')) status = error = nil on_error = Proc.new do |yielded_status, yielded_error| From c5cb5e71e34d7743fb92bf1ed4d8be7607217627 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:16:16 +0530 Subject: [PATCH 12/37] Auto-correct quotes within interpolation --- .rubocop_todo.yml | 7 ------- lib/segment/analytics/utils.rb | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 344ba28..282a1c6 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -248,10 +248,3 @@ Style/SignalException: Exclude: - 'lib/segment/analytics/client.rb' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: single_quotes, double_quotes -Style/StringLiteralsInInterpolation: - Exclude: - - 'lib/segment/analytics/utils.rb' diff --git a/lib/segment/analytics/utils.rb b/lib/segment/analytics/utils.rb index fe5fbe6..f25c3e0 100644 --- a/lib/segment/analytics/utils.rb +++ b/lib/segment/analytics/utils.rb @@ -66,7 +66,7 @@ def time_in_iso8601 time, fraction_digits = 3 ('.%06i' % time.usec)[0, fraction_digits + 1] end - "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{fraction}#{formatted_offset(time, true, 'Z')}" + "#{time.strftime('%Y-%m-%dT%H:%M:%S')}#{fraction}#{formatted_offset(time, true, 'Z')}" end def date_in_iso8601 date From 359647bc60dd62a310a2c520e132e4d073cf95e3 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:17:39 +0530 Subject: [PATCH 13/37] Autocorrect raising exceptions --- .rubocop_todo.yml | 8 -------- lib/segment/analytics/client.rb | 20 ++++++++++---------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 282a1c6..02b80df 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -240,11 +240,3 @@ Style/Semicolon: Exclude: - 'lib/segment/analytics/utils.rb' -# Offense count: 10 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: only_raise, only_fail, semantic -Style/SignalException: - Exclude: - - 'lib/segment/analytics/client.rb' - diff --git a/lib/segment/analytics/client.rb b/lib/segment/analytics/client.rb index 25a02e0..878c3d9 100644 --- a/lib/segment/analytics/client.rb +++ b/lib/segment/analytics/client.rb @@ -66,10 +66,10 @@ def track attrs check_timestamp! timestamp if event.nil? || event.empty? - fail ArgumentError, 'Must supply event as a non-empty string' + raise ArgumentError, 'Must supply event as a non-empty string' end - fail ArgumentError, 'Properties must be a Hash' unless properties.is_a? Hash + raise ArgumentError, 'Properties must be a Hash' unless properties.is_a? Hash isoify_dates! properties add_context context @@ -110,7 +110,7 @@ def identify attrs check_timestamp! timestamp - fail ArgumentError, 'Must supply traits as a hash' unless traits.is_a? Hash + raise ArgumentError, 'Must supply traits as a hash' unless traits.is_a? Hash isoify_dates! traits add_context context @@ -185,7 +185,7 @@ def group(attrs) context = attrs[:context] || {} message_id = attrs[:message_id].to_s if attrs[:message_id] - fail ArgumentError, '.traits must be a hash' unless traits.is_a? Hash + raise ArgumentError, '.traits must be a hash' unless traits.is_a? Hash isoify_dates! traits check_presence! group_id, 'group_id' @@ -228,7 +228,7 @@ def page(attrs) context = attrs[:context] || {} message_id = attrs[:message_id].to_s if attrs[:message_id] - fail ArgumentError, '.properties must be a hash' unless properties.is_a? Hash + raise ArgumentError, '.properties must be a hash' unless properties.is_a? Hash isoify_dates! properties check_timestamp! timestamp @@ -271,7 +271,7 @@ def screen(attrs) context = attrs[:context] || {} message_id = attrs[:message_id].to_s if attrs[:message_id] - fail ArgumentError, '.properties must be a hash' unless properties.is_a? Hash + raise ArgumentError, '.properties must be a hash' unless properties.is_a? Hash isoify_dates! properties check_timestamp! timestamp @@ -325,7 +325,7 @@ def enqueue(action) # def check_presence!(obj, name) if obj.nil? || (obj.is_a?(String) && obj.empty?) - fail ArgumentError, "#{name} must be given" + raise ArgumentError, "#{name} must be given" end end @@ -338,12 +338,12 @@ def add_context(context) # private: Checks that the write_key is properly initialized def check_write_key! - fail ArgumentError, 'Write key must be initialized' if @write_key.nil? + raise ArgumentError, 'Write key must be initialized' if @write_key.nil? end # private: Checks the timstamp option to make sure it is a Time. def check_timestamp!(timestamp) - fail ArgumentError, 'Timestamp must be a Time' unless timestamp.is_a? Time + raise ArgumentError, 'Timestamp must be a Time' unless timestamp.is_a? Time end def event attrs @@ -360,7 +360,7 @@ def event attrs end def check_user_id! attrs - fail ArgumentError, 'Must supply either user_id or anonymous_id' unless attrs[:user_id] || attrs[:anonymous_id] + raise ArgumentError, 'Must supply either user_id or anonymous_id' unless attrs[:user_id] || attrs[:anonymous_id] end def ensure_worker_running From d70d5282d8b3feaf708820190a0e6cd7d8dd5424 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:19:17 +0530 Subject: [PATCH 14/37] Avoid using semi-colon as a separator --- .rubocop_todo.yml | 7 ------- lib/segment/analytics/utils.rb | 10 ++++++++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 02b80df..008f771 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -233,10 +233,3 @@ Style/SafeNavigation: Exclude: - 'lib/segment/analytics/client.rb' -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: AllowAsExpressionSeparator. -Style/Semicolon: - Exclude: - - 'lib/segment/analytics/utils.rb' - diff --git a/lib/segment/analytics/utils.rb b/lib/segment/analytics/utils.rb index f25c3e0..28ded97 100644 --- a/lib/segment/analytics/utils.rb +++ b/lib/segment/analytics/utils.rb @@ -8,7 +8,10 @@ module Utils # public: Return a new hash with keys converted from strings to symbols # def symbolize_keys(hash) - hash.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo } + hash.inject({}) { |memo, (k, v)| + memo[k.to_sym] = v + memo + } end # public: Convert hash keys from strings to symbols in place @@ -20,7 +23,10 @@ def symbolize_keys!(hash) # public: Return a new hash with keys as strings # def stringify_keys(hash) - hash.inject({}) { |memo, (k, v)| memo[k.to_s] = v; memo } + hash.inject({}) { |memo, (k, v)| + memo[k.to_s] = v + memo + } end # public: Returns a new hash with all the date values in the into iso8601 From deaa2eb1f2cb8ce8c67b64263c517b5f07a21b13 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:22:16 +0530 Subject: [PATCH 15/37] Set target ruby version --- .rubocop_todo.yml | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 008f771..f167854 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,6 +6,10 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. +AllCops: + # Rubocop doesn't support 1.9, so we'll use the minimum available + TargetRubyVersion: 2.1 + Layout/IndentHash: EnforcedStyle: consistent @@ -226,10 +230,3 @@ Style/Proc: - 'lib/segment/analytics/worker.rb' - 'spec/segment/analytics/worker_spec.rb' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: ConvertCodeThatCanStartToReturnNil. -Style/SafeNavigation: - Exclude: - - 'lib/segment/analytics/client.rb' - From bc9c013bcf9d2879aad8e12e148c6b5a8ccdcf34 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:25:28 +0530 Subject: [PATCH 16/37] Auto-correct Proc style --- .rubocop_todo.yml | 7 ------- lib/segment/analytics/worker.rb | 2 +- spec/segment/analytics/worker_spec.rb | 4 ++-- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f167854..15dafdd 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -223,10 +223,3 @@ Style/PreferredHashMethods: Exclude: - 'lib/segment/analytics.rb' -# Offense count: 3 -# Cop supports --auto-correct. -Style/Proc: - Exclude: - - 'lib/segment/analytics/worker.rb' - - 'spec/segment/analytics/worker_spec.rb' - diff --git a/lib/segment/analytics/worker.rb b/lib/segment/analytics/worker.rb index 9288c85..f983d72 100644 --- a/lib/segment/analytics/worker.rb +++ b/lib/segment/analytics/worker.rb @@ -25,7 +25,7 @@ def initialize(queue, write_key, options = {}) @queue = queue @write_key = write_key @batch_size = options[:batch_size] || Queue::BATCH_SIZE - @on_error = options[:on_error] || Proc.new { |status, error| } + @on_error = options[:on_error] || proc { |status, error| } @batch = [] @lock = Mutex.new end diff --git a/spec/segment/analytics/worker_spec.rb b/spec/segment/analytics/worker_spec.rb index c94c18a..8ab073a 100644 --- a/spec/segment/analytics/worker_spec.rb +++ b/spec/segment/analytics/worker_spec.rb @@ -39,7 +39,7 @@ class Analytics Segment::Analytics::Request.any_instance.stub(:post).and_return(Segment::Analytics::Response.new(400, 'Some error')) status = error = nil - on_error = Proc.new do |yielded_status, yielded_error| + on_error = proc do |yielded_status, yielded_error| sleep 0.2 # Make this take longer than thread spin-up (below) status, error = yielded_status, yielded_error end @@ -61,7 +61,7 @@ class Analytics end it 'does not call on_error if the request is good' do - on_error = Proc.new do |status, error| + on_error = proc do |status, error| puts "#{status}, #{error}" end From d0224fb3b7a608449d896a56303739dcb6a51540 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:26:53 +0530 Subject: [PATCH 17/37] Specify 'verbose' style for Hash#has_key? --- .rubocop_todo.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 15dafdd..4d63c9c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -215,11 +215,6 @@ Style/ParallelAssignment: - 'lib/segment/analytics/request.rb' - 'spec/segment/analytics/worker_spec.rb' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: short, verbose Style/PreferredHashMethods: - Exclude: - - 'lib/segment/analytics.rb' + EnforcedStyle: verbose From 970ea26b8c1db98e16b62186ee8560d96073e99c Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:31:04 +0530 Subject: [PATCH 18/37] Allow parallel assignment --- .rubocop_todo.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4d63c9c..45a66f6 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -208,12 +208,8 @@ Style/NumericPredicate: - 'spec/**/*' - 'lib/segment/analytics/utils.rb' -# Offense count: 2 -# Cop supports --auto-correct. Style/ParallelAssignment: - Exclude: - - 'lib/segment/analytics/request.rb' - - 'spec/segment/analytics/worker_spec.rb' + Enabled: false Style/PreferredHashMethods: EnforcedStyle: verbose From 6a3060492497b4d6c1bb61e0405b35da7d340b8c Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:32:46 +0530 Subject: [PATCH 19/37] Allow 6 digits without underscores --- .rubocop_todo.yml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 45a66f6..60f76f5 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -193,21 +193,9 @@ Style/MutableConstant: - 'lib/segment/analytics/version.rb' - 'spec/spec_helper.rb' -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: Strict. Style/NumericLiterals: MinDigits: 6 -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: AutoCorrect, EnforcedStyle, SupportedStyles. -# SupportedStyles: predicate, comparison -Style/NumericPredicate: - Exclude: - - 'spec/**/*' - - 'lib/segment/analytics/utils.rb' - Style/ParallelAssignment: Enabled: false From 16c7eb17d502d547a33e5effc33cdb66a0e724c6 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:35:45 +0530 Subject: [PATCH 20/37] Allow mutable constants This behaviour could've been relied on by users, not worth changing. --- .rubocop_todo.yml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 60f76f5..1a40ea4 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -184,14 +184,8 @@ Style/MultilineIfModifier: Exclude: - 'spec/segment/analytics/client_spec.rb' -# Offense count: 12 -# Cop supports --auto-correct. Style/MutableConstant: - Exclude: - - 'lib/segment/analytics/defaults.rb' - - 'lib/segment/analytics/utils.rb' - - 'lib/segment/analytics/version.rb' - - 'spec/spec_helper.rb' + Enabled: false Style/NumericLiterals: MinDigits: 6 From 73948e4cf2d7c3f6b9ae48953fd02b2b47daf758 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:38:34 +0530 Subject: [PATCH 21/37] Avoid multi-line if modifier usage --- .rubocop_todo.yml | 10 ++-------- spec/segment/analytics/client_spec.rb | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1a40ea4..1220505 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -31,12 +31,12 @@ Metrics/AbcSize: # Offense count: 13 # Configuration parameters: CountComments, ExcludedMethods. Metrics/BlockLength: - Max: 253 + Max: 255 # Offense count: 3 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 255 + Max: 257 # Offense count: 1 Metrics/CyclomaticComplexity: @@ -178,12 +178,6 @@ Style/ModuleFunction: Exclude: - 'lib/segment/analytics/utils.rb' -# Offense count: 1 -# Cop supports --auto-correct. -Style/MultilineIfModifier: - Exclude: - - 'spec/segment/analytics/client_spec.rb' - Style/MutableConstant: Enabled: false diff --git a/spec/segment/analytics/client_spec.rb b/spec/segment/analytics/client_spec.rb index dd90eb0..1b7c2bd 100644 --- a/spec/segment/analytics/client_spec.rb +++ b/spec/segment/analytics/client_spec.rb @@ -245,17 +245,19 @@ class Analytics expect(client_with_worker.queued_messages).to eq(0) end - it 'completes when the process forks' do - client_with_worker.identify Queued::IDENTIFY + unless defined? JRUBY_VERSION + it 'completes when the process forks' do + client_with_worker.identify Queued::IDENTIFY - Process.fork do - client_with_worker.track Queued::TRACK - client_with_worker.flush - expect(client_with_worker.queued_messages).to eq(0) - end + Process.fork do + client_with_worker.track Queued::TRACK + client_with_worker.flush + expect(client_with_worker.queued_messages).to eq(0) + end - Process.wait - end unless defined? JRUBY_VERSION + Process.wait + end + end end context 'common' do From 666f502a2eaf255c5ef9ac5a874503fc229e0b20 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:41:08 +0530 Subject: [PATCH 22/37] Use parentheses for all method definitions --- .rubocop_todo.yml | 11 ----------- lib/segment/analytics.rb | 4 ++-- lib/segment/analytics/client.rb | 10 +++++----- lib/segment/analytics/logging.rb | 2 +- lib/segment/analytics/utils.rb | 8 ++++---- 5 files changed, 12 insertions(+), 23 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1220505..9e99389 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -160,17 +160,6 @@ Style/HashSyntax: - 'spec/segment/analytics_spec.rb' - 'spec/spec_helper.rb' -# Offense count: 13 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: require_parentheses, require_no_parentheses, require_no_parentheses_except_multiline -Style/MethodDefParentheses: - Exclude: - - 'lib/segment/analytics.rb' - - 'lib/segment/analytics/client.rb' - - 'lib/segment/analytics/logging.rb' - - 'lib/segment/analytics/utils.rb' - # Offense count: 1 # Configuration parameters: EnforcedStyle, SupportedStyles. # SupportedStyles: module_function, extend_self diff --git a/lib/segment/analytics.rb b/lib/segment/analytics.rb index da0b400..0105c16 100644 --- a/lib/segment/analytics.rb +++ b/lib/segment/analytics.rb @@ -9,12 +9,12 @@ module Segment class Analytics - def initialize options = {} + def initialize(options = {}) Request.stub = options[:stub] if options.has_key?(:stub) @client = Segment::Analytics::Client.new options end - def method_missing message, *args, &block + def method_missing(message, *args, &block) if @client.respond_to? message @client.send message, *args, &block else diff --git a/lib/segment/analytics/client.rb b/lib/segment/analytics/client.rb index 878c3d9..c0f0d67 100644 --- a/lib/segment/analytics/client.rb +++ b/lib/segment/analytics/client.rb @@ -15,7 +15,7 @@ class Client # :write_key - String of your project's write_key # :max_queue_size - Fixnum of the max calls to remain queued (optional) # :on_error - Proc which handles error calls from the API - def initialize attrs = {} + def initialize(attrs = {}) symbolize_keys! attrs @queue = Queue.new @@ -53,7 +53,7 @@ def flush # :timestamp - Time of when the event occurred. (optional) # :user_id - String of the user id. # :message_id - String of the message id that uniquely identified a message across the API. (optional) - def track attrs + def track(attrs) symbolize_keys! attrs check_user_id! attrs @@ -99,7 +99,7 @@ def track attrs # :traits - Hash of user traits. (optional) # :user_id - String of the user id # :message_id - String of the message id that uniquely identified a message across the API. (optional) - def identify attrs + def identify(attrs) symbolize_keys! attrs check_user_id! attrs @@ -346,7 +346,7 @@ def check_timestamp!(timestamp) raise ArgumentError, 'Timestamp must be a Time' unless timestamp.is_a? Time end - def event attrs + def event(attrs) symbolize_keys! attrs { @@ -359,7 +359,7 @@ def event attrs } end - def check_user_id! attrs + def check_user_id!(attrs) raise ArgumentError, 'Must supply either user_id or anonymous_id' unless attrs[:user_id] || attrs[:anonymous_id] end diff --git a/lib/segment/analytics/logging.rb b/lib/segment/analytics/logging.rb index 4b48ebf..20b1a7f 100644 --- a/lib/segment/analytics/logging.rb +++ b/lib/segment/analytics/logging.rb @@ -17,7 +17,7 @@ def logger attr_writer :logger end - def self.included base + def self.included(base) class << base def logger Logging.logger diff --git a/lib/segment/analytics/utils.rb b/lib/segment/analytics/utils.rb index 28ded97..5ed2a44 100644 --- a/lib/segment/analytics/utils.rb +++ b/lib/segment/analytics/utils.rb @@ -54,7 +54,7 @@ def uid '%08x-%04x-%04x-%04x-%04x%08x' % arr end - def datetime_in_iso8601 datetime + def datetime_in_iso8601(datetime) case datetime when Time time_in_iso8601 datetime @@ -67,7 +67,7 @@ def datetime_in_iso8601 datetime end end - def time_in_iso8601 time, fraction_digits = 3 + def time_in_iso8601(time, fraction_digits = 3) fraction = if fraction_digits > 0 ('.%06i' % time.usec)[0, fraction_digits + 1] end @@ -75,11 +75,11 @@ def time_in_iso8601 time, fraction_digits = 3 "#{time.strftime('%Y-%m-%dT%H:%M:%S')}#{fraction}#{formatted_offset(time, true, 'Z')}" end - def date_in_iso8601 date + def date_in_iso8601(date) date.strftime('%F') end - def formatted_offset time, colon = true, alternate_utc_string = nil + def formatted_offset(time, colon = true, alternate_utc_string = nil) time.utc? && alternate_utc_string || seconds_to_utc_offset(time.utc_offset, colon) end From 60713a636adcfb579f524ef09eeb881cb98b8e17 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:45:09 +0530 Subject: [PATCH 23/37] Allow 1.9 hash syntax in specs --- .rubocop_todo.yml | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9e99389..c78c4cc 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -146,19 +146,10 @@ Style/GuardClause: Exclude: - 'lib/segment/analytics/client.rb' -# Offense count: 167 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. -# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys Style/HashSyntax: + EnforcedStyle: hash_rockets Exclude: - - 'lib/segment/analytics/client.rb' - - 'lib/segment/analytics/defaults.rb' - - 'lib/segment/analytics/request.rb' - - 'spec/segment/analytics/client_spec.rb' - - 'spec/segment/analytics/worker_spec.rb' - - 'spec/segment/analytics_spec.rb' - - 'spec/spec_helper.rb' + - 'spec/**/*.rb' # Offense count: 1 # Configuration parameters: EnforcedStyle, SupportedStyles. From dfa3f8c2d4971c890b88ca40574b3088c0764954 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:47:29 +0530 Subject: [PATCH 24/37] Allow one-liners to be wrapped in conditionals --- .rubocop_todo.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index c78c4cc..542b185 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -140,11 +140,10 @@ Style/FormatString: Style/FrozenStringLiteralComment: Enabled: false -# Offense count: 1 -# Configuration parameters: MinBodyLength. +# Allow one-liner functions to be wrapped in conditionals rather +# than forcing a guard clause Style/GuardClause: - Exclude: - - 'lib/segment/analytics/client.rb' + MinBodyLength: 2 Style/HashSyntax: EnforcedStyle: hash_rockets From bf43e1dd55d6c4f20db28f0daa045128b8db23a8 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:48:39 +0530 Subject: [PATCH 25/37] Allow % for formatting --- .rubocop_todo.yml | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 542b185..a7a7c9f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -125,20 +125,8 @@ Style/EachWithObject: Exclude: - 'lib/segment/analytics/utils.rb' -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: format, sprintf, percent Style/FormatString: - Exclude: - - 'lib/segment/analytics/utils.rb' - -# Offense count: 16 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: when_needed, always, never -Style/FrozenStringLiteralComment: - Enabled: false + EnforcedStyle: percent # Allow one-liner functions to be wrapped in conditionals rather # than forcing a guard clause From 0d2b55f89b84dc31cf2171a224e121ef9ee12967 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 16:54:46 +0530 Subject: [PATCH 26/37] Use each_with_object instead of inject --- .rubocop_todo.yml | 6 ------ lib/segment/analytics/utils.rb | 9 +++------ 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a7a7c9f..96e41b1 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -119,12 +119,6 @@ Style/Documentation: - 'lib/segment/analytics/utils.rb' - 'lib/segment/analytics/worker.rb' -# Offense count: 3 -# Cop supports --auto-correct. -Style/EachWithObject: - Exclude: - - 'lib/segment/analytics/utils.rb' - Style/FormatString: EnforcedStyle: percent diff --git a/lib/segment/analytics/utils.rb b/lib/segment/analytics/utils.rb index 5ed2a44..a6d2338 100644 --- a/lib/segment/analytics/utils.rb +++ b/lib/segment/analytics/utils.rb @@ -8,9 +8,8 @@ module Utils # public: Return a new hash with keys converted from strings to symbols # def symbolize_keys(hash) - hash.inject({}) { |memo, (k, v)| + hash.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v - memo } end @@ -23,9 +22,8 @@ def symbolize_keys!(hash) # public: Return a new hash with keys as strings # def stringify_keys(hash) - hash.inject({}) { |memo, (k, v)| + hash.each_with_object({}) { |(k, v), memo| memo[k.to_s] = v - memo } end @@ -33,9 +31,8 @@ def stringify_keys(hash) # strings # def isoify_dates(hash) - hash.inject({}) { |memo, (k, v)| + hash.each_with_object({}) { |(k, v), memo| memo[k] = datetime_in_iso8601(v) - memo } end From 8e9ceedabe33bccd4d9157511a23680744bef3b8 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 17:08:48 +0530 Subject: [PATCH 27/37] Allow DateTime in specs --- .rubocop_todo.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 96e41b1..8b103cd 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -101,10 +101,9 @@ Style/ColonMethodCall: Exclude: - 'spec/segment/analytics/worker_spec.rb' -# Offense count: 3 Style/DateTime: Exclude: - - 'spec/segment/analytics/client_spec.rb' + - 'spec/**/*.rb' # Offense count: 7 Style/Documentation: From 7fdd3656a2f9e0fc3b890673739955731b36bfe3 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 17:10:03 +0530 Subject: [PATCH 28/37] Remove colon method call --- .rubocop_todo.yml | 6 ------ spec/segment/analytics/worker_spec.rb | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8b103cd..8ee9977 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -95,12 +95,6 @@ Style/BracesAroundHashParameters: - 'spec/segment/analytics/client_spec.rb' - 'spec/spec_helper.rb' -# Offense count: 1 -# Cop supports --auto-correct. -Style/ColonMethodCall: - Exclude: - - 'spec/segment/analytics/worker_spec.rb' - Style/DateTime: Exclude: - 'spec/**/*.rb' diff --git a/spec/segment/analytics/worker_spec.rb b/spec/segment/analytics/worker_spec.rb index 8ab073a..b4d2f00 100644 --- a/spec/segment/analytics/worker_spec.rb +++ b/spec/segment/analytics/worker_spec.rb @@ -53,7 +53,7 @@ class Analytics sleep 0.1 # First give thread time to spin-up. sleep 0.01 while worker.is_requesting? - Segment::Analytics::Request::any_instance.unstub(:post) + Segment::Analytics::Request.any_instance.unstub(:post) expect(queue).to be_empty expect(status).to eq(400) From 3f7f72089e83d45bee0d07b2efff34ec0e11253b Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Wed, 22 Nov 2017 17:16:56 +0530 Subject: [PATCH 29/37] Allow bracket symbol arrays --- .rubocop_todo.yml | 3 +++ spec/segment/analytics/client_spec.rb | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8ee9977..8912bdc 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -144,3 +144,6 @@ Style/ParallelAssignment: Style/PreferredHashMethods: EnforcedStyle: verbose +# Ruby 1.9 doesn't support percent-styled symbol arrays +Style/SymbolArray: + EnforcedStyle: brackets diff --git a/spec/segment/analytics/client_spec.rb b/spec/segment/analytics/client_spec.rb index 1b7c2bd..ebc9a0a 100644 --- a/spec/segment/analytics/client_spec.rb +++ b/spec/segment/analytics/client_spec.rb @@ -266,7 +266,7 @@ class Analytics let(:data) { { :user_id => 1, :group_id => 2, :previous_id => 3, :anonymous_id => 4, :message_id => 5, :event => 'coco barked', :name => 'coco' } } it 'does not convert ids given as fixnums to strings' do - %i[track screen page identify].each do |s| + [:track, :screen, :page, :identify].each do |s| client.send(s, data) message = queue.pop(true) @@ -276,7 +276,7 @@ class Analytics end it 'converts message id to string' do - %i[track screen page group identify alias].each do |s| + [:track, :screen, :page, :group, :identify, :alias].each do |s| client.send(s, data) message = queue.pop(true) @@ -305,7 +305,7 @@ class Analytics end it 'sends integrations' do - %i[track screen page group identify alias].each do |s| + [:track, :screen, :page, :group, :identify, :alias].each do |s| client.send s, :integrations => { :All => true, :Salesforce => false }, :user_id => 1, :group_id => 2, :previous_id => 3, :anonymous_id => 4, :event => 'coco barked', :name => 'coco' message = queue.pop(true) expect(message[:integrations][:All]).to eq(true) From 6654ff06d9b935e11a649f0d4602554a37b0d2f7 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Thu, 23 Nov 2017 15:09:10 +0530 Subject: [PATCH 30/37] Disable doc checks --- .rubocop_todo.yml | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8912bdc..ba1938a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -99,18 +99,8 @@ Style/DateTime: Exclude: - 'spec/**/*.rb' -# Offense count: 7 Style/Documentation: - Exclude: - - 'spec/**/*' - - 'test/**/*' - - 'lib/segment/analytics.rb' - - 'lib/segment/analytics/client.rb' - - 'lib/segment/analytics/logging.rb' - - 'lib/segment/analytics/request.rb' - - 'lib/segment/analytics/response.rb' - - 'lib/segment/analytics/utils.rb' - - 'lib/segment/analytics/worker.rb' + Enabled: false Style/FormatString: EnforcedStyle: percent From a969429c5ff610b4db6b713391063e1cdc8980c3 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Thu, 23 Nov 2017 15:15:52 +0530 Subject: [PATCH 31/37] Allow all block delimiters in specs --- .rubocop_todo.yml | 10 +--------- lib/segment/analytics/utils.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ba1938a..448740a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -73,17 +73,9 @@ Style/AsciiComments: Exclude: - 'spec/segment/analytics/worker_spec.rb' -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, ProceduralMethods, FunctionalMethods, IgnoredMethods. -# SupportedStyles: line_count_based, semantic, braces_for_chaining -# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object -# FunctionalMethods: let, let!, subject, watch -# IgnoredMethods: lambda, proc, it Style/BlockDelimiters: Exclude: - - 'lib/segment/analytics/utils.rb' - - 'spec/segment/analytics/client_spec.rb' + - 'spec/**/*' # Offense count: 14 # Cop supports --auto-correct. diff --git a/lib/segment/analytics/utils.rb b/lib/segment/analytics/utils.rb index a6d2338..60bfa52 100644 --- a/lib/segment/analytics/utils.rb +++ b/lib/segment/analytics/utils.rb @@ -8,9 +8,9 @@ module Utils # public: Return a new hash with keys converted from strings to symbols # def symbolize_keys(hash) - hash.each_with_object({}) { |(k, v), memo| + hash.each_with_object({}) do |(k, v), memo| memo[k.to_sym] = v - } + end end # public: Convert hash keys from strings to symbols in place @@ -22,18 +22,18 @@ def symbolize_keys!(hash) # public: Return a new hash with keys as strings # def stringify_keys(hash) - hash.each_with_object({}) { |(k, v), memo| + hash.each_with_object({}) do |(k, v), memo| memo[k.to_s] = v - } + end end # public: Returns a new hash with all the date values in the into iso8601 # strings # def isoify_dates(hash) - hash.each_with_object({}) { |(k, v), memo| + hash.each_with_object({}) do |(k, v), memo| memo[k] = datetime_in_iso8601(v) - } + end end # public: Converts all the date values in the into iso8601 strings in place From c59fbbb65e389082c19ee2ed6bc78d155d0e261f Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Thu, 23 Nov 2017 15:16:49 +0530 Subject: [PATCH 32/37] Remove non-ascii quote in comment --- .rubocop_todo.yml | 5 ----- spec/segment/analytics/worker_spec.rb | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 448740a..f61447e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -68,11 +68,6 @@ Naming/PredicateName: - 'spec/**/*' - 'lib/segment/analytics/worker.rb' -# Offense count: 1 -Style/AsciiComments: - Exclude: - - 'spec/segment/analytics/worker_spec.rb' - Style/BlockDelimiters: Exclude: - 'spec/**/*' diff --git a/spec/segment/analytics/worker_spec.rb b/spec/segment/analytics/worker_spec.rb index b4d2f00..3abd4d8 100644 --- a/spec/segment/analytics/worker_spec.rb +++ b/spec/segment/analytics/worker_spec.rb @@ -48,7 +48,7 @@ class Analytics queue << {} worker = Segment::Analytics::Worker.new queue, 'secret', :on_error => on_error - # This is to ensure that Client#flush doesn’t finish before calling the error handler. + # This is to ensure that Client#flush doesn't finish before calling the error handler. Thread.new { worker.run } sleep 0.1 # First give thread time to spin-up. sleep 0.01 while worker.is_requesting? From 3ae65edb9e050cc9a91c9517a796649e94cb7aa8 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Thu, 23 Nov 2017 15:22:54 +0530 Subject: [PATCH 33/37] Allow is_requesting? as an exception for predicate names --- .rubocop_todo.yml | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f61447e..9bbd294 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -18,12 +18,6 @@ Lint/RescueException: Exclude: - 'lib/segment/analytics/request.rb' -# Offense count: 1 -# Cop supports --auto-correct. -Lint/UnneededRequireStatement: - Exclude: - - 'lib/segment/analytics/client.rb' - # Offense count: 9 Metrics/AbcSize: Max: 32 @@ -57,16 +51,9 @@ Metrics/MethodLength: Metrics/PerceivedComplexity: Max: 8 -# Offense count: 1 -# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist, MethodDefinitionMacros. -# NamePrefix: is_, has_, have_ -# NamePrefixBlacklist: is_, has_, have_ -# NameWhitelist: is_a? -# MethodDefinitionMacros: define_method, define_singleton_method Naming/PredicateName: - Exclude: - - 'spec/**/*' - - 'lib/segment/analytics/worker.rb' + NameWhitelist: + - is_requesting? # Can't be renamed, backwards compatibility Style/BlockDelimiters: Exclude: From de64b489fd2519a5137b6eb0f8b9566c8ffac4cc Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Thu, 23 Nov 2017 15:24:52 +0530 Subject: [PATCH 34/37] Disable BracesAroundHashParameters check --- .rubocop_todo.yml | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9bbd294..2d79999 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -59,15 +59,8 @@ Style/BlockDelimiters: Exclude: - 'spec/**/*' -# Offense count: 14 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: braces, no_braces, context_dependent Style/BracesAroundHashParameters: - Exclude: - - 'lib/segment/analytics/client.rb' - - 'spec/segment/analytics/client_spec.rb' - - 'spec/spec_helper.rb' + Enabled: false Style/DateTime: Exclude: From 82fd5efb2393478198a6ffab26fc9a387aa9a11f Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Thu, 23 Nov 2017 15:28:27 +0530 Subject: [PATCH 35/37] Move legit items from .rubucop_todo.yml to .rubocop.yml --- .rubocop.yml | 57 +++++++++++++++++++++++++++++++++++++++++++ .rubocop_todo.yml | 61 ----------------------------------------------- 2 files changed, 57 insertions(+), 61 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index cc32da4..c8a06f9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1 +1,58 @@ inherit_from: .rubocop_todo.yml + +AllCops: + # Rubocop doesn't support 1.9, so we'll use the minimum available + TargetRubyVersion: 2.1 + +Layout/IndentHash: + EnforcedStyle: consistent + +Naming/PredicateName: + NameWhitelist: + - is_requesting? # Can't be renamed, backwards compatibility + +Style/BlockDelimiters: + Exclude: + - 'spec/**/*' + +Style/BracesAroundHashParameters: + Enabled: false + +Style/DateTime: + Exclude: + - 'spec/**/*.rb' + +Style/Documentation: + Enabled: false + +Style/FormatString: + EnforcedStyle: percent + +# Allow one-liner functions to be wrapped in conditionals rather +# than forcing a guard clause +Style/GuardClause: + MinBodyLength: 2 + +Style/HashSyntax: + EnforcedStyle: hash_rockets + Exclude: + - 'spec/**/*.rb' + +Style/ModuleFunction: + Enabled: false + +Style/MutableConstant: + Enabled: false + +Style/NumericLiterals: + MinDigits: 6 + +Style/ParallelAssignment: + Enabled: false + +Style/PreferredHashMethods: + EnforcedStyle: verbose + +# Ruby 1.9 doesn't support percent-styled symbol arrays +Style/SymbolArray: + EnforcedStyle: brackets diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 2d79999..4ad1321 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,13 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -AllCops: - # Rubocop doesn't support 1.9, so we'll use the minimum available - TargetRubyVersion: 2.1 - -Layout/IndentHash: - EnforcedStyle: consistent - # Offense count: 1 Lint/RescueException: Exclude: @@ -50,57 +43,3 @@ Metrics/MethodLength: # Offense count: 1 Metrics/PerceivedComplexity: Max: 8 - -Naming/PredicateName: - NameWhitelist: - - is_requesting? # Can't be renamed, backwards compatibility - -Style/BlockDelimiters: - Exclude: - - 'spec/**/*' - -Style/BracesAroundHashParameters: - Enabled: false - -Style/DateTime: - Exclude: - - 'spec/**/*.rb' - -Style/Documentation: - Enabled: false - -Style/FormatString: - EnforcedStyle: percent - -# Allow one-liner functions to be wrapped in conditionals rather -# than forcing a guard clause -Style/GuardClause: - MinBodyLength: 2 - -Style/HashSyntax: - EnforcedStyle: hash_rockets - Exclude: - - 'spec/**/*.rb' - -# Offense count: 1 -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: module_function, extend_self -Style/ModuleFunction: - Exclude: - - 'lib/segment/analytics/utils.rb' - -Style/MutableConstant: - Enabled: false - -Style/NumericLiterals: - MinDigits: 6 - -Style/ParallelAssignment: - Enabled: false - -Style/PreferredHashMethods: - EnforcedStyle: verbose - -# Ruby 1.9 doesn't support percent-styled symbol arrays -Style/SymbolArray: - EnforcedStyle: brackets From 1f1229ae9e97c7c4610d3c4ba04731aec5188a91 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Thu, 23 Nov 2017 15:34:50 +0530 Subject: [PATCH 36/37] Disable metrics check for specs --- .rubocop.yml | 28 ++++++++++++++++++++++++++++ .rubocop_todo.yml | 15 +++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c8a06f9..dc74787 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -7,6 +7,34 @@ AllCops: Layout/IndentHash: EnforcedStyle: consistent +Metrics/AbcSize: + Exclude: + - "spec/**/*.rb" + +Metrics/BlockLength: + Exclude: + - "spec/**/*.rb" + +Metrics/ClassLength: + Exclude: + - "spec/**/*.rb" + +Metrics/CyclomaticComplexity: + Exclude: + - "spec/**/*.rb" + +Metrics/LineLength: + Exclude: + - "spec/**/*.rb" + +Metrics/MethodLength: + Exclude: + - "spec/**/*.rb" + +Metrics/PerceivedComplexity: + Exclude: + - "spec/**/*.rb" + Naming/PredicateName: NameWhitelist: - is_requesting? # Can't be renamed, backwards compatibility diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4ad1321..8057bd2 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2017-11-22 15:40:32 +0530 using RuboCop version 0.51.0. +# on 2017-11-23 15:34:00 +0530 using RuboCop version 0.51.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -15,27 +15,22 @@ Lint/RescueException: Metrics/AbcSize: Max: 32 -# Offense count: 13 -# Configuration parameters: CountComments, ExcludedMethods. -Metrics/BlockLength: - Max: 255 - -# Offense count: 3 +# Offense count: 1 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 257 + Max: 223 # Offense count: 1 Metrics/CyclomaticComplexity: Max: 8 -# Offense count: 71 +# Offense count: 36 # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https Metrics/LineLength: Max: 223 -# Offense count: 10 +# Offense count: 9 # Configuration parameters: CountComments. Metrics/MethodLength: Max: 29 From 3ae4855b907ae7ea9eb2a2acdfc766afe0c7b3e0 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Thu, 23 Nov 2017 23:09:21 +0530 Subject: [PATCH 37/37] Upload coverage stats to codecov in specs --- analytics-ruby.gemspec | 2 ++ spec/spec_helper.rb | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/analytics-ruby.gemspec b/analytics-ruby.gemspec index 0fc6467..ff9636f 100644 --- a/analytics-ruby.gemspec +++ b/analytics-ruby.gemspec @@ -26,4 +26,6 @@ Gem::Specification.new do |spec| if RUBY_VERSION >= "2.1" spec.add_development_dependency 'rubocop', '~> 0.51.0' end + + spec.add_development_dependency 'codecov', '~> 0.1.4' end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 33cf320..8d8bdb8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,9 @@ +# https://github.com/codecov/codecov-ruby#usage +require 'simplecov' +SimpleCov.start +require 'codecov' +SimpleCov.formatter = SimpleCov::Formatter::Codecov + require 'segment/analytics' require 'active_support/time'