Class: Gem::Uri

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/uri.rb

Overview

The Uri handles rubygems source URIs.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_uri) ⇒ Uri

Returns a new instance of Uri.



46
47
48
# File 'lib/rubygems/uri.rb', line 46

def initialize(source_uri)
  @parsed_uri = parse(source_uri)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/rubygems/uri.rb', line 72

def method_missing(method_name, *args, &blk)
  if @parsed_uri.respond_to?(method_name)
    @parsed_uri.send(method_name, *args, &blk)
  else
    super
  end
end

Class Method Details

.parse(uri) ⇒ Object

Parses uri, returning the original uri if it’s invalid



40
41
42
43
44
# File 'lib/rubygems/uri.rb', line 40

def self.parse(uri)
  parse!(uri)
rescue Gem::URI::InvalidURIError
  uri
end

.parse!(uri) ⇒ Object

Parses uri, raising if it’s invalid



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubygems/uri.rb', line 18

def self.parse!(uri)
  require_relative "vendor/uri/lib/uri"

  raise Gem::URI::InvalidURIError unless uri

  return uri unless uri.is_a?(String)

  # Always escape URI's to deal with potential spaces and such
  # It should also be considered that source_uri may already be
  # a valid URI with escaped characters. e.g. "{DESede}" is encoded
  # as "%7BDESede%7D". If this is escaped again the percentage
  # symbols will be escaped.
  begin
    Gem::URI.parse(uri)
  rescue Gem::URI::InvalidURIError
    Gem::URI.parse(Gem::URI::DEFAULT_PARSER.escape(uri))
  end
end

.redact(uri) ⇒ Object

Parses and redacts uri



11
12
13
# File 'lib/rubygems/uri.rb', line 11

def self.redact(uri)
  new(uri).redacted
end

Instance Method Details

#redact_credentials_from(text) ⇒ Object



66
67
68
69
70
# File 'lib/rubygems/uri.rb', line 66

def redact_credentials_from(text)
  return text unless valid_uri? && password? && text.include?(to_s)

  text.sub(password, "REDACTED")
end

#redactedObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubygems/uri.rb', line 50

def redacted
  return self unless valid_uri?

  if token? || oauth_basic?
    with_redacted_user
  elsif password?
    with_redacted_password
  else
    self
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/rubygems/uri.rb', line 80

def respond_to_missing?(method_name, include_private = false)
  @parsed_uri.respond_to?(method_name, include_private) || super
end

#to_sObject



62
63
64
# File 'lib/rubygems/uri.rb', line 62

def to_s
  @parsed_uri.to_s
end