Skip to content

Instantly share code, notes, and snippets.

@tommi-lew
Last active October 6, 2015 04:18
Show Gist options
  • Save tommi-lew/2934994 to your computer and use it in GitHub Desktop.
Save tommi-lew/2934994 to your computer and use it in GitHub Desktop.
Using Nokogiri to convert hash to XML
require 'nokogiri'
def create_resource(xml)
xml.resource('path' => 'statuses/public_timeline.{format}') {
xml.param('name' => 'format', :'required' => 'true') {
xml.option('value' => 'json', 'mediaType' => 'application/json')
xml.option('value' => 'xml', 'mediaType' => 'application/xml')
}
xml.method_('id' => 'METHOD_ID', 'name' => 'GET', 'apigee:displayName' => 'METHOD_DISPALY_NAME') {
xml['apigee'].authentication('required' => 'false')
xml['apigee'].example('url' => 'false')
xml.doc_('title' => '', 'apigee:url' => 'http://www.farkyou.com') {
xml.text 'DOCUMENTATION HERE'
}
}
}
end
def create_application(xml)
xml.application('xmlns' => 'http://wadl.dev.java.net/2009/02',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:apigee' => 'http://api.apigee.com/wadl/2010/07/',
'xsi:schemaLocation' => 'http://wadl.dev.java.net/2009/02 http://apigee.com/schemas/wadl-schema.xsd http://api.apigee.com/wadl/2010/07/ http://apigee.com/schemas/apigee-wadl-extensions.xsd') {
xml.resources('base' => 'http://www.website.com/api/v3') {
#...create resource here...
create_resource(xml)
}
}
end
apigee = Nokogiri::XML::Builder.new { |xml|
create_application(xml)
}
puts apigee.to_xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment