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 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(var = 'DATABASE_URL') ⇒ Env

Constructor.


66
67
68
69
# File 'lib/pgtk/wire.rb', line 66

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.


72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pgtk/wire.rb', line 72

def connection
  v = ENV[@var]
  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