From c67e71defe472e0ef0bb6ce4488e8162d274410b Mon Sep 17 00:00:00 2001 From: Gonzalo Rodriguez Date: Wed, 27 Feb 2019 23:29:32 -0300 Subject: [PATCH] style: prefer ruby 1.9+ hash syntax --- .rubocop.yml | 3 +++ Rakefile | 2 +- lib/rack/attack/cache.rb | 6 +++--- lib/rack/attack/throttle.rb | 8 ++++---- spec/allow2ban_spec.rb | 2 +- spec/fail2ban_spec.rb | 2 +- spec/integration/offline_spec.rb | 2 +- spec/rack_attack_throttle_spec.rb | 16 ++++++++-------- spec/rack_attack_track_spec.rb | 2 +- 9 files changed, 23 insertions(+), 20 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 59a1af4..2ced30d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -44,6 +44,9 @@ Style/BracesAroundHashParameters: Style/FrozenStringLiteralComment: Enabled: true +Style/HashSyntax: + Enabled: true + Style/RedundantFreeze: Enabled: true diff --git a/Rakefile b/Rakefile index f08b537..caa479c 100644 --- a/Rakefile +++ b/Rakefile @@ -26,4 +26,4 @@ Rake::TestTask.new(:test) do |t| t.pattern = "spec/**/*_spec.rb" end -task :default => [:rubocop, :test] +task default: [:rubocop, :test] diff --git a/lib/rack/attack/cache.rb b/lib/rack/attack/cache.rb index ac0f99c..53f09f1 100644 --- a/lib/rack/attack/cache.rb +++ b/lib/rack/attack/cache.rb @@ -29,7 +29,7 @@ module Rack end def write(unprefixed_key, value, expires_in) - store.write("#{prefix}:#{unprefixed_key}", value, :expires_in => expires_in) + store.write("#{prefix}:#{unprefixed_key}", value, expires_in: expires_in) end def reset_count(unprefixed_key, period) @@ -54,13 +54,13 @@ module Rack enforce_store_presence! enforce_store_method_presence!(:increment) - result = store.increment(key, 1, :expires_in => expires_in) + result = store.increment(key, 1, expires_in: expires_in) # NB: Some stores return nil when incrementing uninitialized values if result.nil? enforce_store_method_presence!(:write) - store.write(key, 1, :expires_in => expires_in) + store.write(key, 1, expires_in: expires_in) end result || 1 end diff --git a/lib/rack/attack/throttle.rb b/lib/rack/attack/throttle.rb index e834fc0..c688a0a 100644 --- a/lib/rack/attack/throttle.rb +++ b/lib/rack/attack/throttle.rb @@ -31,10 +31,10 @@ module Rack epoch_time = cache.last_epoch_time data = { - :count => count, - :period => current_period, - :limit => current_limit, - :epoch_time => epoch_time + count: count, + period: current_period, + limit: current_limit, + epoch_time: epoch_time } (request.env['rack.attack.throttle_data'] ||= {})[name] = data diff --git a/spec/allow2ban_spec.rb b/spec/allow2ban_spec.rb index 074a231..0b7e780 100644 --- a/spec/allow2ban_spec.rb +++ b/spec/allow2ban_spec.rb @@ -9,7 +9,7 @@ describe 'Rack::Attack.Allow2Ban' do @findtime = 60 @bantime = 60 Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new - @f2b_options = { :bantime => @bantime, :findtime => @findtime, :maxretry => 2 } + @f2b_options = { bantime: @bantime, findtime: @findtime, maxretry: 2 } Rack::Attack.blocklist('pentest') do |req| Rack::Attack::Allow2Ban.filter(req.ip, @f2b_options) { req.query_string =~ /OMGHAX/ } diff --git a/spec/fail2ban_spec.rb b/spec/fail2ban_spec.rb index 91242f7..56c9ccc 100644 --- a/spec/fail2ban_spec.rb +++ b/spec/fail2ban_spec.rb @@ -9,7 +9,7 @@ describe 'Rack::Attack.Fail2Ban' do @findtime = 60 @bantime = 60 Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new - @f2b_options = { :bantime => @bantime, :findtime => @findtime, :maxretry => 2 } + @f2b_options = { bantime: @bantime, findtime: @findtime, maxretry: 2 } Rack::Attack.blocklist('pentest') do |req| Rack::Attack::Fail2Ban.filter(req.ip, @f2b_options) { req.query_string =~ /OMGHAX/ } diff --git a/spec/integration/offline_spec.rb b/spec/integration/offline_spec.rb index 1f2cbfa..26779a8 100644 --- a/spec/integration/offline_spec.rb +++ b/spec/integration/offline_spec.rb @@ -24,7 +24,7 @@ if defined?(::ActiveSupport::Cache::RedisStore) before do @cache = Rack::Attack::Cache.new # Use presumably unused port for Redis client - @cache.store = ActiveSupport::Cache::RedisStore.new(:host => '127.0.0.1', :port => 3333) + @cache.store = ActiveSupport::Cache::RedisStore.new(host: '127.0.0.1', port: 3333) end end end diff --git a/spec/rack_attack_throttle_spec.rb b/spec/rack_attack_throttle_spec.rb index 616fea0..ef47a7b 100644 --- a/spec/rack_attack_throttle_spec.rb +++ b/spec/rack_attack_throttle_spec.rb @@ -6,7 +6,7 @@ describe 'Rack::Attack.throttle' do before do @period = 60 # Use a long period; failures due to cache key rotation less likely Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new - Rack::Attack.throttle('ip/sec', :limit => 1, :period => @period) { |req| req.ip } + Rack::Attack.throttle('ip/sec', limit: 1, period: @period) { |req| req.ip } end it('should have a throttle') { Rack::Attack.throttles.key?('ip/sec') } @@ -22,7 +22,7 @@ describe 'Rack::Attack.throttle' do end it 'should populate throttle data' do - data = { :count => 1, :limit => 1, :period => @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i } + data = { count: 1, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i } last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data end end @@ -39,7 +39,7 @@ describe 'Rack::Attack.throttle' do it 'should tag the env' do last_request.env['rack.attack.matched'].must_equal 'ip/sec' last_request.env['rack.attack.match_type'].must_equal :throttle - last_request.env['rack.attack.match_data'].must_equal(:count => 2, :limit => 1, :period => @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i) + last_request.env['rack.attack.match_data'].must_equal(count: 2, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i) last_request.env['rack.attack.match_discriminator'].must_equal('1.2.3.4') end @@ -53,7 +53,7 @@ describe 'Rack::Attack.throttle with limit as proc' do before do @period = 60 # Use a long period; failures due to cache key rotation less likely Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new - Rack::Attack.throttle('ip/sec', :limit => lambda { |_req| 1 }, :period => @period) { |req| req.ip } + Rack::Attack.throttle('ip/sec', limit: lambda { |_req| 1 }, period: @period) { |req| req.ip } end it_allows_ok_requests @@ -67,7 +67,7 @@ describe 'Rack::Attack.throttle with limit as proc' do end it 'should populate throttle data' do - data = { :count => 1, :limit => 1, :period => @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i } + data = { count: 1, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i } last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data end end @@ -77,7 +77,7 @@ describe 'Rack::Attack.throttle with period as proc' do before do @period = 60 # Use a long period; failures due to cache key rotation less likely Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new - Rack::Attack.throttle('ip/sec', :limit => lambda { |_req| 1 }, :period => lambda { |_req| @period }) { |req| req.ip } + Rack::Attack.throttle('ip/sec', limit: lambda { |_req| 1 }, period: lambda { |_req| @period }) { |req| req.ip } end it_allows_ok_requests @@ -91,7 +91,7 @@ describe 'Rack::Attack.throttle with period as proc' do end it 'should populate throttle data' do - data = { :count => 1, :limit => 1, :period => @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i } + data = { count: 1, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i } last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data end end @@ -101,7 +101,7 @@ describe 'Rack::Attack.throttle with block retuning nil' do before do @period = 60 Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new - Rack::Attack.throttle('ip/sec', :limit => 1, :period => @period) { |_| nil } + Rack::Attack.throttle('ip/sec', limit: 1, period: @period) { |_| nil } end it_allows_ok_requests diff --git a/spec/rack_attack_track_spec.rb b/spec/rack_attack_track_spec.rb index 7ec4483..bba7035 100644 --- a/spec/rack_attack_track_spec.rb +++ b/spec/rack_attack_track_spec.rb @@ -56,7 +56,7 @@ describe 'Rack::Attack.track' do describe "with limit and period options" do it "should assign the track filter to a Throttle instance" do - track = Rack::Attack.track("homepage", :limit => 10, :period => 10) { |req| req.path == "/" } + track = Rack::Attack.track("homepage", limit: 10, period: 10) { |req| req.path == "/" } track.filter.class.must_equal Rack::Attack::Throttle end end