Class: SitemapGenerator::SitemapLocation
- Inherits:
-
Hash
- Object
- Hash
- SitemapGenerator::SitemapLocation
- Includes:
- Helpers::NumberHelper
- Defined in:
- lib/sitemap_generator/sitemap_location.rb
Overview
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.
Direct Known Subclasses
Constant Summary collapse
- PATH_OUTPUT_WIDTH =
Character width of the path in the summary lines
47
Constants included from Helpers::NumberHelper
Helpers::NumberHelper::DECIMAL_UNITS, Helpers::NumberHelper::STORAGE_UNITS
Instance Method Summary collapse
-
#[]=(key, value, opts = {}) ⇒ Object
If you set the filename, clear the namer and vice versa.
- #content_type ⇒ Object
-
#directory ⇒ Object
Full path to the directory of the file.
-
#filename ⇒ Object
Return the filename.
-
#filesize ⇒ Object
Return the size of the file at path.
- #gzip? ⇒ Boolean
-
#initialize(opts = {}) ⇒ SitemapLocation
constructor
If no
filenameornameris provided, the default namer is used, which generates names like sitemap.xml.gz, sitemap1.xml.gz, sitemap2.xml.gz and so on. - #namer ⇒ Object
-
#path ⇒ Object
Full path of the file including the filename.
-
#path_in_public ⇒ Object
Relative path of the file (including the filename) relative to public_path.
-
#reserve_name ⇒ Object
If a namer is set, reserve the filename and increment the namer.
-
#reserved_name? ⇒ Boolean
Return true if this location has a fixed filename.
-
#summary(link_count) ⇒ Object
Return a summary string.
-
#url ⇒ Object
Full URL of the file.
- #verbose? ⇒ Boolean
-
#with(opts = {}) ⇒ Object
Return a new Location instance with the given options merged in.
-
#write(data, link_count) ⇒ Object
Write
dataout to a file.
Methods included from Helpers::NumberHelper
#number_to_human_size, #number_with_delimiter, #number_with_precision
Constructor Details
#initialize(opts = {}) ⇒ SitemapLocation
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. 'sitemap1.xml.gz'
- :host - host name for URLs. The full URL to the file is then constructed from the host, sitemaps_path and filename
- :namer - a SitemapGenerator::SimpleNamer instance for generating file names.
Should be passed if no
filenameis provided. - :public_path - path to the "public" directory, or the directory you want to write sitemaps in. Default is a directory public/ in the current working directory, or relative to the Rails root directory if running under Rails.
- :sitemaps_path - gives the path relative to the public_path in which to write sitemaps e.g. sitemaps/.
- :verbose - whether to output summary into to STDOUT. Default
false. - :create_index - whether to create a sitemap index. Default
:auto. See LinkSet::create_index= for possible values. Only applies to the SitemapIndexLocation object. - compress - The LinkSet compress setting. Default:
true. Iffalseany.gzextension is stripped from the filename. If:all_but_first, only the.gzextension of the first filename is stripped off. Iftruethe extensions are left unchanged. - max_sitemap_links - The maximum number of links to put in each sitemap.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 51 def initialize(opts = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength super() SitemapGenerator::Utilities.assert_valid_keys(opts, %i[ 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/' # rubocop:disable Style/StringConcatenation # 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. opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap, start: 2, zero: 1) if !opts[:filename] && !opts[:namer] opts[:verbose] = !!opts[:verbose] # rubocop:disable Style/DoubleNegation merge!(opts) end |
Instance Method Details
#[]=(key, value, opts = {}) ⇒ Object
If you set the filename, clear the namer and vice versa.
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 162 def []=(key, value, opts = {}) unless opts[:super] case key when :namer super(:filename, nil) when :filename super(:namer, nil) end end super(key, value) end |
#content_type ⇒ Object
111 112 113 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 111 def content_type gzip? ? 'application/x-gzip' : 'application/xml' end |
#directory ⇒ Object
Full path to the directory of the file.
83 84 85 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 83 def directory (public_path + sitemaps_path)..to_s end |
#filename ⇒ Object
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.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 118 def filename # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity raise SitemapGenerator::SitemapError, 'No filename or namer set' unless self[:filename] || self[:namer] unless self[:filename] 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]&.start? && self[:compress] == :all_but_first) self[:filename].gsub!(/\.gz$/, '') end end self[:filename] end |
#filesize ⇒ Object
Return the size of the file at path
103 104 105 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 103 def filesize # rubocop:disable Naming/PredicateMethod File.size?(path) end |
#gzip? ⇒ Boolean
107 108 109 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 107 def gzip? /\.gz$/.match?(filename.to_s) end |
#namer ⇒ Object
153 154 155 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 153 def namer self[:namer] end |
#path ⇒ Object
Full path of the file including the filename.
88 89 90 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 88 def path (public_path + sitemaps_path + filename)..to_s end |
#path_in_public ⇒ Object
Relative path of the file (including the filename) relative to public_path
93 94 95 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 93 def path_in_public (sitemaps_path + filename).to_s end |
#reserve_name ⇒ Object
If a namer is set, reserve the filename and increment the namer. Returns the reserved name.
139 140 141 142 143 144 145 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 139 def reserve_name if self[:namer] filename self[:namer].next end self[:filename] end |
#reserved_name? ⇒ Boolean
Return true if this location has a fixed filename. If no name has been reserved from the namer, for instance, returns false.
149 150 151 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 149 def reserved_name? !!self[:filename] end |
#summary(link_count) ⇒ Object
Return a summary string
182 183 184 185 186 187 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 182 def summary(link_count) filesize = number_to_human_size(self.filesize) width = self.class::PATH_OUTPUT_WIDTH path = SitemapGenerator::Utilities.ellipsis(path_in_public, width) "+ #{format("%-#{width}s", path)} #{format('%10s', link_count)} links / #{format('%10s', filesize)}" end |
#url ⇒ Object
Full URL of the file.
98 99 100 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 98 def url URI.join(host, sitemaps_path.to_s, filename.to_s).to_s end |
#verbose? ⇒ Boolean
157 158 159 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 157 def verbose? self[:verbose] end |
#with(opts = {}) ⇒ Object
Return a new Location instance with the given options merged in
78 79 80 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 78 def with(opts = {}) merge(opts) end |
#write(data, link_count) ⇒ Object
Write data out to a file.
Output a summary line if verbose is true.
176 177 178 179 |
# File 'lib/sitemap_generator/sitemap_location.rb', line 176 def write(data, link_count) adapter.write(self, data) puts summary(link_count) if verbose? end |