Class: Rack::Smack

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/smack.rb,
lib/rack/smack/version.rb

Overview

don’t cross me boy

Constant Summary collapse

ASSET =
%w[css gif jpg jpeg js png ico txt].freeze
BLOCKED =
%w[wp wordpress xmlrpc sfn].freeze
FILENAME =
'./ban_list.txt'.freeze
VERSION =
'2.0.2'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Smack

Returns a new instance of Smack.

Raises:

  • (TypeError)


8
9
10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
# 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