class UriShortener
Attributes
long_uri[R]
short_uri[R]
type[RW]
Public Class Methods
new(long_uri, type="t.cn")
click to toggle source
# File lib/uri_shortener.rb, line 8 def initialize long_uri, type="t.cn" @long_uri = URI.encode(long_uri.gsub /^http:\/\/|https:\/\//,"") @type = type # t.cn || rdcnzz || url.cn || 126.am @short_uri = "" end
Public Instance Methods
shorten()
click to toggle source
# File lib/uri_shortener.rb, line 14 def shorten @short_uri = case @type when "t.cn" then t_cn @long_uri when "url.cn" then url_cn @long_uri when "rdcnzz" then rdcnzz @long_uri when "126.am" then am @long_uri else puts "Not correct type" end end
verify()
click to toggle source
# File lib/uri_shortener.rb, line 24 def verify begin res = Net::HTTP.get_response URI(@short_uri) URI("http://"+@long_uri) == URI(URI.encode res["location"]) rescue false end end
Private Instance Methods
am(long_uri)
click to toggle source
# File lib/uri_shortener.rb, line 49 def am long_uri response = Net::HTTP.post_form(URI("http://126.am/api!shorten.action"), {:key=>"d1f04c518037412988e26e449469106f", :longUrl=>(URI.decode "http://"+long_uri)}) result = JSON.parse response.body "http://"+result["url"] end
rdcnzz(long_uri)
click to toggle source
# File lib/uri_shortener.rb, line 45 def rdcnzz long_uri response = Net::HTTP.get(URI("http://rdcnzz.com/v1/data/link_conv/rd_dispatcher.php?orig_link=#{long_uri}")) end
t_cn(long_uri)
click to toggle source
# File lib/uri_shortener.rb, line 34 def t_cn long_uri response = Net::HTTP.post_form(URI("http://tinyurl.duapp.com/create.php"), {:url=>(URI.encode_www_form_component URI.decode(long_uri)), :type=>"t.cn"}) result = JSON.parse response.body result['data'] end
url_cn(long_uri)
click to toggle source
# File lib/uri_shortener.rb, line 40 def url_cn long_uri response = Net::HTTP.get(URI("http://121.199.13.65/web/json.php?type=url.cn&xzurl=#{long_uri}")) (/http\:\/\/\w+\.\w+\/\w+/.match response).to_s end