public
Last active

  • Download Gist
addressable_unicode.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# encoding: utf-8
require 'addressable/uri'
 
str = "http://उदाहरण.परीक्षा/मुख्य_पृष्ठ"
 
puts "original:"
puts "#=> #{str}"
#=> http://उदाहरण.परीक्षा/मुख्य_पृष्ठ
 
normalized = Addressable::URI.parse(str).normalize
 
puts "normalized:"
puts "#=> #{normalized.to_s}"
#=> http://xn--p1b6ci4b4b3a.xn--11b5bs3a9aj6g/%E0%A4%AE%E0%A5%81%E0%A4%96%E0%A5%8D%E0%A4%AF_%E0%A4%AA%E0%A5%83%E0%A4%B7%E0%A5%8D%E0%A4%A0
 
nicer = normalized.display_uri.to_s
 
puts "display_uri:"
puts "#=> #{nicer}"
#=> http://उदाहरण.परीक्षा/%E0%A4%AE%E0%A5%81%E0%A4%96%E0%A5%8D%E0%A4%AF_%E0%A4%AA%E0%A5%83%E0%A4%B7%E0%A5%8D%E0%A4%A0
 
display_s = Addressable::URI.parse(str).display_uri.to_s
pretty = Addressable::URI.unencode(display_s.force_encoding("ASCII-8BIT"))
 
puts "unencoded:"
puts "#=> #{pretty}"
#=> http://उदाहरण.परीक्षा/मुख्य_पृष्ठ

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.