Class: PG::AzureWorkloadIdentity::ConnectionInfo::URI
- Inherits:
-
Object
- Object
- PG::AzureWorkloadIdentity::ConnectionInfo::URI
- Defined in:
- lib/pg/azure_workload_identity/connection_info/uri.rb
Overview
Parses and manipulates a PostgreSQL connection string in URI format
(e.g. postgres://user@host:5432/db?param=value).
Connection parameters may be provided either as URI components (userinfo, host, port) or as query-string parameters. Accessors prefer the URI component and fall back to the query string when absent.
The non-standard azure_workload_identity query parameter is recognized
and extracted on construction so the value can drive Azure Workload
Identity authentication while keeping the rest of the connection string
valid for libpq.
Instance Attribute Summary collapse
-
#azure_workload_identity ⇒ String?
readonly
The value of the
azure_workload_identityquery parameter that was present in the original connection string, ornilif the parameter was not set.
Class Method Summary collapse
-
.matches?(connection_string) ⇒ Boolean
Tests whether a connection string is in URI format.
Instance Method Summary collapse
-
#host ⇒ String?
The host from the URI authority, falling back to the
hostquery parameter when the URI host is missing or empty. -
#initialize(connection_string) ⇒ URI
constructor
Parses the given connection string and extracts the
azure_workload_identityquery parameter (if any) so it is not forwarded to libpq. -
#password=(value) ⇒ String
Sets the password, storing it as a
passwordquery parameter and clearing any password embedded in the URI userinfo. -
#port ⇒ Integer, ...
The port from the URI authority, falling back to the
portquery parameter. -
#to_s ⇒ String
Serializes the connection info back to a URI string.
-
#user ⇒ String?
The username from the URI userinfo, falling back to the
userquery parameter.
Constructor Details
#initialize(connection_string) ⇒ URI
Parses the given connection string and extracts the
azure_workload_identity query parameter (if any) so it is not
forwarded to libpq.
39 40 41 42 43 |
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 39 def initialize(connection_string) @uri = ::URI.parse(connection_string) @query = @uri.query ? ::URI.decode_www_form(@uri.query).to_h : {} # : Hash[String, String] @azure_workload_identity = @query.delete("azure_workload_identity") end |
Instance Attribute Details
#azure_workload_identity ⇒ String? (readonly)
Returns the value of the azure_workload_identity query
parameter that was present in the original connection string, or
nil if the parameter was not set.
32 33 34 |
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 32 def azure_workload_identity @azure_workload_identity end |
Class Method Details
.matches?(connection_string) ⇒ Boolean
Tests whether a connection string is in URI format.
25 26 27 |
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 25 def self.matches?(connection_string) /\A#{::URI::RFC2396_PARSER.regexp[:ABS_URI_REF]}\z/.match?(connection_string) end |
Instance Method Details
#host ⇒ String?
Returns the host from the URI authority, falling back to
the host query parameter when the URI host is missing or empty.
53 54 55 56 57 |
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 53 def host return @query["host"] if @uri.host.nil? || @uri.host.empty? @uri.host end |
#password=(value) ⇒ String
Sets the password, storing it as a password query parameter and
clearing any password embedded in the URI userinfo.
70 71 72 73 |
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 70 def password=(value) @uri.password = nil @query["password"] = value end |
#port ⇒ Integer, ...
Returns the port from the URI authority,
falling back to the port query parameter.
61 62 63 |
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 61 def port @uri.port || @query["port"] end |
#to_s ⇒ String
Serializes the connection info back to a URI string.
The query string is rebuilt from the current parameters (so the
azure_workload_identity flag is omitted) and the scheme is normalized
to always include the "//" authority separator, ensuring the result
is a valid libpq connection URI.
83 84 85 86 87 |
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 83 def to_s @uri.query = ::URI.encode_www_form(@query) @uri.to_s.sub(%r{^#{@uri.scheme}:(?!//)}, "#{@uri.scheme}://") end |
#user ⇒ String?
Returns the username from the URI userinfo, falling back
to the user query parameter.
47 48 49 |
# File 'lib/pg/azure_workload_identity/connection_info/uri.rb', line 47 def user @uri.user || @query["user"] end |