class PostUrlNormaliser
  def self.normalise raw_url
    value = raw_url.to_s.strip
    uri = URI.parse(value)
    return nil unless uri.is_a?(URI::HTTP) && uri.host.present?

    uri.host = uri.host.downcase
    uri.path = uri.path.sub(/\/\z/, '') if uri.path.present?
    PostUrlSanitisationRule.sanitise(uri.to_s)
  rescue URI::InvalidURIError
    nil
  end
end
