Skip to main content

mod_xml_curl Ruby directory example

About

This example show how to build a directory application in Ruby.

This example is old, but included to provide guidance.

#!/usr/bin/env ruby
#
# 2007-10-16 -- jontow@zenbsd.net
#
#############################################################################
# Example psql session follows:
#############################################################################
#
#freeswitch=> \dt
# List of relations
# Schema | Name | Type | Owner
#--------+--------------+-------+-------
# public | auth | table | fs
#(1 row)
#
#freeswitch=> \d auth
# Table "public.auth"
# Column | Type | Modifiers
#----------+------------------------+---------------------------------------------------
# id | integer | not null default nextval('auth_id_seq'::regclass)
# username | character varying(64) | not null default ''::character varying
# password | character varying(64) | not null default ''::character varying
# domain | character varying(256) | not null default ''::character varying
# fullname | character varying(256) | not null default ''::character varying
#
#freeswitch=> select * from auth;
# id | username | password | domain | fullname
#----+----------+----------+-------------+----------------
# 1 | user | passwd | example.com | Joe Random
#(1 row)
#
#############################################################################

require 'postgres'
require "cgi"

pghost = 'database.example.com'
pgport = 5432
pgoptions = nil
pgtty = nil
dbname = 'freeswitch'
pguser = 'username'
pgpass = 'password'

cgi = CGI.new
params = cgi.params

def printheader
puts "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
puts "<document type=\"freeswitch/xml\">"
puts "<section name=\"directory\" description=\"User Directory\">"
end

def printfooter
puts "</section>"
puts "</document>"
end

puts "Content-type: text/plain\r\n\r\n"

domain = params['domain'].to_s
username = params['user'].to_s

if (domain == "") or (username == "")
exit
end

pgdb = PGconn.connect(pghost, pgport, pgoptions, pgtty, dbname, pguser, pgpass)
pgres = pgdb.exec("
SELECT password
FROM auth
WHERE domain = '#{domain}'
AND username = '#{username}'
LIMIT 1")

if pgres.num_tuples != 0
pgres.result.each do |row|
password = row[0]
printheader
puts "<domain name=\"#{domain}\">"
puts " <user id=\"#{username}\">"
puts " <params>"
puts " <param name=\"password\" value=\"#{password}\" />"
puts " </params>"
puts " </user>"
puts "</domain>"
printfooter
end
end

exit