class SitemapGenerator::SitemapLocation
A class for determining the exact location at which to write sitemap data. Handles reserving filenames from namers, constructing paths and sending data to the adapter to be written out.
Constants
- PATH_OUTPUT_WIDTH
Public Class Methods
If no filename
or namer
is provided, the default namer is used, which generates names like sitemap.xml.gz
, sitemap1.xml.gz
, sitemap2.xml.gz
and so on.
Options¶ ↑
-
:adapter
- SitemapGenerator::Adapter subclass -
:filename
- full name of the file e.g. <tt>'sitemap1.xml.gz'<tt> -
:host
- host name for URLs. The full URL to the file is then constructed from thehost
,sitemaps_path
andfilename
-
:namer
- aSitemapGenerator::SimpleNamer
instance for generating file names. Should be passed if nofilename
is provided. -
:public_path
- path to the “public” directory, or the directory you want to write sitemaps in. Default is a directorypublic/
in the current working directory, or relative to the Rails root directory if running under Rails. -
:sitemaps_path
- gives the path relative to thepublic_path
in which to write sitemaps e.g.sitemaps/
. -
:verbose
- whether to output summary into to STDOUT. Defaultfalse
. -
:create_index
- whether to create a sitemap index. Default `:auto`. SeeLinkSet::create_index=
for possible values. Only applies to theSitemapIndexLocation
object. -
compress
- TheLinkSet
compress setting. Default:true
. If `false` any `.gz` extension is stripped from the filename. If `:all_but_first`, only the `.gz` extension of the first filename is stripped off. If `true` the extensions are left unchanged. -
max_sitemap_links
- The maximum number of links to put in each sitemap.
# File lib/sitemap_generator/sitemap_location.rb, line 48 def initialize(opts={}) SitemapGenerator::Utilities.assert_valid_keys(opts, [ :adapter, :public_path, :sitemaps_path, :host, :filename, :namer, :verbose, :create_index, :compress, :max_sitemap_links ]) opts[:adapter] ||= SitemapGenerator::FileAdapter.new opts[:public_path] ||= SitemapGenerator.app.root + 'public/' # This is a bit of a hack to make the SimpleNamer act like the old SitemapNamer. # It doesn't really make sense to create a default namer like this because the # namer instance should be shared by the location objects of the sitemaps and # sitemap index files. However, this greatly eases testing, so I'm leaving it in # for now. if !opts[:filename] && !opts[:namer] opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap, :start => 2, :zero => 1) end opts[:verbose] = !!opts[:verbose] self.merge!(opts) end
Public Instance Methods
If you set the filename, clear the namer and vice versa.
# File lib/sitemap_generator/sitemap_location.rb, line 151 def []=(key, value, opts={}) if !opts[:super] case key when :namer super(:filename, nil) when :filename super(:namer, nil) end end super(key, value) end
Full path to the directory of the file.
# File lib/sitemap_generator/sitemap_location.rb, line 81 def directory (public_path + sitemaps_path).expand_path.to_s end
Return the filename. Raises an exception if no filename or namer is set. If using a namer once the filename has been retrieved from the namer its value is locked so that it is unaffected by further changes to the namer.
# File lib/sitemap_generator/sitemap_location.rb, line 108 def filename raise SitemapGenerator::SitemapError, "No filename or namer set" unless self[:filename] || self[:namer] unless self[:filename] self.send(:[]=, :filename, self[:namer].to_s, :super => true) # Post-process the filename for our compression settings. # Strip the `.gz` from the extension if we aren't compressing this file. # If you're setting the filename manually, :all_but_first won't work as # expected. Ultimately I should force using a namer in all circumstances. # Changing the filename here will affect how the FileAdapter writes out the file. if self[:compress] == false || (self[:namer] && self[:namer].start? && self[:compress] == :all_but_first) self[:filename].gsub!(/\.gz$/, '') end end self[:filename] end
Return the size of the file at path
# File lib/sitemap_generator/sitemap_location.rb, line 101 def filesize File.size?(path) end
# File lib/sitemap_generator/sitemap_location.rb, line 142 def namer self[:namer] end
Full path of the file including the filename.
# File lib/sitemap_generator/sitemap_location.rb, line 86 def path (public_path + sitemaps_path + filename).expand_path.to_s end
Relative path of the file (including the filename) relative to public_path
# File lib/sitemap_generator/sitemap_location.rb, line 91 def path_in_public (sitemaps_path + filename).to_s end
If a namer is set, reserve the filename and increment the namer. Returns the reserved name.
# File lib/sitemap_generator/sitemap_location.rb, line 128 def reserve_name if self[:namer] filename self[:namer].next end self[:filename] end
Return true if this location has a fixed filename. If no name has been reserved from the namer, for instance, returns false.
# File lib/sitemap_generator/sitemap_location.rb, line 138 def reserved_name? !!self[:filename] end
Return a summary string
# File lib/sitemap_generator/sitemap_location.rb, line 171 def summary(link_count) filesize = number_to_human_size(self.filesize) width = self.class::PATH_OUTPUT_WIDTH path = SitemapGenerator::Utilities.ellipsis(self.path_in_public, width) "+ #{('%-'+width.to_s+'s') % path} #{'%10s' % link_count} links / #{'%10s' % filesize}" end
Full URL of the file.
# File lib/sitemap_generator/sitemap_location.rb, line 96 def url URI.join(host, sitemaps_path.to_s, filename.to_s).to_s end
# File lib/sitemap_generator/sitemap_location.rb, line 146 def verbose? self[:verbose] end
Return a new Location instance with the given options merged in
# File lib/sitemap_generator/sitemap_location.rb, line 76 def with(opts={}) self.merge(opts) end
Write `data` out to a file. Output a summary line if verbose is true.
# File lib/sitemap_generator/sitemap_location.rb, line 165 def write(data, link_count) adapter.write(self, data) puts summary(link_count) if verbose? end