Skip to content

Commit 906f30c

Browse files
committed
Fix rubocop config to include .rb files and resolve all offenses
Add **/*.rb to rubocop Include list so all source files are inspected. Fix offenses across the codebase: use Dir.home, safe navigation, anonymous forwarding, respond_to_missing?, const_get instead of instance_eval, rename exception param, use $stdout, sort redundant Dir.glob, and various layout/style fixes. Exclude Metrics/BlockLength and Style/OpenStructUse for spec files.
1 parent 90e6ae1 commit 906f30c

21 files changed

Lines changed: 84 additions & 70 deletions

.rubocop.yml

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
AllCops:
2-
TargetRubyVersion: 2.7
2+
TargetRubyVersion: 3.4
33

44
# Include gemspec and Rakefile
55
Include:
6-
- '**/*.gemspec'
7-
- '**/*.podspec'
8-
- '**/*.jbuilder'
9-
- '**/*.rake'
10-
- '**/Gemfile'
11-
- '**/Rakefile'
12-
- '**/Capfile'
13-
- '**/Guardfile'
14-
- '**/Podfile'
15-
- '**/Thorfile'
16-
- '**/Vagrantfile'
6+
- "**/*.rb"
7+
- "**/*.gemspec"
8+
- "**/*.podspec"
9+
- "**/*.jbuilder"
10+
- "**/*.rake"
11+
- "**/Gemfile"
12+
- "**/Rakefile"
13+
- "**/Capfile"
14+
- "**/Guardfile"
15+
- "**/Podfile"
16+
- "**/Thorfile"
17+
- "**/Vagrantfile"
1718
Exclude:
18-
- 'vendor/**/*'
19-
- 'stubs/**/*'
20-
- 'spec/support/shared_contexts/*'
19+
- "vendor/**/*"
20+
- "stubs/**/*"
21+
- "spec/support/shared_contexts/*"
2122

2223
NewCops: enable
2324

@@ -51,6 +52,10 @@ Style/DoubleNegation:
5152
Style/PerlBackrefs:
5253
Enabled: false
5354

55+
Style/OpenStructUse:
56+
Exclude:
57+
- "spec/**/*"
58+
5459
########################################
5560
# Lint Cops
5661

@@ -66,6 +71,10 @@ Security/Eval:
6671
########################################
6772
# Metrics Cops
6873

74+
Metrics/BlockLength:
75+
Exclude:
76+
- "spec/**/*"
77+
6978
Metrics/MethodLength:
7079
CountComments: false # count full line comments?
7180
Max: 30

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require 'rubygems'
44
require 'bundler/setup'
55

66
require 'rake'
7-
Dir['lib/tasks/**/*.rake'].sort.each { |ext| load ext }
7+
Dir['lib/tasks/**/*.rake'].each { |ext| load ext }
88

99
# Install rubygem tasks
1010
Bundler::GemHelper.install_tasks

lib/etl/common.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def show_me
1616
def limit(count)
1717
count = Integer(count || -1)
1818
return if count == -1
19+
1920
transform do |row|
2021
@counter ||= 0
2122
@counter += 1

lib/etl/csv_source.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def initialize(filename:, headers: true, delimeter: "\t", quote_char: '"')
1414

1515
def each
1616
CSV.open(filename,
17-
col_sep: delimeter,
18-
headers: headers,
19-
header_converters: :symbol,
20-
quote_char: quote_char) do |csv|
17+
col_sep: delimeter,
18+
headers: headers,
19+
header_converters: :symbol,
20+
quote_char: quote_char) do |csv|
2121
csv.each do |row|
2222
yield(row.to_hash)
2323
end

lib/free_zipcode_data.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ def self.current_environment
1414
ENV.fetch('APP_ENV', 'development')
1515
end
1616

17-
#:nocov:
17+
# :nocov:
1818
def self.config_file(filename = '.free_zipcode_data.yml')
1919
return root.join('spec', 'fixtures', filename) if current_environment == 'test'
2020

21-
home = ENV.fetch('HOME')
21+
home = Dir.home
2222
file = ENV.fetch('FZD_CONFIG_FILE', File.join(home, '.free_zipcode_data.yml'))
2323
FileUtils.touch(file)
2424
file
2525
end
26-
#:nocov:
26+
# :nocov:
2727

2828
def self.os
2929
if RUBY_PLATFORM.match?(/cygwin|mswin|mingw|bccwin|wince|emx/)

