diff options
author | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-12-14 15:09:35 +0000 |
---|---|---|
committer | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-12-14 15:09:35 +0000 |
commit | 1a74fa4b04da04bd2bb33103dd3cf431438df38e (patch) | |
tree | f4a1d6c2961339e0c1d653c0f8427a53315080f0 /ruby_2_2/lib/rubygems/uri_formatter.rb | |
parent | a5b755e50e2d9aabf28ba24bf58644ca22b01a4f (diff) |
add tag v2_2_9
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/tags/v2_2_9@61257 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ruby_2_2/lib/rubygems/uri_formatter.rb')
-rw-r--r-- | ruby_2_2/lib/rubygems/uri_formatter.rb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/ruby_2_2/lib/rubygems/uri_formatter.rb b/ruby_2_2/lib/rubygems/uri_formatter.rb new file mode 100644 index 0000000000..68aacc6369 --- /dev/null +++ b/ruby_2_2/lib/rubygems/uri_formatter.rb @@ -0,0 +1,49 @@ +require 'cgi' +require 'uri' + +## +# The UriFormatter handles URIs from user-input and escaping. +# +# uf = Gem::UriFormatter.new 'example.com' +# +# p uf.normalize #=> 'http://example.com' + +class Gem::UriFormatter + + ## + # The URI to be formatted. + + attr_reader :uri + + ## + # Creates a new URI formatter for +uri+. + + def initialize uri + @uri = uri + end + + ## + # Escapes the #uri for use as a CGI parameter + + def escape + return unless @uri + CGI.escape @uri + end + + ## + # Normalize the URI by adding "http://" if it is missing. + + def normalize + (@uri =~ /^(https?|ftp|file):/i) ? @uri : "http://#{@uri}" + end + + ## + # Unescapes the #uri which came from a CGI parameter + + def unescape + return unless @uri + CGI.unescape @uri + end + +end + |