Class: DansGuardian::List

Inherits:
Object
  • Object
show all
Defined in:
lib/dansguardian/list.rb

Overview

this class does not inherit from ConfigFiles::Base

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ List

DansGuardian::List.new(:file = ‘/path/to/list’)



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dansguardian/list.rb', line 8

def initialize(h={})
  if h.is_a? String
    @init           = {:file => h}
    @file_encoding  = Encoding::BINARY
  else
    @init           = h      
  end
  @items          = []
  @includes       = []
  @listcategory   = nil
  #read! if @init[:file] 
end

Instance Attribute Details

#includesObject (readonly)

Returns the value of attribute includes.



5
6
7
# File 'lib/dansguardian/list.rb', line 5

def includes
  @includes
end

#itemsObject (readonly)

Returns the value of attribute items.



5
6
7
# File 'lib/dansguardian/list.rb', line 5

def items
  @items
end

#listcategoryObject (readonly)

Returns the value of attribute listcategory.



5
6
7
# File 'lib/dansguardian/list.rb', line 5

def listcategory
  @listcategory
end

Instance Method Details

#each_line(opts = {:exclude => []}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dansguardian/list.rb', line 45

def each_line(opts={:exclude => []}) 
  Enumerator.new do |yielder|
    File.foreach(@init[:file]) do |line|
      line.strip!
      line.force_encoding file_encoding
      case line
      when /^#listcategory:\s*"(.*)"/
        next if opts[:exclude].include? :listcategory
      when /^[\s#]*\.Include/
        next if opts[:exclude].include? :includes
      when /^[\s#]*</
        next if opts[:exclude].include? :items
      when /^\s*#/
        next if opts[:exclude].include? :comment_lines
      when /^\s*$/
        next if opts[:exclude].include? :blank_lines
      end
      yielder.yield line 
    end
  end
end

#file_encodingObject



22
# File 'lib/dansguardian/list.rb', line 22

def file_encoding; @init[:file_encoding]; end

#filenameObject



21
# File 'lib/dansguardian/list.rb', line 21

def filename; @init[:file]; end

#read!Object

Reads the file and fill @items ad @includes . This method might be overridden for non-trivial list types (?)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dansguardian/list.rb', line 26

def read!
  File.foreach(@init[:file]) do |line|
    line.force_encoding file_encoding
    line.strip!
    # Special comment used to "categorize" DG message pages
    if line =~ /^#listcategory:\s*"(.*)"/ 
      @listcategory = $1
      next
    end
    next if line =~ /^\s*#/
    line.sub! /\s#.*$/, '' # remove comments but allow http://url#anchor 
    if    line =~ /^\.Include<(.*)>/
      @includes << $1
    elsif line =~ /\S/
      @items << line.strip
    end
  end
end