I have created a module that validates the credentials against different databases.
module Authentication
DATABASES = %w[mysql mssql oracle].freeze
DATABASES.each do |database|
define_method("#{database}_connect") do |args|
client = case database
when 'mysql' then Mysql2::Client.new args.merge(ssl_mode: :disabled)
when 'mssql' then TinyTds::Client.new args
when 'oracle' then OCI8.new(args[:username], args[:password], "//#{args[:host]}:#{args[:port]}/#{args[:service_name]}")
end
case database
when 'mysql', 'oracle' then true
when 'mssql' then client.active?
end
end
end
def database_authentication_success?(database:, host:, port:, service_name:, username:, password:)
options = {host: host, port: port, service_name: service_name, username: username, password: password}
case database
when 'mysql' then mysql_connect(options)
when 'mssql' then mssql_connect(options)
when 'oracle' then oracle_connect(options)
end
end
end
Please let me know, how can it be improved?