Class: MiddleSquid::BlackList

Inherits:
Object
  • Object
show all
Includes:
Database
Defined in:
lib/middle_squid/blacklist.rb

Overview

Use to query the blacklist database. URIs can be matched by hostname (see #include_domain?), path (see #include_url?) or both (see #include?).

Instances of this class should be created using Builder#blacklist (otherwise they would not be seen by the “middle_squid index” command unless the “--full” flag is enabled).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Database

#db, setup

Constructor Details

#initialize(category, aliases: []) ⇒ BlackList

Returns a new instance of BlackList. Use Builder#blacklist instead.

Parameters:

  • category (String)
  • aliases (Array<String>) (defaults to: [])


19
20
21
22
# File 'lib/middle_squid/blacklist.rb', line 19

def initialize(category, aliases: [])
  @category = category
  @aliases = aliases
end

Instance Attribute Details

#aliasesArray<String> (readonly)

Returns the aliases passed to #initialize.

Returns:

  • (Array<String>)

    the aliases passed to #initialize



14
15
16
# File 'lib/middle_squid/blacklist.rb', line 14

def aliases
  @aliases
end

#categoryString (readonly)

Returns the category passed to #initialize.

Returns:



11
12
13
# File 'lib/middle_squid/blacklist.rb', line 11

def category
  @category
end

Instance Method Details

#include?(uri) ⇒ Boolean

Whether this blacklists contains the uri. Matches by domain and/or path.

Parameters:

  • uri (URI)

    the uri to search

Returns:

  • (Boolean)


63
64
65
# File 'lib/middle_squid/blacklist.rb', line 63

def include?(uri)
  include_domain?(uri) || include_url?(uri)
end

#include_domain?(uri) ⇒ Boolean

Whether the blacklist category contains the URI’s hostname or an upper-level domain.

Rules to the www subdomain match any subdomains.

Examples:

Rule: sub.domain.com

Matches:
- http://sub.domain.com/...
- http://second.sub.domain.com/...
- http://infinite.level.of.sub.domain.com/...

Parameters:

  • uri (URI)

    the URI to search

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/middle_squid/blacklist.rb', line 34

def include_domain?(uri)
  !!db.get_first_value(
    "SELECT 1 FROM domains WHERE category = ? AND ? LIKE '%' || host LIMIT 1",
    [@category, uri.cleanhost]
  )
end

#include_url?(uri) ⇒ Boolean

Whether the blacklist category contains the URI. Matches by partial domain (like #include_domain?) and path. The query string is ignored.

Rules to index files (index.html, Default.aspx and friends) match the whole directory.

Examples:

Rule: domain.com/path

Matches:
- http://domain.com/path
- http://domain.com/dummy/../path
- http://domain.com/path/extra_path
- http://domain.com/path?query=string

Parameters:

  • uri (URI)

    the URI to search

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/middle_squid/blacklist.rb', line 52

def include_url?(uri)
  !!db.get_first_value(
    "SELECT 1 FROM urls WHERE category = ? AND ? LIKE '%' || host AND ? LIKE path || '%' LIMIT 1",
    [@category, uri.cleanhost, uri.cleanpath]
  )
end