Skip to content

Commit

Permalink
Merge pull request #410 from grzuy/style
Browse files Browse the repository at this point in the history
Code style updates
  • Loading branch information
grzuy committed Mar 6, 2019
2 parents bf4e902 + 6541634 commit 935b918
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 83 deletions.
27 changes: 23 additions & 4 deletions .rubocop.yml
Expand Up @@ -41,16 +41,35 @@ Style/BlockDelimiters:
Style/BracesAroundHashParameters:
Enabled: true

Style/Encoding:
Enabled: true

Style/EmptyMethod:
Enabled: true

Style/FrozenStringLiteralComment:
Enabled: true

Style/HashSyntax:
Enabled: true

Style/OptionalArguments:
Enabled: true

Style/RaiseArgs:
Enabled: true

Style/RedundantBegin:
Enabled: true

Style/RedundantFreeze:
Enabled: true

# TODO
# Remove cop disabling and fix offenses
Lint/HandleExceptions:
Enabled: false
Style/RedundantSelf:
Enabled: true

Style/Semicolon:
Enabled: true

Style/SingleLineMethods:
Enabled: true
34 changes: 20 additions & 14 deletions lib/rack/attack.rb
Expand Up @@ -30,50 +30,56 @@ class << self
attr_accessor :notifier, :blocklisted_response, :throttled_response, :anonymous_blocklists, :anonymous_safelists

def safelist(name = nil, &block)
safelist = Safelist.new(name, block)
safelist = Safelist.new(name, &block)

if name
self.safelists[name] = safelist
safelists[name] = safelist
else
anonymous_safelists << safelist
end
end

def blocklist(name = nil, &block)
blocklist = Blocklist.new(name, block)
blocklist = Blocklist.new(name, &block)

if name
self.blocklists[name] = blocklist
blocklists[name] = blocklist
else
anonymous_blocklists << blocklist
end
end

def blocklist_ip(ip_address)
ip_blocklist_proc = lambda { |request| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }
anonymous_blocklists << Blocklist.new(nil, ip_blocklist_proc)
anonymous_blocklists << Blocklist.new { |request| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }
end

def safelist_ip(ip_address)
ip_safelist_proc = lambda { |request| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }
anonymous_safelists << Safelist.new(nil, ip_safelist_proc)
anonymous_safelists << Safelist.new { |request| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }
end

def throttle(name, options, &block)
self.throttles[name] = Throttle.new(name, options, block)
throttles[name] = Throttle.new(name, options, &block)
end

def track(name, options = {}, &block)
self.tracks[name] = Track.new(name, options, block)
tracks[name] = Track.new(name, options, &block)
end

def safelists; @safelists ||= {}; end
def safelists
@safelists ||= {}
end

def blocklists; @blocklists ||= {}; end
def blocklists
@blocklists ||= {}
end

def throttles; @throttles ||= {}; end
def throttles
@throttles ||= {}
end

def tracks; @tracks ||= {}; end
def tracks
@tracks ||= {}
end

def safelisted?(request)
anonymous_safelists.any? { |safelist| safelist.matched_by?(request) } ||
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/attack/blocklist.rb
Expand Up @@ -3,7 +3,7 @@
module Rack
class Attack
class Blocklist < Check
def initialize(name, block)
def initialize(name = nil, &block)
super
@type = :blocklist
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/attack/check.rb
Expand Up @@ -4,7 +4,7 @@ module Rack
class Attack
class Check
attr_reader :name, :block, :type
def initialize(name, options = {}, block)
def initialize(name, options = {}, &block)
@name, @block = name, block
@type = options.fetch(:type, nil)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/attack/safelist.rb
Expand Up @@ -3,7 +3,7 @@
module Rack
class Attack
class Safelist < Check
def initialize(name, block)
def initialize(name = nil, &block)
super
@type = :safelist
end
Expand Down
38 changes: 25 additions & 13 deletions lib/rack/attack/store_proxy/dalli_proxy.rb
Expand Up @@ -24,42 +24,54 @@ def initialize(client)
end

def read(key)
with do |client|
client.get(key)
rescuing do
with do |client|
client.get(key)
end
end
rescue Dalli::DalliError
end

def write(key, value, options = {})
with do |client|
client.set(key, value, options.fetch(:expires_in, 0), raw: true)
rescuing do
with do |client|
client.set(key, value, options.fetch(:expires_in, 0), raw: true)
end
end
rescue Dalli::DalliError
end

def increment(key, amount, options = {})
with do |client|
client.incr(key, amount, options.fetch(:expires_in, 0), amount)
rescuing do
with do |client|
client.incr(key, amount, options.fetch(:expires_in, 0), amount)
end
end
rescue Dalli::DalliError
end

def delete(key)
with do |client|
client.delete(key)
rescuing do
with do |client|
client.delete(key)
end
end
rescue Dalli::DalliError
end

private

def stub_with_if_missing
unless __getobj__.respond_to?(:with)
class << self
def with; yield __getobj__; end
def with
yield __getobj__
end
end
end
end

