Module: DatabaseUrl
- Defined in:
- lib/database_url.rb,
lib/database_url/version.rb
Constant Summary collapse
- DEFAULT_HOST =
'127.0.0.1'
- VERSION =
"0.0.1"
Class Method Summary collapse
Class Method Details
.active_record_config(url = nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/database_url.rb', line 8 def active_record_config(url = nil) if url.nil? url = ENV.fetch 'DATABASE_URL' end uri = URI.parse url retval = { adapter: uri.scheme, host: uri.host, database: File.basename(uri.path), } if uri.port retval[:port] = uri.port end if uri.user retval[:user] = uri.user end if uri.password retval[:password] = uri.password end query = CGI.parse uri.query if query.has_key?('encoding') retval[:encoding] = query['encoding'][0] end if query.has_key?('pool') retval[:pool] = query['pool'][0].to_i end retval end |
.database_url(active_record_config) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/database_url.rb', line 37 def database_url(active_record_config) # stringify keys c = active_record_config.inject({}) { |memo, (k, v)| memo[k.to_s] = v; memo } userinfo = if c.has_key?('user') or c.has_key?('username') or c.has_key?('password') username = c.values_at('user', 'username').compact.first [ username, c['password'] ].join ':' end query = {} if c.has_key?('encoding') query['encoding'] = c['encoding'] end if c.has_key?('pool') query['pool'] = c['pool'].to_i end query = if query.length > 0 URI.encode_www_form query end # URI.new(scheme, userinfo, host, port, registry, path, opaque, query, fragment, parser = DEFAULT_PARSER, arg_check = false) uri = URI::Generic.new( c.fetch('adapter'), userinfo, c.fetch('host', DEFAULT_HOST), c['port'], nil, # registry "/#{c.fetch('database')}", # path nil, # opaque query, # query nil, # fragment ) uri.to_s end |