Skip to content

Instantly share code, notes, and snippets.

@anaumov
Created December 3, 2012 06:39
Show Gist options
  • Save anaumov/4193187 to your computer and use it in GitHub Desktop.
Save anaumov/4193187 to your computer and use it in GitHub Desktop.
Ruby xml validator
# -*- 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