def rescuing
yield
rescue Dalli::DalliError
nil
end
end
end
end
Expand Down
26 changes: 16 additions & 10 deletions lib/rack/attack/store_proxy/redis_proxy.rb
Expand Up @@ -19,34 +19,40 @@ def self.handle?(store)
end

def read(key)
get(key)
rescue Redis::BaseError
rescuing { get(key) }
end

def write(key, value, options = {})
if (expires_in = options[:expires_in])
setex(key, expires_in, value)
rescuing { setex(key, expires_in, value) }
else
set(key, value)
rescuing { set(key, value) }
end
rescue Redis::BaseError
end

def increment(key, amount, options = {})
count = nil

pipelined do
count = incrby(key, amount)
expire(key, options[:expires_in]) if options[:expires_in]
rescuing do
pipelined do
count = incrby(key, amount)
expire(key, options[:expires_in]) if options[:expires_in]
end
end

count.value if count
rescue Redis::BaseError
end

def delete(key, _options = {})
del(key)
rescuing { del(key) }
end

private

def rescuing
yield
rescue Redis::BaseError
nil
end
end
end
Expand Down
8 changes: 3 additions & 5 deletions lib/rack/attack/store_proxy/redis_store_proxy.rb
Expand Up @@ -11,17 +11,15 @@ def self.handle?(store)
end

def read(key)
get(key, raw: true)
rescue Redis::BaseError
rescuing { get(key, raw: true) }
end

def write(key, value, options = {})
if (expires_in = options[:expires_in])
setex(key, expires_in, value, raw: true)
rescuing { setex(key, expires_in, value, raw: true) }
else
set(key, value, raw: true)
rescuing { set(key, value, raw: true) }
end
rescue Redis::BaseError
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rack/attack/throttle.rb
Expand Up @@ -6,10 +6,10 @@ class Throttle
MANDATORY_OPTIONS = [:limit, :period].freeze

attr_reader :name, :limit, :period, :block, :type
def initialize(name, options, block)
def initialize(name, options, &block)
@name, @block = name, block
MANDATORY_OPTIONS.each do |opt|
raise ArgumentError.new("Must pass #{opt.inspect} option") unless options[opt]
raise ArgumentError, "Must pass #{opt.inspect} option" unless options[opt]
end
@limit = options[:limit]
@period = options[:period].respond_to?(:call) ? options[:period] : options[:period].to_i
Expand Down
6 changes: 3 additions & 3 deletions lib/rack/attack/track.rb
Expand Up @@ -5,13 +5,13 @@ class Attack
class Track
attr_reader :filter

def initialize(name, options = {}, block)
def initialize(name, options = {}, &block)
options[:type] = :track

if options[:limit] && options[:period]
@filter = Throttle.new(name, options, block)
@filter = Throttle.new(name, options, &block)
else
@filter = Check.new(name, options, block)
@filter = Check.new(name, options, &block)
end
end

Expand Down
1 change: 0 additions & 1 deletion rack-attack.gemspec
@@ -1,4 +1,3 @@
# -*- encoding: utf-8 -*-
# frozen_string_literal: true

lib = File.expand_path('../lib/', __FILE__)
Expand Down
18 changes: 6 additions & 12 deletions spec/acceptance/cache_store_config_for_allow2ban_spec.rb
Expand Up @@ -22,11 +22,9 @@
raised_exception = nil

fake_store_class = Class.new do
def write(key, value)
end
def write(key, value); end

def increment(key, count, options = {})
end
def increment(key, count, options = {}); end
end

Object.stub_const(:FakeStore, fake_store_class) do
Expand All @@ -44,11 +42,9 @@ def increment(key, count, options = {})
raised_exception = nil

fake_store_class = Class.new do
def read(key)
end
def read(key); end

def increment(key, count, options = {})
end
def increment(key, count, options = {}); end
end

Object.stub_const(:FakeStore, fake_store_class) do
Expand All @@ -66,11 +62,9 @@ def increment(key, count, options = {})
raised_exception = nil

fake_store_class = Class.new do
def read(key)
end
def read(key); end

def write(key, value)
end
def write(key, value); end
end

Object.stub_const(:FakeStore, fake_store_class) do
Expand Down
18 changes: 6 additions & 12 deletions spec/acceptance/cache_store_config_for_fail2ban_spec.rb
Expand Up @@ -22,11 +22,9 @@
raised_exception = nil

fake_store_class = Class.new do
def write(key, value)
end
def write(key, value); end

def increment(key, count, options = {})
end
def increment(key, count, options = {}); end
end

Object.stub_const(:FakeStore, fake_store_class) do
Expand All @@ -44,11 +42,9 @@ def increment(key, count, options = {})
raised_exception = nil

fake_store_class = Class.new do
def read(key)
end
def read(key); end

def increment(key, count, options = {})
end
def increment(key, count, options = {}); end
end

Object.stub_const(:FakeStore, fake_store_class) do
Expand All @@ -66,11 +62,9 @@ def increment(key, count, options = {})
raised_exception = nil

fake_store_class = Class.new do
def read(key)
end
def read(key); end

def write(key, value)
end
def write(key, value); end
end

Object.stub_const(:FakeStore, fake_store_class) do
Expand Down

0 comments on commit 935b918

Please sign in to comment.