Class: Knj::Google_sitemap
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
Instance Method Summary collapse
-
#add_url(url_value, lastmod_value, cf_value = nil, priority_value = nil) ⇒ Object
Adds a URL to the XML.
-
#initialize(args = {}) ⇒ Google_sitemap
constructor
A new instance of Google_sitemap.
-
#to_s ⇒ Object
This will return a non-human-readable XML-string.
-
#to_xml ⇒ Object
This will return a non-human-readable XML-string.
-
#write(io = $stdout) ⇒ Object
This will print the result.
Constructor Details
#initialize(args = {}) ⇒ Google_sitemap
Returns a new instance of Google_sitemap.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/knj/google_sitemap.rb', line 4 def initialize(args = {}) raise "No block given." if !block_given? @args = args #used for Time.iso8601. require "time" #REXML is known to leak memory - use subprocess. Knj.gem_require(:Ruby_process) begin Ruby_process::Cproxy.run do |data| @subproc = data[:subproc] @subproc.static(:Object, :require, "rexml/rexml") @subproc.static(:Object, :require, "rexml/document") @subproc.static(:Object, :require, "rexml/element") @doc = @subproc.new("REXML::Document") xmldecl = @subproc.new("REXML::XMLDecl", "1.0", "UTF-8") @doc << xmldecl urlset = @doc.add_element("urlset") urlset.add_attributes("xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9") @root = @doc.root yield(self) end ensure @doc = nil @root = nil @subproc = nil end end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
2 3 4 |
# File 'lib/knj/google_sitemap.rb', line 2 def doc @doc end |
Instance Method Details
#add_url(url_value, lastmod_value, cf_value = nil, priority_value = nil) ⇒ Object
Adds a URL to the XML.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/knj/google_sitemap.rb', line 41 def add_url(url_value, lastmod_value, cf_value = nil, priority_value = nil) if !lastmod_value or lastmod_value.to_i == 0 raise sprintf("Invalid date: %1$s, url: %2$s", lastmod_value.to_s, url_value) end el = @subproc.new("REXML::Element", "url") loc = el.add_element("loc") loc.text = url_value lm = el.add_element("lastmod") if @args.key?(:date_min) and @args[:date_min] > lastmod_value lastmod_value = @args[:date_min] end lm.text = lastmod_value.iso8601 if cf_value cf = el.add_element("changefreq") cf.text = cf_value end if priority_value priority = el.add_element("priority") priority.text = priority_value end @root << el end |
#to_s ⇒ Object
This will return a non-human-readable XML-string.
77 78 79 |
# File 'lib/knj/google_sitemap.rb', line 77 def to_s return @doc.to_s end |
#to_xml ⇒ Object
This will return a non-human-readable XML-string.
72 73 74 |
# File 'lib/knj/google_sitemap.rb', line 72 def to_xml return @doc.to_s end |
#write(io = $stdout) ⇒ Object
This will print the result.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/knj/google_sitemap.rb', line 82 def write(io = $stdout) #Require and spawn StringIO in the subprocess. @subproc.static(:Object, :require, "stringio") string_io = @subproc.new("StringIO") #We want a human-readable print. writer = @subproc.new("REXML::Formatters::Pretty", 5) writer.write(@doc, string_io) #Prepare printing by rewinding StringIO to read from beginning. string_io.rewind #Print out the result in bits to avoid raping the memory (subprocess is already raped - no question there...). string_io.each(4096) do |str| io.print(str) end end |