Class: Gem::UriFormatter

Inherits:
Object
  • Object
show all
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'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ UriFormatter

Creates a new URI formatter for uri.


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

def initialize(uri)
  @uri = uri
end

Instance Attribute Details

#uriObject (readonly)

The URI to be formatted.


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

def uri
  @uri
end

Instance Method Details

#escapeObject

Escapes the #uri for use as a CGI parameter


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

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

#normalizeObject

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


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

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

#unescapeObject

Unescapes the #uri which came from a CGI parameter


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

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