Class: Gem::Uri
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/rubygems/uri.rb |
Overview
The Uri handles rubygems source URIs.
Class Method Summary
- .new(source_uri) ⇒ Uri constructor
Instance Attribute Summary
-
#parsed_uri
readonly
protected
Add a protected reader for the cloned instance to access the original object’s parsed uri.
- #oauth_basic? ⇒ Boolean readonly private
- #password? ⇒ Boolean readonly private
- #token? ⇒ Boolean readonly private
- #valid_uri? ⇒ Boolean readonly private
Instance Method Summary
- #method_missing(method_name, *args, &blk)
- #redact_credentials_from(text)
- #redacted
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #to_s
- #initialize_copy(original) private
-
#parse(uri)
private
Parses the
#uri
, returning the original uri if it’s invalid. -
#parse!(uri)
private
Parses the
#uri
, raising if it’s invalid. - #with_redacted_password private
- #with_redacted_user private
Constructor Details
.new(source_uri) ⇒ Uri
# File 'lib/rubygems/uri.rb', line 8
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)
[ GitHub ]# File 'lib/rubygems/uri.rb', line 34
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
Instance Attribute Details
#oauth_basic? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubygems/uri.rb', line 100
def oauth_basic? password == 'x-oauth-basic' end
#parsed_uri (readonly, protected)
Add a protected reader for the cloned instance to access the original object’s parsed uri
# File 'lib/rubygems/uri.rb', line 49
attr_reader :parsed_uri
#password? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubygems/uri.rb', line 96
def password? !!password end
#token? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubygems/uri.rb', line 104
def token? !user.nil? && password.nil? end
#valid_uri? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubygems/uri.rb', line 92
def valid_uri? !@parsed_uri.is_a?(String) end
Instance Method Details
#initialize_copy(original) (private)
[ GitHub ]# File 'lib/rubygems/uri.rb', line 108
def initialize_copy(original) @parsed_uri = original.parsed_uri.clone end
#parse(uri) (private)
Parses the #uri
, returning the original uri if it’s invalid
# File 'lib/rubygems/uri.rb', line 76
def parse(uri) return uri unless uri.is_a?(String) parse!(uri) rescue URI::InvalidURIError uri end
#parse!(uri) (private)
Parses the #uri
, raising if it’s invalid
# File 'lib/rubygems/uri.rb', line 56
def parse!(uri) require "uri" raise URI::InvalidURIError unless uri # 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 URI.parse(uri) rescue URI::InvalidURIError URI.parse(URI::DEFAULT_PARSER.escape(uri)) end end
#redact_credentials_from(text)
[ GitHub ]# File 'lib/rubygems/uri.rb', line 28
def redact_credentials_from(text) return text unless valid_uri? && password? text.sub(password, 'REDACTED') end
#redacted
[ GitHub ]# File 'lib/rubygems/uri.rb', line 12
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
# File 'lib/rubygems/uri.rb', line 42
def respond_to_missing?(method_name, include_private = false) @parsed_uri.respond_to?(method_name, include_private) || super end
#to_s
[ GitHub ]# File 'lib/rubygems/uri.rb', line 24
def to_s @parsed_uri.to_s end
#with_redacted_password (private)
[ GitHub ]# File 'lib/rubygems/uri.rb', line 88
def with_redacted_password clone.tap {|uri| uri.password = 'REDACTED' } end
#with_redacted_user (private)
[ GitHub ]# File 'lib/rubygems/uri.rb', line 84
def with_redacted_user clone.tap {|uri| uri.user = 'REDACTED' } end