Class: Pgtk::Wire::Env

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

Overview

Using ENV variable.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2019-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(var = 'DATABASE_URL') ⇒ Env

Constructor.



49
50
51
52
# File 'lib/pgtk/wire.rb', line 49

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

Instance Method Details

#connectionObject

Create a new connection to PostgreSQL server.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pgtk/wire.rb', line 55

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