Module: DomainExtractor::URIHelpers
- Defined in:
- lib/domain_extractor/uri_helpers.rb
Overview
URIHelpers provides advanced URI manipulation methods Including merge, normalize, authentication helpers, and proxy detection rubocop:disable Metrics/ModuleLength
Constant Summary collapse
- CREDENTIAL_ESCAPE_PATTERN =
/[^A-Za-z0-9\-._~]/- DEFAULT_PORTS =
{ 'ftp' => 21, 'ftps' => 990, 'http' => 80, 'https' => 443, 'mongodb' => 27_017, 'mysql' => 3306, 'postgresql' => 5432, 'redis' => 6379, 'rediss' => 6380, 'sftp' => 22, 'ssh' => 22 }.freeze
- HTTP_PROXY_KEYS =
%w[http_proxy HTTP_PROXY].freeze
- ALL_PROXY_KEYS =
%w[all_proxy ALL_PROXY].freeze
Class Method Summary collapse
-
.basic_auth_header(username, password) ⇒ String
Generate Basic Authentication header.
-
.bearer_auth_header(token) ⇒ String
Generate Bearer token header.
-
.decode_credential(value) ⇒ String
Decode percent-encoded credential.
- .default_port_for(uri_or_scheme) ⇒ Object
-
.encode_credential(value) ⇒ String
Encode credentials for URL (percent-encoding).
-
.find_proxy(uri) ⇒ URI::Generic?
Find proxy from environment variables Checks http_proxy, HTTP_PROXY, and no_proxy.
-
.merge_uri(base, relative) ⇒ URI::Generic
Merge a relative URI with a base URI.
-
.normalize_uri(uri) ⇒ URI::Generic
Normalize a URI (lowercase scheme and host, remove default ports).
Class Method Details
.basic_auth_header(username, password) ⇒ String
Generate Basic Authentication header
34 35 36 37 38 |
# File 'lib/domain_extractor/uri_helpers.rb', line 34 def basic_auth_header(username, password) credentials = "#{username}:#{password}" encoded = Base64.strict_encode64(credentials) "Basic #{encoded}" end |
.bearer_auth_header(token) ⇒ String
Generate Bearer token header
43 44 45 |
# File 'lib/domain_extractor/uri_helpers.rb', line 43 def bearer_auth_header(token) "Bearer #{token}" end |
.decode_credential(value) ⇒ String
Decode percent-encoded credential
57 58 59 60 61 |
# File 'lib/domain_extractor/uri_helpers.rb', line 57 def decode_credential(value) URI::DEFAULT_PARSER.unescape(value.to_s) rescue StandardError value end |
.default_port_for(uri_or_scheme) ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'lib/domain_extractor/uri_helpers.rb', line 158 def default_port_for(uri_or_scheme) case uri_or_scheme when URI::Generic uri_or_scheme.default_port || DEFAULT_PORTS[uri_or_scheme.scheme] else DEFAULT_PORTS[uri_or_scheme.to_s] end end |
.encode_credential(value) ⇒ String
Encode credentials for URL (percent-encoding)
50 51 52 |
# File 'lib/domain_extractor/uri_helpers.rb', line 50 def encode_credential(value) URI::DEFAULT_PARSER.escape(value.to_s, CREDENTIAL_ESCAPE_PATTERN) end |
.find_proxy(uri) ⇒ URI::Generic?
Find proxy from environment variables Checks http_proxy, HTTP_PROXY, and no_proxy
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/domain_extractor/uri_helpers.rb', line 67 def find_proxy(uri) uri_obj = coerce_uri(uri) return nil unless uri_obj return nil if should_bypass_proxy?(uri_obj) proxy_url = proxy_url_for(uri_obj.scheme) return nil unless proxy_url URI.parse(proxy_url) rescue URI::InvalidURIError nil end |
.merge_uri(base, relative) ⇒ URI::Generic
Merge a relative URI with a base URI
154 155 156 |
# File 'lib/domain_extractor/uri_helpers.rb', line 154 def merge_uri(base, relative) base.merge(relative) end |
.normalize_uri(uri) ⇒ URI::Generic
Normalize a URI (lowercase scheme and host, remove default ports)
146 147 148 |
# File 'lib/domain_extractor/uri_helpers.rb', line 146 def normalize_uri(uri) uri.normalize end |