Class: Pgchief::ConnectionString

Inherits:
Object
  • Object
show all
Defined in:
lib/pgchief/connection_string.rb

Overview

Class to parse and manipulate connection strings

Constant Summary collapse

URL_REGEX =
%r{(?x)\A
postgres(ql)?://
(?<username>[^:@]*)?
:?(?<password>[^@]*)?
@?(?<host>[^:]*)?
:?(?<port>\d+)?
/?(?<database>[^?]*)?
\z}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database_url, username: nil, password: nil, host: nil, port: nil, database: nil) ⇒ ConnectionString

Returns a new instance of ConnectionString.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pgchief/connection_string.rb', line 17

def initialize(
  database_url,
  username: nil,
  password: nil,
  host: nil,
  port: nil,
  database: nil
)
  @database_url = database_url
  @host = host
  @username = username
  @password = password
  @port = port
  @database = database
end

Instance Attribute Details

#database_urlObject (readonly)

Returns the value of attribute database_url.



15
16
17
# File 'lib/pgchief/connection_string.rb', line 15

def database_url
  @database_url
end

Instance Method Details

#databaseObject



53
54
55
# File 'lib/pgchief/connection_string.rb', line 53

def database
  @database || matched[:database] || ''
end

#hostObject



37
38
39
# File 'lib/pgchief/connection_string.rb', line 37

def host
  @host || (matched[:username] if matched[:host].empty?) || matched[:host]
end

#passwordObject



45
46
47
# File 'lib/pgchief/connection_string.rb', line 45

def password
  @password || matched[:password] || ''
end

#portObject



49
50
51
# File 'lib/pgchief/connection_string.rb', line 49

def port
  @port || matched[:port] || '5432'
end

#to_sObject



33
34
35
# File 'lib/pgchief/connection_string.rb', line 33

def to_s
  "postgresql://#{username}:#{password}@#{host}:#{port}/#{database}"
end

#usernameObject



41
42
43
# File 'lib/pgchief/connection_string.rb', line 41

def username
  @username || ('' if matched[:host].empty? && !matched[:username].empty?) || matched[:username] || ''
end