Class: MiddleSquid::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/middle_squid/builder.rb

Overview

Small DSL to configure MiddleSquid.

Examples:

database '/home/proxy/blacklist.db'

adv     = blacklist 'adv', aliases: ['ads']
tracker = blacklist 'tracker'

run lambda {|uri, extras|
  if adv.include? uri
    redirect_to 'http://your.webserver/block_pages/advertising.html'
  end

  if tracker.include? uri
    redirect_to 'http://your.webserver/block_pages/tracker.html'
  end
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



43
44
45
46
# File 'lib/middle_squid/builder.rb', line 43

def initialize
  @blacklists = []
  @custom_actions = {}
end

Instance Attribute Details

#adapterAdapter (readonly)

Returns the adapter selected by #use.

Returns:



39
40
41
# File 'lib/middle_squid/builder.rb', line 39

def adapter
  @adapter ||= Adapters::Squid.new
end

#blacklistsArray<BlackList> (readonly)

Returns the blacklists registered by #blacklist.

Returns:



23
24
25
# File 'lib/middle_squid/builder.rb', line 23

def blacklists
  @blacklists
end

#custom_actionsHash<Symbol, Proc> (readonly)

Returns the custom actions created by #define_action.

Returns:

  • (Hash<Symbol, Proc>)


28
29
30
# File 'lib/middle_squid/builder.rb', line 28

def custom_actions
  @custom_actions
end

#handler#call (readonly)

Returns the object passed to #run.

Returns:

  • (#call)


33
34
35
# File 'lib/middle_squid/builder.rb', line 33

def handler
  @handler
end

Class Method Details

.from_file(file) ⇒ Builder

Returns:



49
50
51
52
53
54
55
# File 'lib/middle_squid/builder.rb', line 49

def self.from_file(file)
  obj = self.new
  content = File.read file

  obj.instance_eval content, file
  obj
end

Instance Method Details

#blacklist(*args) ⇒ BlackList

Note:

You need to call #database in order to use the blacklists.

Returns a new registered blacklist instance.

Examples:

Block advertising

adv = blacklist 'adv'

run lambda {|uri, extras|
  do_something if adv.include? uri
}

Group blacklists

adv = blacklist 'adv'
tracker = blacklist 'tracker'

group = [adv, tracker]

run lambda {|uri, extras|
  do_something if group.any? {|bl| bl.include? uri }
}

Create an alias

adv = blacklist 'adv', aliases: ['ads']

run lambda {|uri, extras|
  do_something if adv.include? uri
}

Returns:

See Also:



114
115
116
117
118
# File 'lib/middle_squid/builder.rb', line 114

def blacklist(*args)
  bl = BlackList.new *args
  @blacklists << bl
  bl
end

#database(path) ⇒ Object

Setup the blacklist database. It will be created if the file does not exists. Read/write access is required.

Run middle_squid index to add your blacklists to the database.

Examples:

database '/home/proxy/blacklist.db'

run lambda {|uri, extras| }

Parameters:

  • path (String)

    path to the SQLite database



84
85
86
# File 'lib/middle_squid/builder.rb', line 84

def database(path)
  Database.setup path
end

#define_action(name, &block) ⇒ Object Also known as: define_helper

Register a custom action or helper.

Examples:

Don’t Repeat Yourself

define_action :block do
  redirect_to 'http://goodsite.com/'
end

run lambda {|uri, extras|
  block if uri.host == 'badsite.com'
  # ...
  block if uri.host == 'terriblesite.com'
}

Parameters:

  • name (Symbol)

    method name

  • block (Proc)

    method body

Raises:

  • (ArgumentError)

See Also:



136
137
138
139
140
# File 'lib/middle_squid/builder.rb', line 136

def define_action(name, &block)
  raise ArgumentError, 'no block given' unless block_given?

  @custom_actions[name] = block
end

#run(handler) ⇒ Object

Takes any object that responds to the call method with two arguments: the URI to process and an array of extra data.

Examples:

run lambda {|uri, extras|
  # executed when the adapter has received a query from an underlying software (eg. Squid)
}

Parameters:

  • handler (#call<URI, Array>)

Raises:

  • (ArgumentError)

    if the handler does not respond to #call

See Also:



153
154
155
156
157
# File 'lib/middle_squid/builder.rb', line 153

def run(handler)
  raise ArgumentError, 'the handler must respond to #call' unless handler.respond_to? :call

  @handler = handler
end

#use(adapter, **options) ⇒ Adapter

Select the active adapter. By default Adapters::Squid with no options will be used.

Examples:

Squid in concurrency mode

use Adapters::Squid, concurrency: true

Parameters:

  • adapter (Class)
  • options (Hash)

    adapter configuration

Returns:

Raises:

  • (ArgumentError)

    if the adapter is not a subclass of Adapter

See Also:



67
68
69
70
71
# File 'lib/middle_squid/builder.rb', line 67

def use(adapter, **options)
  raise ArgumentError, 'Not an adapter.' unless adapter < Adapter

  @adapter = adapter.new(options)
end