module OpmlParser
OpmlParser
Module
A very simple and easy to use module for importing and exporting OPML.
Credits¶ ↑
Kevin Gillieron <kevin.gillieron@gw-computing.net>
Public Instance Methods
export(feeds, title="No title")
click to toggle source
Export an OPML String to an array of Outline
objects.
Arguments:
feeds: (Array of Outline) An array of Outline objects title: (String) Title of the OPML document
# File lib/opml-parser.rb, line 47 def export(feeds, title="No title") builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml| xml.opml(version: "1.0") do xml.head { xml.title title } xml.body { feeds.each { |outline| xml.outline(outline.attributes) }} end end builder.to_xml end
import(contents)
click to toggle source
Convert an OPML String to an array of Outline
objects.
Arguments:
contents: (String) A String that contains the OMPL
# File lib/opml-parser.rb, line 33 def import(contents) doc = Nokogiri.XML(contents) doc.xpath("//body//outline").inject(Array.new) do |feeds, outline| next if outline.attributes.empty? feeds << Outline.new(xml_node_to_hash(outline.attributes)) end end
Private Instance Methods
xml_node_to_hash(nokogiri_node)
click to toggle source
Convert a Nokogiri XML Node to a Hash table
# File lib/opml-parser.rb, line 59 def xml_node_to_hash(nokogiri_node) attributes = Hash.new nokogiri_node.each do |k,v| attributes[k.to_sym] = v.value end return attributes end