Class: SiteMaps::Builder::URLSet

Inherits:
Object
  • Object
show all
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
HEADER =
<<~HEADER
  <?xml version="1.0" encoding="UTF-8"?>
  <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")}
  >
HEADER
"</urlset>"
FOOTER.bytesize

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeURLSet

Returns a new instance of URLSet.



27
28
29
30
31
32
33
# File 'lib/site_maps/builder/url_set.rb', line 27

def initialize
  @content = StringIO.new
  @content.puts(HEADER)
  @links_count = 0
  @news_count = 0
  @last_modified = nil
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



25
26
27
# File 'lib/site_maps/builder/url_set.rb', line 25

def content
  @content
end

Returns the value of attribute links_count.



25
26
27
# File 'lib/site_maps/builder/url_set.rb', line 25

def links_count
  @links_count
end

#news_countObject (readonly)

Returns the value of attribute news_count.



25
26
27
# File 'lib/site_maps/builder/url_set.rb', line 25

def news_count
  @news_count
end

Instance Method Details

#add(link, **options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/site_maps/builder/url_set.rb', line 35

def add(link, **options)
  raise SiteMaps::FullSitemapError if finalized?

  url = SiteMaps::Builder::URL.new(link, **options)
  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

Returns:

  • (Boolean)


70
71
72
# File 'lib/site_maps/builder/url_set.rb', line 70

def empty?
  links_count.zero?
end

#finalize!Object



51
52
53
54
55
56
57
58
# File 'lib/site_maps/builder/url_set.rb', line 51

def finalize!
  return if finalized?

  content.puts(FOOTER)
  @to_xml = content.string.freeze
  content.close
  @to_xml
end

#finalized?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/site_maps/builder/url_set.rb', line 66

def finalized?
  defined?(@to_xml)
end

#last_modifiedObject



74
75
76
# File 'lib/site_maps/builder/url_set.rb', line 74

def last_modified
  @last_modified || Time.now
end

#to_xmlObject



60
61
62
63
64
# File 'lib/site_maps/builder/url_set.rb', line 60

def to_xml
  return content.string + FOOTER unless finalized?

  @to_xml
end