Class: W3C::FeedValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/feed_validator.rb,
lib/feed_validator/assertions.rb

Overview

Implements an interface to the W3C Feed Validation online service, based on its SOAP 1.2 support.

It helps to find errors in RSS or Atom feeds.


Please remember that the W3C Feed Validation service is a shared resource, so do not abuse it: you should make your scripts sleep between requests.

Constant Summary collapse

VERSION =
"0.2.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFeedValidator

Initialize the feed validator object


78
79
80
# File 'lib/feed_validator.rb', line 78

def initialize
  clear
end

Instance Attribute Details

#errorsObject (readonly)

Collection of errors founded by the w3c feed validation service. Every error is a hash containing: :type, :line, :column, :text, :element


57
58
59
# File 'lib/feed_validator.rb', line 57

def errors
  @errors
end

#informationsObject (readonly)

Collection of informations founded by the w3c feed validation service. Every error is a hash containing: :type, :line, :column, :text, :element


69
70
71
# File 'lib/feed_validator.rb', line 69

def informations
  @informations
end

#request_typeObject (readonly)

request_type :manuel for direct input validation :url for url validation


74
75
76
# File 'lib/feed_validator.rb', line 74

def request_type
  @request_type
end

#responseObject (readonly)

The complete response (as Net::HTTPResponse object) sent it by the w3c feed validation service.


51
52
53
# File 'lib/feed_validator.rb', line 51

def response
  @response
end

#validObject (readonly) Also known as: valid?

True if the w3c feed validation service not found errors in the feed.


47
48
49
# File 'lib/feed_validator.rb', line 47

def valid
  @valid
end

#warningsObject (readonly)

Collection of warnings founded by the w3c feed validation service. Every error is a hash containing: :type, :line, :column, :text, :element


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

def warnings
  @warnings
end

Instance Method Details

#parse(response) ⇒ Object

Parse a response from the w3c feed validation service. Used by assert_valid_feed


33
34
35
36
37
38
39
40
# File 'lib/feed_validator/assertions.rb', line 33

def parse(response)
  clear
  if response.respond_to?(:body)
    parse_response(response.body)
  else
    parse_response(response)
  end
end

#to_sObject


123
124
125
126
127
128
129
130
131
132
# File 'lib/feed_validator.rb', line 123

def to_s
  msg = "Vailidity: #{@valid}\n"
  msg << "Errors count: #{@errors.size}\n"
  @errors.each_with_index{ |item, i| msg << "(#{i+1}) type: #{item[:type]} | line: #{item[:line]} | column: #{item[:column]} | text: #{item[:text]},\n"}
  msg << "Warnings count: #{@warnings.size}\n"
  @warnings.each_with_index{ |item, i| msg << "(#{i+1}) type: #{item[:type]} | line: #{item[:line]} | column: #{item[:column]} | text: #{item[:text]},\n"}
  msg << "Informations count: #{@informations.size}\n"
  @informations.each_with_index{ |item, i| msg << "(#{i+1}) type: #{item[:type]} | line: #{item[:line]} | column: #{item[:column]} | text: #{item[:text]},\n"}
  msg
end

#validate_data(rawdata) ⇒ Object

Validate the data provided. Returns a true value if the validation succeeded (regardless of whether the feed contains errors).


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/feed_validator.rb', line 85

def validate_data(rawdata)
  clear
  params = "rawdata=#{CGI.escape(rawdata)}&manual=1&output=soap12"
  @request_type = :manual
  begin
#        headers = VERSION == "1.8.4" ? {'Content-Type'=>'application/x-www-form-urlencoded'} : {}
    headers = {'Content-Type'=>'application/x-www-form-urlencoded'}
    @response = Net::HTTP.start('validator.w3.org',80) {|http|
      http.post('/feed/check.cgi',params,headers)
    }
  rescue Exception => e
    warn "Exception: #{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}" if $VERBOSE
    return false
  end

  parse_response(@response.body)
  return true
end

#validate_url(url) ⇒ Object

Validate the url provided. Returns a true value if the validation succeeded (regardless of whether the feed contains errors).


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/feed_validator.rb', line 107

def validate_url(url)
  clear
  params = "url=#{CGI.escape(url)}&output=soap12"
  @request_type = :url
  begin
    @response = Net::HTTP.get_response('validator.w3.org',"/feed/check.cgi?#{params}",80)
  rescue Exception => e
    warn "Exception: #{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}" if $VERBOSE
    return false
  end
  parse_response(@response.body)
  return true
end