class Rack::Smack

don't cross me boy

Constants

ASSET
BLOCKED
FILENAME
VERSION

Public Class Methods

new(app, opts = {}) click to toggle source
# File lib/rack/smack.rb, line 8
def initialize(app, opts = {})
  @app     = app
  @asset   = opts.delete(:asset) || ASSET
  @blocked = opts.delete(:list)  || BLOCKED
  @file    = opts.delete(:file)  || FILENAME
  @anon    = opts.delete(:anon)  || false
  raise TypeError        unless options_valid?
  IO.write(FILENAME, '') unless ::File.file?(@file)

  if @anon
    define_singleton_method(:ip) { Digest::SHA2.hexdigest(@req.ip)[0...16] }
  end
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/smack.rb, line 22
def call(env)
  @req = Rack::Request.new(env)
  return @app.call(env) if @asset.include? @req.path.split('.')[-1]
  return smack          if banned?
  return ban!           if @blocked.any? { |block| @req.path.index(block) }
  @app.call(env)
end

Private Instance Methods

ban!() click to toggle source
# File lib/rack/smack.rb, line 32
def ban!
  IO.write(@file, "#{ip},#{@req.path},#{Time.now}\n", mode: 'a')
  smack
end
banned?() click to toggle source
# File lib/rack/smack.rb, line 37
def banned?
  IO.foreach(@file) do |row|
    return true if row.split(',')[0] == ip
  end
  false
end
ip() click to toggle source
# File lib/rack/smack.rb, line 52
def ip
  @req.ip
end
options_valid?() click to toggle source
# File lib/rack/smack.rb, line 44
def options_valid?
  @blocked.is_a?(Array) && @asset.is_a?(Array) && @file.is_a?(String)
end
smack() click to toggle source
# File lib/rack/smack.rb, line 48
def smack
  [403, { 'Content-Type' => 'text/html' }, ['Banned.']]
end