aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rubygems/uri_formatter.rb
blob: deac3bfe7515eb0e8e26001335b9055deecd30d1 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
require 'uri'

class Gem::UriFormatter
  attr_reader :uri

  def initialize uri
    @uri = uri
  end

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

  ##
  # Normalize the URI by adding "http://" if it is missing.

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

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

  private

  def escaper
    @uri_parser ||=
      begin
        URI::Parser.new
      rescue NameError
        URI
      end
  end

end