From 4a1f4ca3c1e0cffda983d3b5e76312bb66afb79f Mon Sep 17 00:00:00 2001 From: Hakan Ensari Date: Tue, 11 Oct 2016 16:54:28 +0100 Subject: [PATCH] Use Rubocop and fix existing offences --- Gemfile | 1 + Gemfile.lock | 15 ++++++++++++++- Rakefile | 4 +++- config.ru | 2 ++ config/unicorn.rb | 4 +++- deploy/before_restart | 4 +++- lib/currency.rb | 3 +-- lib/quote.rb | 9 +++------ lib/tasks/rubocop.rake | 5 +++++ spec/converter_spec.rb | 4 +++- spec/edge_cases_spec.rb | 2 ++ 11 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 lib/tasks/rubocop.rake diff --git a/Gemfile b/Gemfile index 3cb7bcc..5861b4a 100644 --- a/Gemfile +++ b/Gemfile @@ -18,5 +18,6 @@ group :development do gem 'minitest-around' gem 'pry' gem 'rack-test' + gem 'rubocop' gem 'shotgun' end diff --git a/Gemfile.lock b/Gemfile.lock index 32dc05f..7a49781 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -16,7 +16,10 @@ GEM ast ruby-ll (~> 2.1) oj (2.15.1) + parser (2.3.1.4) + ast (~> 2.2) pg (0.18.4) + powerpack (0.1.1) pry (0.10.3) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -27,11 +30,19 @@ GEM rack rack-test (0.6.3) rack (>= 1.0) + rainbow (2.1.0) raindrops (0.16.0) rake (11.1.2) + rubocop (0.43.0) + parser (>= 2.3.1.1, < 3.0) + powerpack (~> 0.1) + rainbow (>= 1.99.1, < 3.0) + ruby-progressbar (~> 1.7) + unicode-display_width (~> 1.0, >= 1.0.1) ruby-ll (2.1.2) ansi ast + ruby-progressbar (1.8.1) sequel (4.35.0) sequel_pg (1.6.17) pg (>= 0.8.0) @@ -44,6 +55,7 @@ GEM tilt (>= 1.3, < 3) slop (3.6.0) tilt (2.0.5) + unicode-display_width (1.1.1) unicorn (5.1.0) kgio (~> 2.6) raindrops (~> 0.7) @@ -61,6 +73,7 @@ DEPENDENCIES rack-cors rack-test rake + rubocop sequel_pg shotgun sinatra @@ -70,4 +83,4 @@ RUBY VERSION ruby 2.3.1p112 BUNDLED WITH - 1.12.3 + 1.13.6 diff --git a/Rakefile b/Rakefile index 23632e1..802bdcc 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,4 @@ +# frozen_string_literal: true + Dir.glob('lib/tasks/*.rake').each { |r| import r } -task default: %w(db:migrate rates:load test) +task default: %w(db:migrate rates:load test rubocop) diff --git a/config.ru b/config.ru index 1012048..9450151 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require './config/environment' require 'api' diff --git a/config/unicorn.rb b/config/unicorn.rb index e35b8b1..bb3101f 100644 --- a/config/unicorn.rb +++ b/config/unicorn.rb @@ -1,7 +1,9 @@ # frozen_string_literal: true +worker_process_count = (ENV['WORKER_PROCESSES'] || 4).to_i + preload_app true -worker_processes((ENV['WORKER_PROCESSES'] || 4).to_i) +worker_processes worker_process_count timeout 10 before_fork do |_, _| diff --git a/deploy/before_restart b/deploy/before_restart index 5320730..1e431dc 100755 --- a/deploy/before_restart +++ b/deploy/before_restart @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: true + oldrev, newrev = ARGV def run(cmd) @@ -10,7 +12,7 @@ run 'whenever --update-crontab' tasks = [] num_migrations = `git diff #{oldrev} #{newrev} --diff-filter=A --name-only -z db/migrate`.split("\0").size -tasks << 'db:migrate' if num_migrations > 0 +tasks << 'db:migrate' if num_migrations.positive? tasks << 'rates:load' run "foreman run bundle exec rake #{tasks.join(' ')}" diff --git a/lib/currency.rb b/lib/currency.rb index 4437f0a..dcd5632 100644 --- a/lib/currency.rb +++ b/lib/currency.rb @@ -11,8 +11,7 @@ class Currency < Sequel::Model end def current_date - currency = recent.first - currency.date if currency + recent.first&.date end def current_date_before(value) diff --git a/lib/quote.rb b/lib/quote.rb index 8490d7d..6550b1f 100644 --- a/lib/quote.rb +++ b/lib/quote.rb @@ -27,7 +27,7 @@ class Quote private def base=(base) - @base = base ? base.upcase : DEFAULT_BASE + @base = base&.upcase || DEFAULT_BASE end def date=(date) @@ -35,11 +35,8 @@ class Quote raise Invalid, 'Date too old' unless current_date @date = current_date rescue Sequel::DatabaseError => ex - if ex.wrapped_exception.is_a?(PG::DataException) - raise Invalid, 'Invalid date' - else - raise - end + raise Invalid, 'Invalid date' if ex.wrapped_exception.is_a?(PG::DataException) + raise end def find_rates diff --git a/lib/tasks/rubocop.rake b/lib/tasks/rubocop.rake new file mode 100644 index 0000000..686fb05 --- /dev/null +++ b/lib/tasks/rubocop.rake @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +require 'rubocop/rake_task' + +RuboCop::RakeTask.new diff --git a/spec/converter_spec.rb b/spec/converter_spec.rb index 8185481..679aabc 100644 --- a/spec/converter_spec.rb +++ b/spec/converter_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative 'helper' require 'converter' @@ -29,7 +31,7 @@ describe Converter do end it 'should raise with invalid currency' do - quote.stub :rates, Hash.new do + quote.stub :rates, {} do -> { converter.convert(quote) }.must_raise Quote::Invalid, 'Invalid to' end end diff --git a/spec/edge_cases_spec.rb b/spec/edge_cases_spec.rb index 94d82eb..9a9da6d 100644 --- a/spec/edge_cases_spec.rb +++ b/spec/edge_cases_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative 'helper' require 'rack/test' require 'api'