Sha256: 7b76e4871c301964fb42935b775b0fd81c4fde1e1dcfd558b575cb0d83e51d06

Contents?: true

Size: 781 Bytes

Versions: 32

Compression:

Stored size: 781 Bytes

Contents

# frozen_string_literal: true

##
# 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)
    require "cgi"

    @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

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
rubygems-update-3.3.27 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.22 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.21 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.20 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.19 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.18 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.17 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.16 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.15 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.14 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.13 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.12 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.11 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.10 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.9 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.8 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.7 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.6 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.5 lib/rubygems/uri_formatter.rb
rubygems-update-3.4.4 lib/rubygems/uri_formatter.rb