Class: Pgtk::Wire::Env
- Inherits:
-
Object
- Object
- Pgtk::Wire::Env
- Defined in:
- lib/pgtk/wire.rb
Overview
Using ENV variable.
The value of the variable should be in this format:
postgres://user:password@host:port/dbname
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2019-2025 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
-
#connection ⇒ Object
Create a new connection to PostgreSQL server.
-
#initialize(var = 'DATABASE_URL') ⇒ Env
constructor
Constructor.
Constructor Details
#initialize(var = 'DATABASE_URL') ⇒ Env
Constructor.
62 63 64 65 |
# File 'lib/pgtk/wire.rb', line 62 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
#connection ⇒ Object
Create a new connection to PostgreSQL server.
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/pgtk/wire.rb', line 68 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 |