-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move URI parsing to a separate class
- Loading branch information
1 parent
e6ce7b2
commit 361e080
Showing
3 changed files
with
62 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
## | ||
# The UriParser handles parsing URIs. | ||
# | ||
|
||
class Gem::UriParser | ||
|
||
## | ||
# Parses the #uri, raising if it's invalid | ||
|
||
def parse!(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 | ||
|
||
## | ||
# Parses the #uri, returning the original uri if it's invalid | ||
|
||
def parse(uri) | ||
parse!(uri) | ||
rescue URI::InvalidURIError | ||
uri | ||
end | ||
|
||
end |