lib/free_zipcode_data/county_table.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def write(row)
4141
database.execute(sql)
4242
rescue SQLite3::ConstraintException
4343
# swallow duplicates
44-
rescue StandardError => err
45-
raise "Please file an issue at #{ISSUE_URL}: [#{err}] -> SQL: [#{sql}]"
44+
rescue StandardError => e
45+
raise "Please file an issue at #{ISSUE_URL}: [#{e}] -> SQL: [#{sql}]"
4646
end
4747

4848
update_progress

lib/free_zipcode_data/db_table.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class DbTable
88
ISSUE_URL = 'https://github.com/midwire/free_zipcode_data/issues/new'
99

1010
attr_reader :database, :tablename
11+
1112
@@progressbar = nil
1213

1314
def initialize(database:, tablename:)
@@ -33,9 +34,9 @@ def country_lookup_table
3334

3435
def select_first(sql)
3536
rows = database.execute(sql)
36-
rows[0].nil? ? nil : rows[0].first
37-
rescue SQLite3::SQLException => err
38-
raise "Please file an issue at #{ISSUE_URL}: [#{err}] -> SQL: [#{sql}]"
37+
rows[0]&.first
38+
rescue SQLite3::SQLException => e
39+
raise "Please file an issue at #{ISSUE_URL}: [#{e}] -> SQL: [#{sql}]"
3940
end
4041

4142
def get_country_id(country)
@@ -51,12 +52,13 @@ def get_state_id(state_abbr, state_name)
5152

5253
def get_county_id(county)
5354
return nil if county.nil?
55+
5456
sql = "SELECT id FROM counties WHERE name = '#{escape_single_quotes(county)}'"
5557
select_first(sql)
5658
end
5759

5860
def escape_single_quotes(string)
59-
string&.gsub(/[']/, '\'\'') || ''
61+
string&.gsub('\'', '\'\'') || ''
6062
end
6163
end
6264
end

lib/free_zipcode_data/logger.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,23 @@ def initialize(provider = default_logger)
1313
@log_provider = provider
1414
end
1515

16-
def log_exception(e, data = {})
17-
msg = "EXCEPTION : #{e.class.name} : #{e.message}"
16+
def log_exception(error, data = {})
17+
msg = "EXCEPTION : #{error.class.name} : #{error.message}"
1818
msg += "\n data : #{data.inspect}" if data && !data.empty?
19-
msg += "\n #{e.backtrace[0, 6].join("\n ")}"
19+
msg += "\n #{error.backtrace[0, 6].join("\n ")}"
2020
log_provider.error(msg)
2121
end
2222

23-
def method_missing(meth, *args, &block)
23+
def method_missing(meth, *, &)
2424
if log_provider.respond_to?(meth)
25-
log_provider.send(meth, *args, &block)
25+
log_provider.send(meth, *, &)
2626
else
2727
super
2828
end
2929
end
3030

31-
def respond_to?(meth, include_private = false)
32-
if log_provider.respond_to?(meth)
33-
true
34-
else
35-
super
36-
end
31+
def respond_to_missing?(meth, include_private = false)
32+
log_provider.respond_to?(meth) || super
3733
end
3834

3935
def verbose(msg)
@@ -43,7 +39,7 @@ def verbose(msg)
4339
private
4440

4541
def default_logger
46-
logger = ::Logger.new(STDOUT)
42+
logger = ::Logger.new($stdout)
4743
logger.formatter = proc do |_, _, _, msg|
4844
"#{msg}\n"
4945
end

lib/free_zipcode_data/runner.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ def start
5858
private
5959

6060
def initialize_table(table_sym, database)
61-
tablename = options["#{table_sym}_tablename".to_sym]
61+
tablename = options[:"#{table_sym}_tablename"]
6262
logger.verbose("Initializing #{table_sym} table: '#{tablename}'...")
63-
klass = instance_eval("#{titleize(table_sym)}Table", __FILE__, __LINE__)
63+
klass = FreeZipcodeData.const_get(:"#{titleize(table_sym)}Table")
6464
table = klass.new(
6565
database: database.conn,
6666
tablename: tablename

lib/free_zipcode_data/state_table.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def build
3030

3131
def write(row)
3232
return nil unless row[:short_state]
33+
3334
row[:state] = 'Marshall Islands' if row[:short_state] == 'MH' && row[:state].nil?
3435
country_id = get_country_id(row[:country])
3536
sql = <<-SQL

0 commit comments

Comments
 (0)