Skip to content

Instantly share code, notes, and snippets.

@godfat
Created March 12, 2012 12:02
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 godfat/2021408 to your computer and use it in GitHub Desktop.
Save godfat/2021408 to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'time'
module Plist
module_function
def parse xml
fold_dict(Nokogiri::XML.parse(xml).root.xpath('./dict'))
end
def fold_dict dict
dict.xpath('./key').inject({}){ |r, key|
r[key.inner_text] = fold(key.next_element)
r
}
end
def fold_array array
array.children.map{ |child| fold(child) }.compact
end
def fold element
case element.name
when 'dict' ; fold_dict( element)
when 'array' ; fold_array(element)
when 'integer'; element.inner_text.to_i
when 'real' ; element.inner_text.to_f
when 'date' ; Time.parse(element.inner_text)
when 'false' ; false
when 'true' ; true
when 'string' ; element.inner_text
when 'data' ; element.inner_text
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment