Skip to content

Instantly share code, notes, and snippets.

@mjmac
Created March 30, 2010 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjmac/349319 to your computer and use it in GitHub Desktop.
Save mjmac/349319 to your computer and use it in GitHub Desktop.
def self.parse(filename)
log = self.new(filename)
xml = nil
File.open(filename) do |logfile|
xml = Nokogiri::XML(logfile)
end
xml.xpath('//envelope').collect{|e| Nokogiri::XML(e.to_xml)}.each do |env|
sender = env.xpath('//sender').first.text
env.xpath('//message').each do |xmlmsg|
msg = Message.new(:sender_nick => sender,
:received => DateTime.parse(xmlmsg['received']),
:text => xmlmsg.text.gsub(/^\s*/,'').gsub(/\s*$/,''))
if block_given?
yield log, msg
else
log.messages << msg
end
end
end
log
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment