diff options
author | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-16 18:49:26 +0900 |
---|---|---|
committer | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-30 00:39:06 +0900 |
commit | fcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch) | |
tree | 64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/lib/rubygems/uri_formatter.rb |
Fresh start
Diffstat (limited to 'jni/ruby/lib/rubygems/uri_formatter.rb')
-rw-r--r-- | jni/ruby/lib/rubygems/uri_formatter.rb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/jni/ruby/lib/rubygems/uri_formatter.rb b/jni/ruby/lib/rubygems/uri_formatter.rb new file mode 100644 index 0000000..68aacc6 --- /dev/null +++ b/jni/ruby/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 + |