ruby - How to read an external YAML file on the internet and output to XML -
i need able make ruby application (no rails, if possible) opens external yaml file has on 104k lines of code in it, reads , filters out following 3 things:
!ruby/object:evtevent !ruby/object:nwspost !ruby/object:asset
and outputs these things xml file have built ruby program.
i unclear how start setting up, junior-level developer 1 year's experience.
although found on stack overflow shows snippet of code example on using nokogiri, don't know put code have modify situation:
require 'yaml' require 'nokogiri' yaml = "getorderdetails: id: '114' name: 'xyz'" doc = yaml.load yaml output = nokogiri::xml::builder.new |xml| xml.product{ xml.id doc["getorderdetails"]["id"] xml.name doc["getorderdetails"]["name"] } end puts output.to_xml #=> <?xml version="1.0"?> #=> <product> #=> <id>114</id> #=> <name>xyz</name> #=> </product>
how code init.rb file launch ruby program open yaml file in question, read it, , output xml?
what other ruby files need put in lib folder such ruby program handle task?
the code can go wherever it's convenient. ruby has no real expectation of file locations; run them. development team has guidelines, need talk them.
"init.rb" non-descriptive name file. try use more indicative of purpose of script.
reading remove file purpose easy openuri:
foo = open('http://domain.com/path/to/file.yaml').read
will return contents of file , store them in variable
foo
.the contents of yaml can parsed using:
yaml = yaml.load(foo)
at point
haml
contain array or hash, can accessed normal.what's more interesting that, once openuri loaded, patch
open
method, should make possible like:require 'open-uri' yaml = yaml.load_file('http://domain.com/path/to/file.yaml')
yaml has open file load disk,
load_file
does, , after openuri magic yaml class should have inherited magic. haven't tested should work.nokogiri's builder interface way go.
Comments
Post a Comment