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
-
#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 ⇒ 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 |
# 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. @subproc = Knj::Process_meta.new("id" => "google_sitemap", "debug_err" => true) begin @subproc.static("Object", "require", "rexml/rexml") @subproc.static("Object", "require", "rexml/document") @doc = @subproc.new("REXML::Document") xmldecl = @subproc.new("REXML::XMLDecl", "1.0", "UTF-8") @doc._pm_send_noret("<<", xmldecl) urlset = @subproc.proxy_from_call(@doc, "add_element", "urlset") urlset._pm_send_noret("add_attributes", {"xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9"}) @root = @subproc.proxy_from_call(@doc, "root") yield(self) ensure @doc = nil @root = nil @subproc.destroy @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
37 38 39 40 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 |
# File 'lib/knj/google_sitemap.rb', line 37 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 = @subproc.proxy_from_call(el, "add_element", "loc") loc._pm_send_noret("text=", url_value) lm = @subproc.proxy_from_call(el, "add_element", "lastmod") if @args.key?(:date_min) and @args[:date_min] > lastmod_value lastmod_value = @args[:date_min] end lm._pm_send_noret("text=", lastmod_value.iso8601) if cf_value cf = @subproc.proxy_from_call(el, "add_element", "changefreq") cf._pm_send_noret("text=", cf_value) end if priority_value priority = @subproc.proxy_from_call("el", "add_element", "priority") priority._pm_send_noret("text=", priority_value) end @root._pm_send_noret("<<", el) end |
#to_s ⇒ Object
This will return a non-human-readable XML-string.
73 74 75 |
# File 'lib/knj/google_sitemap.rb', line 73 def to_s return @doc.to_s end |
#to_xml ⇒ Object
This will return a non-human-readable XML-string.
68 69 70 |
# File 'lib/knj/google_sitemap.rb', line 68 def to_xml return @doc.to_s end |
#write ⇒ Object
This will print the result.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/knj/google_sitemap.rb', line 78 def write #Require and spawn StringIO in the subprocess. @subproc.static("Object", "require", "stringio") string_io = @subproc.spawn_object("StringIO") #We want a human-readable print. writer = @subproc.spawn_object("REXML::Formatters::Pretty", 5) writer._pm_send_noret("write", @doc, string_io) #Prepare printing by rewinding StringIO to read from beginning. string_io._pm_send_noret("rewind") #Buffer results from subprocess in order to speed up printing. string_io. = true #Print out the result in bits to avoid raping the memory (subprocess is already raped - no question there...). string_io._pm_send_noret("each", 4096) do |str| print str end end |