Class: SitemapGenerator::Builder::SitemapIndexFile
- Inherits:
-
SitemapFile
- Object
- SitemapFile
- SitemapGenerator::Builder::SitemapIndexFile
- Defined in:
- lib/sitemap_generator/builder/sitemap_index_file.rb
Overview
Builds the sitemap index XML file, listing all sitemap files.
Manages deferred naming (waits for a second sitemap before reserving the index name)
and controls whether an index is written at all via create_index.
Constant Summary
Constants included from Helpers::NumberHelper
Helpers::NumberHelper::DECIMAL_UNITS, Helpers::NumberHelper::STORAGE_UNITS
Instance Attribute Summary
Attributes inherited from SitemapFile
#filesize, #link_count, #location, #news_count
Instance Method Summary collapse
-
#add(link, options = {}) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity.
-
#create_index? ⇒ Boolean
Whether or not we need to create an index file.
-
#file_can_fit?(bytes) ⇒ Boolean
Return a boolean indicating whether the sitemap file can fit another link of bytes bytes in size.
- #finalize! ⇒ Object
-
#index_url ⇒ Object
Return the index file URL.
-
#initialize(opts = {}) ⇒ SitemapIndexFile
constructor
Options.
- #stats_summary(opts = {}) ⇒ Object
-
#super_add ⇒ Object
Finalize sitemaps as they are added to the index.
-
#total_link_count ⇒ Object
Return the total number of links in all sitemaps reference by this index file.
-
#write ⇒ Object
Write out the index if an index is needed.
Methods inherited from SitemapFile
#empty?, #finalized?, #lastmod, #max_sitemap_links, #new, #reserve_name, #reserved_name?, #written?
Methods included from Helpers::NumberHelper
#number_to_human_size, #number_with_delimiter, #number_with_precision
Constructor Details
#initialize(opts = {}) ⇒ SitemapIndexFile
Options
- location - a SitemapGenerator::SitemapIndexLocation instance or a Hash of options from which a SitemapLocation will be created for you.
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 39 40 41 |
# File 'lib/sitemap_generator/builder/sitemap_index_file.rb', line 13 def initialize(opts = {}) # rubocop:disable Lint/MissingSuper, Metrics/MethodLength @location = opts.is_a?(Hash) ? SitemapGenerator::SitemapIndexLocation.new(opts) : opts @link_count = 0 @sitemaps_link_count = 0 # The + prefix makes this string explicitly mutable. Ruby 2.x with # --enable=frozen-string-literal freezes interpolated heredocs, and # gsub! below requires a mutable string. @xml_wrapper_start = +<<-HTML <?xml version="1.0" encoding="UTF-8"?> <sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" > HTML @xml_wrapper_start.gsub!(/\s+/, ' ').gsub!(/ *> */, '>').strip! @xml_wrapper_end = '</sitemapindex>' @filesize = SitemapGenerator::Utilities.bytesize(@xml_wrapper_start) + SitemapGenerator::Utilities.bytesize(@xml_wrapper_end) @xml_content = @xml_wrapper_start @written = false @reserved_name = nil # holds the name reserved from the namer @frozen = false # rather than actually freeze, use this boolean @first_sitemap = nil # reference to the first thing added to this index # Store the URL of the first sitemap added because if create_index is # false this is the "index" URL @first_sitemap_url = nil @create_index = nil end |
Instance Method Details
#add(link, options = {}) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sitemap_generator/builder/sitemap_index_file.rb', line 55 def add(link, = {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity if (file = link.is_a?(SitemapFile) && link) @sitemaps_link_count += file.link_count file.finalize! unless file.finalized? # First link. If it's a SitemapFile store a reference to it and the options # so that we can create a URL from it later. We can't create the URL yet # because doing so fixes the sitemap file's name, and we have to wait to see # if we have more than one link in the index before we can know who gets the # first name (the index, or the sitemap). If the item is not a SitemapFile, # then it has been manually added and we can be sure that the user intends # for there to be an index. if @link_count.zero? @first_sitemap = SitemapGenerator::Builder::LinkHolder.new(file, ) @link_count += 1 # pretend it's added, but don't add it yet else # need an index so make sure name is reserved and first sitemap is written out reserve_name unless @location.create_index == false write_first_sitemap file.write super(SitemapGenerator::Builder::SitemapIndexUrl.new(file, )) end else # A link is being added manually. Obviously the user wants an index. # This overrides the create_index setting. unless @location.create_index == false @create_index = true reserve_name end # Use the host from the location if none provided [:host] ||= @location.host super(SitemapGenerator::Builder::SitemapIndexUrl.new(link, )) end end |
#create_index? ⇒ Boolean
Whether or not we need to create an index file. True if create_index is true or if create_index is :auto and we have more than one link in the index. If a link is added manually and create_index is not false, we force index creation because they obviously intend for there to be an index. False otherwise.
126 127 128 |
# File 'lib/sitemap_generator/builder/sitemap_index_file.rb', line 126 def create_index? @create_index || @location.create_index == true || (@location.create_index == :auto && @link_count > 1) end |
#file_can_fit?(bytes) ⇒ Boolean
Return a boolean indicating whether the sitemap file can fit another link of bytes bytes in size. You can also pass a string and the bytesize will be calculated for you.
94 95 96 97 |
# File 'lib/sitemap_generator/builder/sitemap_index_file.rb', line 94 def file_can_fit?(bytes) bytes = SitemapGenerator::Utilities.bytesize(bytes) if bytes.is_a?(String) (@filesize + bytes) < SitemapGenerator::MAX_SITEMAP_FILESIZE && @link_count < SitemapGenerator::MAX_SITEMAP_FILES end |
#finalize! ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/sitemap_generator/builder/sitemap_index_file.rb', line 109 def finalize! raise SitemapGenerator::SitemapFinalizedError if finalized? reserve_name if create_index? write_first_sitemap @frozen = true end |
#index_url ⇒ Object
Return the index file URL. If create_index is true, this is the URL of the actual index file. If create_index is false, this is the URL of the first sitemap that was written out. Only call this method after the files have been finalized.
134 135 136 137 138 139 140 |
# File 'lib/sitemap_generator/builder/sitemap_index_file.rb', line 134 def index_url if create_index? || !@first_sitemap_url @location.url else @first_sitemap_url end end |
#stats_summary(opts = {}) ⇒ Object
104 105 106 107 |
# File 'lib/sitemap_generator/builder/sitemap_index_file.rb', line 104 def stats_summary(opts = {}) str = "Sitemap stats: #{number_with_delimiter(@sitemaps_link_count)} links / #{@link_count} sitemaps" str + format(' / %dm%02ds', *opts[:time_taken].divmod(60)) if opts[:time_taken] # rubocop:disable Style/FormatStringToken end |
#super_add ⇒ Object
Finalize sitemaps as they are added to the index. If it's the first sitemap, finalize it but don't write it out, because we don't yet know if we need an index. If it's the second sitemap, we know we need an index, so reserve a name for the index, and go and write out the first sitemap. If it's the third or greater sitemap, just finalize and write it out as usual, nothing more needs to be done.
If a link is being added to the index manually as a string, then we can assume that the index is required (unless create_index is false of course). This seems like the logical thing to do.
54 |
# File 'lib/sitemap_generator/builder/sitemap_index_file.rb', line 54 alias super_add add |
#total_link_count ⇒ Object
Return the total number of links in all sitemaps reference by this index file
100 101 102 |
# File 'lib/sitemap_generator/builder/sitemap_index_file.rb', line 100 def total_link_count @sitemaps_link_count end |
#write ⇒ Object
Write out the index if an index is needed
118 119 120 |
# File 'lib/sitemap_generator/builder/sitemap_index_file.rb', line 118 def write super if create_index? end |