Class: SiteMaps::Builder::URLSet
- Inherits:
-
Object
- Object
- SiteMaps::Builder::URLSet
- Defined in:
- lib/site_maps/builder/url_set.rb
Constant Summary collapse
- SCHEMAS =
{ "image" => "http://www.google.com/schemas/sitemap-image/1.1", "mobile" => "http://www.google.com/schemas/sitemap-mobile/1.0", "news" => "http://www.google.com/schemas/sitemap-news/0.9", "pagemap" => "http://www.google.com/schemas/sitemap-pagemap/1.0", "video" => "http://www.google.com/schemas/sitemap-video/1.1" }.freeze
- XML_DECLARATION =
%(<?xml version="1.0" encoding="UTF-8"?>)- URLSET_OPEN =
<<~URLSET_OPEN <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" #{SCHEMAS.map { |name, uri| " xmlns:#{name}=\"#{uri}\"" }.join("\n")} > URLSET_OPEN
- HEADER =
"#{XML_DECLARATION}\n#{URLSET_OPEN}"- FOOTER =
"</urlset>"- FOOTER_BYTESIZE =
FOOTER.bytesize
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#links_count ⇒ Object
readonly
Returns the value of attribute links_count.
-
#news_count ⇒ Object
readonly
Returns the value of attribute news_count.
Instance Method Summary collapse
- #add(link, **options) ⇒ Object
- #empty? ⇒ Boolean
- #finalize! ⇒ Object
- #finalized? ⇒ Boolean
-
#initialize(max_links: SiteMaps::MAX_LENGTH[:links], emit_priority: true, emit_changefreq: true, xsl_url: nil) ⇒ URLSet
constructor
A new instance of URLSet.
- #last_modified ⇒ Object
- #to_xml ⇒ Object
Constructor Details
#initialize(max_links: SiteMaps::MAX_LENGTH[:links], emit_priority: true, emit_changefreq: true, xsl_url: nil) ⇒ URLSet
Returns a new instance of URLSet.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/site_maps/builder/url_set.rb', line 28 def initialize(max_links: SiteMaps::MAX_LENGTH[:links], emit_priority: true, emit_changefreq: true, xsl_url: nil) @content = StringIO.new if xsl_url @content.puts(XML_DECLARATION) @content.puts(XSLStylesheet.processing_instruction(xsl_url)) @content.puts(URLSET_OPEN) else @content.puts(HEADER) end @links_count = 0 @news_count = 0 @last_modified = nil @max_links = max_links @emit_priority = emit_priority @emit_changefreq = emit_changefreq end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
26 27 28 |
# File 'lib/site_maps/builder/url_set.rb', line 26 def content @content end |
#links_count ⇒ Object (readonly)
Returns the value of attribute links_count.
26 27 28 |
# File 'lib/site_maps/builder/url_set.rb', line 26 def links_count @links_count end |
#news_count ⇒ Object (readonly)
Returns the value of attribute news_count.
26 27 28 |
# File 'lib/site_maps/builder/url_set.rb', line 26 def news_count @news_count end |
Instance Method Details
#add(link, **options) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/site_maps/builder/url_set.rb', line 45 def add(link, **) raise SiteMaps::FullSitemapError if finalized? url = SiteMaps::Builder::URL.new(link, emit_priority: @emit_priority, emit_changefreq: @emit_changefreq, **) raise SiteMaps::FullSitemapError unless fit?(url) content.puts(url.to_xml) @links_count += 1 @news_count += 1 if url.news? if (lastmod = url.last_modified) @last_modified ||= lastmod @last_modified = lastmod if lastmod > @last_modified end url end |
#empty? ⇒ Boolean
80 81 82 |
# File 'lib/site_maps/builder/url_set.rb', line 80 def empty? links_count.zero? end |
#finalize! ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/site_maps/builder/url_set.rb', line 61 def finalize! return if finalized? content.puts(FOOTER) @to_xml = content.string.freeze content.close @to_xml end |
#finalized? ⇒ Boolean
76 77 78 |
# File 'lib/site_maps/builder/url_set.rb', line 76 def finalized? defined?(@to_xml) end |
#last_modified ⇒ Object
84 85 86 |
# File 'lib/site_maps/builder/url_set.rb', line 84 def last_modified @last_modified || Time.now end |
#to_xml ⇒ Object
70 71 72 73 74 |
# File 'lib/site_maps/builder/url_set.rb', line 70 def to_xml return content.string + FOOTER unless finalized? @to_xml end |