Class: Pgtk::Wire::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/pgtk/wire.rb

Overview

Using ENV variable.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2019-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(var = 'DATABASE_URL') ⇒ Env

Constructor.

Parameters:

  • var (String) (defaults to: 'DATABASE_URL')

    The name of the environment variable with the connection URL



57
58
59
60
# File 'lib/pgtk/wire.rb', line 57

def initialize(var = 'DATABASE_URL')
  raise "The name of the environment variable can't be nil" if var.nil?
  @var = var
end

Instance Method Details

#connectionObject

Create a new connection to PostgreSQL server.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pgtk/wire.rb', line 63

def connection
  v = ENV.fetch(@var, nil)
  raise "The environment variable #{@var.inspect} is not set" if v.nil?
  uri = URI(v)
  Pgtk::Wire::Direct.new(
    host: uri.host,
    port: uri.port,
    dbname: uri.path[1..-1],
    user: uri.userinfo.split(':')[0],
    password: uri.userinfo.split(':')[1]
  ).connection
end