95 lines
1.9 KiB
Ruby
95 lines
1.9 KiB
Ruby
|
|
module JSONable
|
|
module ClassMethods
|
|
attr_accessor :attributes
|
|
|
|
def attr_accessor *attrs
|
|
self.attributes = Array attrs
|
|
super
|
|
end
|
|
end
|
|
|
|
def self.included(base)
|
|
base.extend(ClassMethods)
|
|
end
|
|
|
|
def as_json options = {}
|
|
serialized = Hash.new
|
|
self.class.attributes.each do |attribute|
|
|
serialized[attribute] = self.public_send attribute
|
|
end
|
|
serialized
|
|
end
|
|
|
|
def to_json *a
|
|
as_json.to_json *a
|
|
end
|
|
|
|
def jdumpXml (ovb, nm, ident=1, output=$stdout)
|
|
|
|
v = JSON.generate ovb
|
|
#puts "#{v}"
|
|
|
|
hash = JSON.parse(v)
|
|
#puts "#{hash}"
|
|
|
|
tb = "\t"
|
|
tbs = tb * ident
|
|
tbse = tb * (ident-1)
|
|
|
|
output.puts "#{tbse}<#{nm}>"
|
|
hash.each do |key, val|
|
|
#puts "attrib: key=#{key} val=#{val}"
|
|
|
|
el = key
|
|
if key.present?
|
|
el = key.gsub(/_/, '-')
|
|
end
|
|
|
|
|
|
sv = val
|
|
if val.to_s.empty?
|
|
#skip ???
|
|
else
|
|
if val.instance_of? String
|
|
#encode the string to be xml safe
|
|
sv = CGI.escapeHTML(val)
|
|
end
|
|
end
|
|
output.puts "#{tbs}<#{el}>#{sv}</#{el}>"
|
|
end
|
|
puts "#{tbse}</#{nm}>"
|
|
end
|
|
end
|
|
|
|
|
|
|
|
module JamRuby
|
|
class IcecastAdminAuthentication < ActiveRecord::Base
|
|
include JSONable
|
|
|
|
attr_accessible :source_password, :relay_user, :relay_password, :admin_user, :admin_password
|
|
attr_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)
|
|
self.jdumpXml(self,"authentication", ident,output)
|
|
end
|
|
|
|
|
|
end
|
|
end |