jam-cloud/ruby/lib/jam_ruby/models/icecast_admin_authenticatio...

102 lines
2.3 KiB
Ruby

module JAmXml
def jdumpXml (hash, nm, ident=1, output=$stdout, child=nil)
tb = "\t"
tbs = tb * ident
unless ident <= 0
tbse = tb * (ident-1)
output.puts "#{tbse}<#{nm}>"
end
hash.each do |key, val|
#puts "attrib: key=#{key} val=#{val}"
el = key
if !key.nil? && !key.empty?
el = key.gsub(/_/, '-')
end
sv = val
if val.to_s.empty?
#skip ???
next if val.to_s.empty?
else
if val.instance_of? String
#encode the string to be xml safe
sv = CGI.escapeHTML(val)
end
end
if key.empty?
output.puts "#{tbs}<#{sv}/>"
else
output.puts "#{tbs}<#{el}>#{sv}</#{el}>"
end
end
if !child.nil?
if child.kind_of? Array
child.each { |x| x.dumpXml(ident+1,output)}
else
child.dumpXml(ident+1,output)
end
end
unless ident <= 0
output.puts "#{tbse}</#{nm}>"
end
end
def jMakeStrXmlSafe(val)
sv = val
if val.to_s.empty?
#skip ???
sv =""
else
if val.instance_of? String
#encode the string to be xml safe
sv = CGI.escapeHTML(val)
end
end
return sv
end
end
module JamRuby
class IcecastAdminAuthentication < ActiveRecord::Base
include JAmXml
attr_accessible :source_password, :relay_user, :relay_password, :admin_user, :admin_password
#myattr_accessor = [:source_password, :relay_user, :relay_password, :admin_user, :admin_password ]
after_initialize :init
protected
def init
#set only if nil
self.source_password ||= "UndefinedSourcePassword"
self.admin_password ||= "JKAminPw"
end
public
self.primary_key = 'id'
validates :source_password, presence: true, length: {minimum: 5}
validates :admin_password, presence: true, length: {minimum: 5}
def dumpXml (ident=1, output=$stdout)
hash = Hash["source_password" => self.source_password,
"relay_user" => self.relay_user,
"relay_password" => self.relay_password,
"admin_user" => self.admin_user,
"admin_password" => self.admin_password]
self.jdumpXml(hash,"authentication", ident,output)
end
end
end