What is the best way to create an xml map of a recursive directory listing in rubyonrails?

I want to build an XML map of all directories and files in the public / catalogs rails directory. (recursive map)

I would like it to be created using an element <directory> <file>with a name attribute equal to the name of the file or file.

<catalogs>
 <file name="index.html">
 <directory name="foo">
    <file name="file1.html" />
    <directory name="bar">
       <file name="file2.html" />
    </directory>
 </directory>
</catalogs>

I'm just not sure what is the best way to make a recursive map for xml - I was looking for a plugin that could handle this, because it looks like someone might have, albeit build.

Any thoughts or direction on the best way to create this?

+3
source share
1 answer

, -, , , ... ... sha url, . , - .

xml = Builder::XmlMarkup.new(:indent => 2,:escape_attrs => true)
xml.instruct!    
xml.catalogs(:version=>2) {list_entries("#{CATALOG_PATH}", xml)}
File.open("#{RAILS_ROOT}/public/catalogs.xml", 'w') {|f| f.write(xml.target!) }

def list_entries(dir,xml)
  Dir.glob("#{dir}/*") do |entry|
    if File::directory?(entry)
      xml.directory(:name=>File.basename(entry)) {
        list_entries(entry, xml)
      }
    else
      xml.file(:name=>File.basename(entry),:sha => Digest::SHA256.hexdigest(entry),
      :url=>entry.gsub("#{CATALOG_PATH}","#{CATALOG_URL}"))
    end
  end
end
+2

Source: https://habr.com/ru/post/1705688/


All Articles