Created
December 3, 2012 06:39
-
-
Save anaumov/4193187 to your computer and use it in GitHub Desktop.
Ruby xml validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class RssFeedValidator < ActiveModel::EachValidator | |
def validates_each(record, attribute, value) | |
feed = Curl::Easy.new(url) do|curl| | |
curl.on_success validate_xml(feed, record, attribute) | |
curl.on_failure record.errors[attribute] << "remote host not avalible." | |
end | |
end | |
def validate_xml(feed, record, attribute) | |
begin | |
XML::Document.string(feed) | |
rescue REXML::ParseException => e | |
record.errors[attribute] << "is not valid xml. #{e.continued_exception}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment