对于Stack Overflow 聊天的 XMPP 接口,我正在解析来自聊天的 JSON 提要,并为每个聊天事件生成 Ruby 对象,例如发送的消息、发送的编辑、用户登录或注销等。我还为“斜线-命令”发送到 XMPP 服务器,例如“/help”或“/auth”,以允许 XMPP 用户使用他们的 Stack Overflow 聊天帐户进行身份验证。
我已经在层次结构中设置了这些类,我觉得这很合逻辑:
class SOChatEvent # base class
|
|--- class SOXMPPEvent # base for all events that are initiated via XMPP
| |
| |--- class SOXMPPMessage # messages sent to the XMPP bridge via XMPP
| | |
| | |--- class SOXMPPMessageToRoom # messages sent from an XMPP user to an XMPP MUC
| | |
| | |--- class SOXMPPUserCommand # class for "slash commands", that is, messages starting
| | | | # with /, used for sending commands to the bridge
| | | |
| | | |--- class SOXMPPUserHelpCommand
| | | |--- class SOXMPPUserLoginCommand
| | | |--- class SOXMPPUserBroadcastCommand
|
|--- class SOChatRoomEvent # base class for all events that originate from an SO chat room
| |
| |--- class SOChatMessage # messages sent to an SO chat room via the SO chat system
| | |
| | |--- class SOChatMessageEdit # edits made to a prior SOChatMessage
| |
| |--- class SOChatUserEvent # events related to SO chat users
| | |
| | |--- class SOChatUserJoinRoom #Event for when a So user joins a room
| | |--- class SOChatUserLeaveRoom #Event for when a So user leaves a room
(etc)
您可以在 Trac或通过 SVN查看完整的层次结构和源代码。
我的问题是双重的:首先,实例化这些事件的最佳方式是什么?我目前正在做的是使用一个巨大的switch
语句解析 JSON 事件——嗯,它是 ruby,所以它是一个case
语句——而且,它还不是巨大的,但如果我继续这样的话:
rooms.each do |room|
rid = "r"+"#{room.room_id}"
if !data[rid].nil?
@last_update = data[rid]['t'] if data[rid]['t']
if data[rid]["e"]
data[rid]["e"].each do |e|
puts "DEBUG: found an event: #{e.inspect}"
case e["event_type"]
when 1
event = SOChatMessage.new(room,e['user_name'])
event.encoded_body = e['content']
event.server = @server
events.push event
when 2
event = SOChatMessageEdit.new(room,e['user_name'])
event.encoded_body = e['content']
event.server = @server
events.push event
when 3
user = SOChatUser.new(e['user_id'], e['user_name'])
event = SOChatUserJoinRoom.new(room,user)
event.server = @server
events.push event
when 4
user = SOChatUser.new(e['user_id'], e['user_name'])
event = SOChatUserLeaveRoom.new(room,user)
event.server = @server
events.push event
end
end
end
end
end
但我想必须有更好的方法来处理它!像SOChatEvent.createFromJSON( json_data )
......但是,构建我的代码以便创建正确子类的对象以响应给定的最佳方式是event_type
什么?
其次,我实际上还没有使用 ant 子类SOXMPPUserCommand
。现在所有命令都只是SOXMPPUserCommand
其自身的实例,并且该类有一个execute
基于命令的正则表达式切换的方法。几乎相同的问题 - 我知道有更好的方法,我只是不确定最好的方法是什么:
def handle_message(msg)
puts "Room \"#{@name}\" handling message: #{msg}"
puts "message: from #{msg.from} type #{msg.type} to #{msg.to}: #{msg.body.inspect}"
event = nil
if msg.body =~ /\/.*/
#puts "DEBUG: Creating a new SOXMPPUserCommand"
event = SOXMPPUserCommand.new(msg)
else
#puts "DEBUG: Creating a new SOXMPPMessageToRoom"
event = SOXMPPMessageToRoom.new(msg)
end
if !event.nil?
event.user = get_soxmpp_user_by_jid event.from
handle_event event
end
end
和:
class SOXMPPUserCommand < SOXMPPMessage
def execute
case @body
when "/help"
"Available topics are: help auth /fkey /cookie\n\nFor information on a topic, send: /help <topic>"
when "/help auth"
"To use this system, you must send your StackOverflow chat cookie and fkey to the system. To do this, use the /fkey and /cookie commands"
when "/help /fkey"
"Usage: /fkey <fkey>. Displays or sets your fkey, used for authentication. Send '/fkey' alone to display your current fkey, send '/fkey <something>' to set your fkey to <something>. You can obtain your fkey via the URL: javascript:alert(fkey().fkey)"
when "/help /cookie"
"Usage: /cookie <cookie>. Displays or sets your cookie, used for authentication. Send '/cookie' alone to display your current fkey, send '/cookie <something>' to set your cookie to <something>"
when /\/fkey( .*)?/
if $1.nil?
"Your fkey is \"#{@user.fkey}\""
else
@user.fkey = $1.strip
if @user.authenticated?
"fkey set to \"#{@user.fkey}\". You are now logged in and can send messages to the chat"
else
"fkey set to \"#{@user.fkey}\". You must also send your cookie with /cookie before you can chat"
end
end
when /\/cookie( .*)?/
if $1.nil?
"Your cookie is: \"#{@user.cookie}\""
else
if $1 == " chocolate chip"
"You get a chocolate chip cookie!"
else
@user.cookie = $1.strip
if @user.authenticated?
"cookie set to \"#{@user.cookie}\". You are now logged in and can send messages to the chat"
else
"cookie set to \"#{@user.cookie}\". You must also send your fkey with /fkey before you can chat"
end
end
end
else
"Unknown Command \"#{@body}\""
end
end
end
我知道有更好的方法可以做到这一点,只是不确定具体是什么。创建子类的责任应该SOXMPPUserCommand
落在SOXMPPUserCommand
自己身上吗?所有子类都应该向父类注册吗?我需要一个新的班级吗?
在这种层次结构中实例化子类对象的最佳方法是什么?