During my Ruby education, I decided I wanted to create a model that would store feed urls. Feeds could be supplied by http or ftp.

I needed a good way to check that the url is valid and the file exists. Here are the results:

  protected
    def url_must_be_a_real_uri
      begin
        uri = URI.parse(url)
        if(uri.class == URI::Generic)
          errors.add(:url, 'should be a valid url')
        end
      rescue URI::InvalidURIError
        errors.add(:url, 'should be a valid url')
      end
    end

    def url_must_open
        file = URI.parse(url).open
    end

The second method, url_must_open needs a bit more work, right now it throws exceptions if the file does not exist. It’s late and I’ve been drinking so I can’t be bothered to add a begin/rescue block :-)

Related posts:

  1. Some reasons why I am learning to love Ruby On Rails I have been programming using the Zend Framework for some...

Related posts brought to you by Yet Another Related Posts Plugin.