Class: BBCStandards::Validator

Inherits:
Object
  • Object
show all
Includes:
Spec::Matchers
Defined in:
lib/bbc_standards/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Spec::Matchers

#obey_bbc_standards

Constructor Details

#initialize(xml) ⇒ Validator

Returns a new instance of Validator.



7
8
9
10
11
12
13
14
15
# File 'lib/bbc_standards/validator.rb', line 7

def initialize(xml)
  @xml = xml
  # TODO: remove this once Barlesque sorts out the BBC Search form
  @xml.sub!('<form method="get" action="http://search.bbc.co.uk/search" accept-charset="utf-8"> <fieldset>  <input',
    '<form method="get" action="http://search.bbc.co.uk/search" accept-charset="utf-8"><fieldset><legend>foo</legend><input')
  @doc = Nokogiri::HTML(xml)
  @errors = []
  validate
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/bbc_standards/validator.rb', line 5

def errors
  @errors
end

Instance Method Details

#inspectObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bbc_standards/validator.rb', line 21

def inspect
  lines = @xml.split("\n")
  strings = []
  errors.each do |error|
    if error.line
      start = error.line - [2, error.line].min
      error_line = error.line == 0 ? 1 : error.line
      strings << "Error on line: #{error_line}:"
      strings << error.message.gsub(/\{[^\}]*\}/, '')
      Range.new(start, error.line + 2).each { |number|
        strings << "#{number + 1}: #{lines[number]}"
      }
      strings << ""
    else
      strings << error.message
    end
  end
  strings.join("\n")
end

#valid?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/bbc_standards/validator.rb', line 17

def valid?
  @errors.empty?
end