Skip to content

Instantly share code, notes, and snippets.

@rthbound
Created December 2, 2013 17:01
Show Gist options
  • Save rthbound/7752691 to your computer and use it in GitHub Desktop.
Save rthbound/7752691 to your computer and use it in GitHub Desktop.
# Extend the Array class with a to_h method
class Array
def to_h
Hash[*self]
end
end
# Extend rexml/document to create a Ruby hash from xml
class REXML::Document
def record(xpath)
self.root.elements.each(xpath + '/*'){}.inject([]) do |r,node|
r << node.name.to_s << node.text.to_s
end.to_h
end
def records(xpath)
self.root.elements.each(xpath){}.map do |row|
row.elements.each{}.inject([]) do |r,node|
r << node.name.to_s << node.text.to_s
end.to_h
end
end
end
# Fetch the Survey document
doc = REXML::Document.new(RestClient.get BASE_URI + '&cmd=sgGetSurvey')
questions = doc.records('//apiResults/surveydata/pages/page/questions/question')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment