Created
December 2, 2013 17:01
-
-
Save rthbound/7752691 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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