123456789_123456789_123456789_123456789_123456789_

Class: Gem::UriFormatter

Relationships & Source Files
Inherits: Object
Defined in: lib/rubygems/uri_formatter.rb

Overview

The UriFormatter handles URIs from user-input and escaping.

uf = Gem::UriFormatter.new 'example.com'

p uf.normalize #=> 'http://example.com'

Class Method Summary

Instance Attribute Summary

  • #uri readonly

    The URI to be formatted.

Instance Method Summary

  • #escape

    Escapes the #uri for use as a CGI parameter.

  • #normalize

    Normalize the URI by adding “http://” if it is missing.

  • #unescape

    Unescapes the #uri which came from a CGI parameter.

Constructor Details

.new(uri) ⇒ UriFormatter

Creates a new URI formatter for #uri.

[ GitHub ]

  
# File 'lib/rubygems/uri_formatter.rb', line 22

def initialize uri
  @uri = uri
end

Instance Attribute Details

#uri (readonly)

The URI to be formatted.

[ GitHub ]

  
# File 'lib/rubygems/uri_formatter.rb', line 17

attr_reader :uri

Instance Method Details

#escape

Escapes the #uri for use as a CGI parameter

[ GitHub ]

  
# File 'lib/rubygems/uri_formatter.rb', line 29

def escape
  return unless @uri
  CGI.escape @uri
end

#normalize

Normalize the URI by adding “http://” if it is missing.

[ GitHub ]

  
# File 'lib/rubygems/uri_formatter.rb', line 37

def normalize
  (@uri =~ /^(https?|ftp|file):/i) ? @uri : "http://#{@uri}"
end

#unescape

Unescapes the #uri which came from a CGI parameter

[ GitHub ]

  
# File 'lib/rubygems/uri_formatter.rb', line 44

def unescape
  return unless @uri
  CGI.unescape @uri
end