如何验证一次又一次重复的网址

时间:2014-05-03 18:03:27

标签: ruby regex ruby-on-rails-4

我想在Rails 4模型中验证网址。大多数事情都有效,只是我被困在如何验证像

这样的网址
http://stackoverflow.com/questions/4716513http://stackoverflow.com/questions/4716513http://stackoverflow.com/questions/4716513

它一次又一次重复相同的url,目前我使用ruby URI lib来解析给定的字符串为url

1 个答案:

答案 0 :(得分:0)

require 'net/http'

class URI_shortener
def self.shorten (input)
checked_input = remove_duplicates(input)
begin
  url = URI.parse(checked_input)
  #shorten
rescue
  puts "Input was unfixable, I swear!"
end
end

private

def self.remove_duplicates(user_input)
normal_separator ='http://'
secure_separator = 'https://'
if(user_input.include?(normal_separator))
  array= user_input.split(normal_separator)
  @cleaned_input = normal_separator
else
  array =  user_input.split(secure_separator)
  @cleaned_input = secure_separator
end
return @cleaned_input+array[1]
end
end

input ="http://stackoverflow.com/questions/4716513http://stackoverflow.com/questions/4716513http://stackoverflow.com/questions/4716513"
p url = URI_shortener::shorten(input)
input = "https://google.com"
p url = URI_shortener::shorten(input)