Class: Spidr::Rules

Inherits:
Object
  • Object
show all
Defined in:
lib/spidr/rules.rb

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (Rules) initialize(options = {})

Creates a new Rules object.

Parameters:

  • (Hash) options (defaults to: {}) — Additional options.

Options Hash (options):

  • (Array<String, Regexp, Proc>) :accept N/A — The patterns to accept data with.
  • (Array<String, Regexp, Proc>) :reject N/A — The patterns to reject data with.


22
23
24
25
26
27
28
# File 'lib/spidr/rules.rb', line 22

def initialize(options={})
  @accept = []
  @reject = []

  @accept += options[:accept] if options[:accept]
  @reject += options[:reject] if options[:reject]
end

Instance Attribute Details

- (Object) accept (readonly)

Accept rules



5
6
7
# File 'lib/spidr/rules.rb', line 5

def accept
  @accept
end

- (Object) reject (readonly)

Reject rules



8
9
10
# File 'lib/spidr/rules.rb', line 8

def reject
  @reject
end

Instance Method Details

- (Boolean) accept?(data)

Determines whether the data should be accepted or rejected.

Returns:

  • (Boolean) — Specifies whether the given data was accepted, using the rules acceptance patterns.


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/spidr/rules.rb', line 37

def accept?(data)
  unless @accept.empty?
    @accept.each do |rule|
      return true if test_data(data,rule)
    end

    return false
  else
    @reject.each do |rule|
      return false if test_data(data,rule)
    end

    return true
  end
end

- (Boolean) reject?(data)

Determines whether the data should be rejected or accepted.

Returns:

  • (Boolean) — Specifies whether the given data was rejected, using the rules rejection patterns.


60
61
62
# File 'lib/spidr/rules.rb', line 60

def reject?(data)
  !(accept?(data))
end

- (Boolean) test_data(data, rule) (protected)

Tests the given data against a given pattern.

Returns:

  • (Boolean) — Specifies whether the given data matched the pattern.


72
73
74
75
76
77
78
79
80
# File 'lib/spidr/rules.rb', line 72

def test_data(data,rule)
  if rule.kind_of?(Proc)
    return (rule.call(data) == true)
  elsif rule.kind_of?(Regexp)
    return !((data.to_s =~ rule).nil?)
  else
    return data == rule
  end
end