sitemap_generator 7.0.3 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +73 -64
  3. data/README.md +142 -143
  4. data/VERSION +1 -1
  5. data/lib/capistrano/sitemap_generator.rb +2 -0
  6. data/lib/sitemap_generator/adapters/active_storage_adapter.rb +15 -10
  7. data/lib/sitemap_generator/adapters/aws_sdk_adapter.rb +13 -12
  8. data/lib/sitemap_generator/adapters/file_adapter.rb +10 -13
  9. data/lib/sitemap_generator/adapters/fog_adapter.rb +2 -2
  10. data/lib/sitemap_generator/adapters/google_storage_adapter.rb +5 -4
  11. data/lib/sitemap_generator/adapters/s3_adapter.rb +15 -14
  12. data/lib/sitemap_generator/adapters/wave_adapter.rb +5 -5
  13. data/lib/sitemap_generator/application.rb +5 -3
  14. data/lib/sitemap_generator/builder/sitemap_file.rb +17 -14
  15. data/lib/sitemap_generator/builder/sitemap_index_file.rb +20 -18
  16. data/lib/sitemap_generator/builder/sitemap_index_url.rb +5 -6
  17. data/lib/sitemap_generator/builder/sitemap_url.rb +58 -34
  18. data/lib/sitemap_generator/builder.rb +4 -2
  19. data/lib/sitemap_generator/core_ext/big_decimal.rb +38 -33
  20. data/lib/sitemap_generator/core_ext/numeric.rb +50 -46
  21. data/lib/sitemap_generator/helpers/number_helper.rb +52 -46
  22. data/lib/sitemap_generator/interpreter.rb +11 -15
  23. data/lib/sitemap_generator/link_set.rb +63 -65
  24. data/lib/sitemap_generator/railtie.rb +1 -0
  25. data/lib/sitemap_generator/simple_namer.rb +3 -4
  26. data/lib/sitemap_generator/sitemap_location.rb +44 -36
  27. data/lib/sitemap_generator/tasks.rb +1 -1
  28. data/lib/sitemap_generator/templates.rb +6 -7
  29. data/lib/sitemap_generator/utilities.rb +25 -17
  30. data/lib/sitemap_generator.rb +22 -14
  31. data/lib/tasks/sitemap_generator_tasks.rake +2 -0
  32. data/rails/install.rb +2 -0
  33. data/rails/uninstall.rb +2 -0
  34. data/templates/sitemap.rb +2 -0
  35. metadata +2 -2
@@ -5,9 +5,12 @@ require 'builder'
5
5
  # A LinkSet provisions a bunch of links to sitemap files. It also writes the index file
6
6
  # which lists all the sitemap files written.
7
7
  module SitemapGenerator
8
- class LinkSet
9
- @@requires_finalization_opts = %i[filename sitemaps_path sitemaps_host namer]
10
- @@new_location_opts = %i[filename sitemaps_path namer]
8
+ # Top-level configuration and orchestration object for a sitemap generation run.
9
+ # Owns the adapter, host, paths, and namer; manages the lifecycle of sitemap files
10
+ # from creation through finalization. See +SitemapGenerator::Sitemap+ for the public API.
11
+ class LinkSet # rubocop:disable Metrics/ClassLength
12
+ REQUIRES_FINALIZATION_OPTS = %i[filename sitemaps_path sitemaps_host namer].freeze
13
+ NEW_LOCATION_OPTS = %i[filename sitemaps_path namer].freeze
11
14
 
12
15
  attr_reader :default_host, :sitemaps_path, :filename, :create_index
13
16
  attr_accessor :include_root, :include_index, :adapter, :yield_sitemap, :max_sitemap_links
@@ -119,25 +122,26 @@ module SitemapGenerator
119
122
  #
120
123
  # Note: When adding a new option be sure to include it in `options_for_group()` if
121
124
  # the option should be inherited by groups.
122
- def initialize(options = {})
123
- @default_host, @sitemaps_host, @yield_sitemap, @sitemaps_path, @adapter, @verbose, @protect_index, @sitemap_index, @added_default_links, @created_group, @sitemap = nil
125
+ def initialize(options = {}) # rubocop:disable Metrics/MethodLength
126
+ @default_host, @sitemaps_host, @yield_sitemap, @sitemaps_path,
127
+ @adapter, @verbose, @protect_index, @sitemap_index,
128
+ @added_default_links, @created_group, @sitemap = nil
124
129
 
125
130
  options = SitemapGenerator::Utilities.reverse_merge(options,
126
- include_root: true,
127
- include_index: false,
128
- filename: :sitemap,
129
- search_engines: {},
130
- create_index: :auto,
131
- compress: true,
132
- max_sitemap_links: SitemapGenerator::MAX_SITEMAP_LINKS
133
- )
131
+ include_root: true,
132
+ include_index: false,
133
+ filename: :sitemap,
134
+ search_engines: {},
135
+ create_index: :auto,
136
+ compress: true,
137
+ max_sitemap_links: SitemapGenerator::MAX_SITEMAP_LINKS)
134
138
  options.each_pair { |k, v| instance_variable_set(:"@#{k}", v) }
135
139
 
136
140
  # If an index is passed in, protect it from modification.
137
141
  # Sitemaps can be added to the index but nothing else can be changed.
138
- if options[:sitemap_index]
139
- @protect_index = true
140
- end
142
+ return unless options[:sitemap_index]
143
+
144
+ @protect_index = true
141
145
  end
142
146
 
143
147
  # Add a link to a Sitemap. If a new Sitemap is required, one will be created for
@@ -198,11 +202,11 @@ module SitemapGenerator
198
202
  @created_group = true
199
203
  original_opts = opts.dup
200
204
 
201
- if (@@requires_finalization_opts & original_opts.keys).empty?
205
+ if (REQUIRES_FINALIZATION_OPTS & original_opts.keys).empty?
202
206
  # If no new filename or path is specified reuse the default sitemap file.
203
207
  # A new location object will be set on it for the duration of the group.
204
208
  original_opts[:sitemap] = sitemap
205
- elsif original_opts.key?(:sitemaps_host) && (@@new_location_opts & original_opts.keys).empty?
209
+ elsif original_opts.key?(:sitemaps_host) && (NEW_LOCATION_OPTS & original_opts.keys).empty?
206
210
  # If no location options are provided we are creating the next sitemap in the
207
211
  # current series, so finalize and inherit the namer.
208
212
  finalize_sitemap!
@@ -294,14 +298,10 @@ module SitemapGenerator
294
298
  name = Utilities.titleize(engine.to_s)
295
299
  begin
296
300
  Timeout.timeout(10) do
297
- if URI.respond_to?(:open) # Available since Ruby 2.5
298
- URI.open(link)
299
- else
300
- open(link) # using Kernel#open became deprecated since Ruby 2.7. See https://bugs.ruby-lang.org/issues/15893
301
- end
301
+ URI.open(link) # rubocop:disable Security/Open
302
302
  end
303
303
  output(" Successful ping of #{name}")
304
- rescue Timeout::Error, StandardError => e
304
+ rescue StandardError => e
305
305
  output("Ping failed for #{name}: #{e.inspect} (URL #{link})")
306
306
  end
307
307
  end
@@ -366,9 +366,7 @@ module SitemapGenerator
366
366
  # By default verbose is true. When running rake tasks, pass the <tt>-s</tt>
367
367
  # option to rake to turn verbose off.
368
368
  def verbose
369
- if @verbose.nil?
370
- @verbose = SitemapGenerator.verbose.nil? ? true : SitemapGenerator.verbose
371
- end
369
+ @verbose = SitemapGenerator.verbose.nil? || SitemapGenerator.verbose if @verbose.nil?
372
370
  @verbose
373
371
  end
374
372
 
@@ -401,10 +399,9 @@ module SitemapGenerator
401
399
  # change the public path.
402
400
  def options_for_group(opts) # rubocop:disable Metrics/MethodLength
403
401
  opts = SitemapGenerator::Utilities.reverse_merge(opts,
404
- include_index: false,
405
- include_root: false,
406
- sitemap_index: sitemap_index
407
- )
402
+ include_index: false,
403
+ include_root: false,
404
+ sitemap_index: sitemap_index)
408
405
  opts.delete(:public_path)
409
406
 
410
407
  # Reverse merge the current settings.
@@ -412,22 +409,21 @@ module SitemapGenerator
412
409
  # This hash could be a problem because it needs to be maintained
413
410
  # when new options are added, but can easily be missed. We really could
414
411
  # do with a separate SitemapOptions class.
415
- current_settings = [
416
- :include_root,
417
- :include_index,
418
- :sitemaps_path,
419
- :public_path,
420
- :sitemaps_host,
421
- :verbose,
422
- :default_host,
423
- :adapter,
424
- :create_index,
425
- :compress,
426
- :max_sitemap_links
427
- ].inject({}) do |hash, key|
412
+ current_settings = %i[
413
+ include_root
414
+ include_index
415
+ sitemaps_path
416
+ public_path
417
+ sitemaps_host
418
+ verbose
419
+ default_host
420
+ adapter
421
+ create_index
422
+ compress
423
+ max_sitemap_links
424
+ ].each_with_object({}) do |key, hash|
428
425
  value = instance_variable_get(:"@#{key}")
429
426
  hash[key] = value unless value.nil?
430
- hash
431
427
  end
432
428
  SitemapGenerator::Utilities.reverse_merge!(opts, current_settings)
433
429
  opts
@@ -438,12 +434,10 @@ module SitemapGenerator
438
434
  def add_default_links
439
435
  @added_default_links = true
440
436
  link_options = { lastmod: Time.now, priority: 1.0 }
441
- if include_root?
442
- add('/', link_options)
443
- end
444
- if include_index?
445
- add(sitemap_index, link_options)
446
- end
437
+ add('/', link_options) if include_root?
438
+ return unless include_index?
439
+
440
+ add(sitemap_index, link_options)
447
441
  end
448
442
 
449
443
  # Finalize a sitemap by including it in the index and outputting a summary line.
@@ -485,9 +479,9 @@ module SitemapGenerator
485
479
  # Reset this instance. Keep the same options, but return to the same state
486
480
  # as before any sitemaps were created.
487
481
  def reset!
488
- @sitemap_index = nil if @sitemap_index && @sitemap_index.finalized? && !@protect_index
489
- @sitemap = nil if @sitemap && @sitemap.finalized?
490
- self.namer.reset
482
+ @sitemap_index = nil if @sitemap_index&.finalized? && !@protect_index
483
+ @sitemap = nil if @sitemap&.finalized?
484
+ namer.reset
491
485
  @added_default_links = false
492
486
  end
493
487
 
@@ -499,9 +493,9 @@ module SitemapGenerator
499
493
  puts string
500
494
  end
501
495
 
496
+ # Setters that propagate location configuration (host, paths, adapter) to the
497
+ # active sitemap and index files whenever a value changes on the LinkSet.
502
498
  module LocationHelpers
503
- public
504
-
505
499
  # Set the host name, including protocol, that will be used by default on each
506
500
  # of your sitemap links. You can pass a different host in your options to `add`
507
501
  # if you need to change it on a per-link basis.
@@ -519,16 +513,14 @@ module SitemapGenerator
519
513
  # Set to nil to use the current directory.
520
514
  def public_path=(value)
521
515
  @public_path = Pathname.new(SitemapGenerator::Utilities.append_slash(value))
522
- if @public_path.relative?
523
- @public_path = SitemapGenerator.app.root + @public_path
524
- end
516
+ @public_path = SitemapGenerator.app.root + @public_path if @public_path.relative?
525
517
  update_location_info(:public_path, @public_path)
526
- @public_path
518
+ @public_path # rubocop:disable Lint/Void -- return value used by send(:public_path=) in the getter
527
519
  end
528
520
 
529
521
  # Return a Pathname with the full path to the public directory
530
522
  def public_path
531
- @public_path ||= self.send(:public_path=, 'public/')
523
+ @public_path ||= send(:public_path=, 'public/')
532
524
  end
533
525
 
534
526
  # Set the sitemaps_path. This path gives the location to write sitemaps to
@@ -612,12 +604,15 @@ module SitemapGenerator
612
604
  # are in your sitemap. If `false` an index file is never created.
613
605
  # If `:auto` an index file is created only if your sitemap has more than
614
606
  # one sitemap file.
615
- def create_index=(value, force = false)
607
+ def create_index=(value, force = false) # rubocop:disable Style/OptionalBooleanParameter
616
608
  @create_index = value
617
609
  # Allow overriding the protected status of the index when we are creating a group.
618
610
  # Because sometimes we need to force an index in that case. But generally we don't
619
611
  # want to allow people to mess with this value if the index is protected.
620
- @sitemap_index.location[:create_index] = value if @sitemap_index && ((!@sitemap_index.finalized? && !@protect_index) || force)
612
+ return unless @sitemap_index && ((!@sitemap_index.finalized? && !@protect_index) || force)
613
+
614
+ @sitemap_index.location[:create_index] =
615
+ value
621
616
  end
622
617
 
623
618
  # Set the namer to use to generate the sitemap (and index) file names.
@@ -632,7 +627,7 @@ module SitemapGenerator
632
627
  # the current sitemap and if there is no sitemap, creates a new one using
633
628
  # the current filename.
634
629
  def namer
635
- @namer ||= @sitemap && @sitemap.location.namer || SitemapGenerator::SimpleNamer.new(@filename)
630
+ @namer ||= @sitemap&.location&.namer || SitemapGenerator::SimpleNamer.new(@filename)
636
631
  end
637
632
 
638
633
  # Set the value of the compress setting.
@@ -663,7 +658,10 @@ module SitemapGenerator
663
658
  # But don't create the index or sitemap files yet if they are not already created.
664
659
  def update_location_info(attribute, value, opts = {})
665
660
  opts = SitemapGenerator::Utilities.reverse_merge(opts, include_index: !@protect_index)
666
- @sitemap_index.location[attribute] = value if opts[:include_index] && @sitemap_index && !@sitemap_index.finalized?
661
+ if opts[:include_index] && @sitemap_index && !@sitemap_index.finalized?
662
+ @sitemap_index.location[attribute] =
663
+ value
664
+ end
667
665
  @sitemap.location[attribute] = value if @sitemap && !@sitemap.finalized?
668
666
  end
669
667
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SitemapGenerator
4
+ # Loads sitemap_generator rake tasks automatically when the gem is used in a Rails app.
4
5
  class Railtie < Rails::Railtie
5
6
  rake_tasks do
6
7
  load 'tasks/sitemap_generator_tasks.rake'
@@ -30,10 +30,9 @@ module SitemapGenerator
30
30
  class SimpleNamer
31
31
  def initialize(base, options = {})
32
32
  @options = SitemapGenerator::Utilities.reverse_merge(options,
33
- zero: nil, # identifies the marker for the start of the series
34
- extension: '.xml.gz',
35
- start: 1
36
- )
33
+ zero: nil, # marker for the start of the series
34
+ extension: '.xml.gz',
35
+ start: 1)
37
36
  @base = base
38
37
  reset
39
38
  end
@@ -6,12 +6,12 @@ module SitemapGenerator
6
6
  # A class for determining the exact location at which to write sitemap data.
7
7
  # Handles reserving filenames from namers, constructing paths and sending
8
8
  # data to the adapter to be written out.
9
- class SitemapLocation < Hash
9
+ class SitemapLocation < Hash # rubocop:disable Metrics/ClassLength
10
10
  include SitemapGenerator::Helpers::NumberHelper
11
11
 
12
12
  PATH_OUTPUT_WIDTH = 47 # Character width of the path in the summary lines
13
13
 
14
- [:host, :adapter].each do |method|
14
+ %i[host adapter].each do |method|
15
15
  define_method(method) do
16
16
  raise SitemapGenerator::SitemapError, "No value set for #{method}" unless self[method]
17
17
 
@@ -19,7 +19,7 @@ module SitemapGenerator
19
19
  end
20
20
  end
21
21
 
22
- [:public_path, :sitemaps_path].each do |method|
22
+ %i[public_path sitemaps_path].each do |method|
23
23
  define_method(method) do
24
24
  Pathname.new(SitemapGenerator::Utilities.append_slash(self[method]))
25
25
  end
@@ -48,36 +48,35 @@ module SitemapGenerator
48
48
  # stripped from the filename. If `:all_but_first`, only the `.gz` extension of the first
49
49
  # filename is stripped off. If `true` the extensions are left unchanged.
50
50
  # * <tt>max_sitemap_links</tt> - The maximum number of links to put in each sitemap.
51
- def initialize(opts = {})
52
- SitemapGenerator::Utilities.assert_valid_keys(opts, [
53
- :adapter,
54
- :public_path,
55
- :sitemaps_path,
56
- :host,
57
- :filename,
58
- :namer,
59
- :verbose,
60
- :create_index,
61
- :compress,
62
- :max_sitemap_links
63
- ])
51
+ def initialize(opts = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
52
+ super()
53
+ SitemapGenerator::Utilities.assert_valid_keys(opts, %i[
54
+ adapter
55
+ public_path
56
+ sitemaps_path
57
+ host
58
+ filename
59
+ namer
60
+ verbose
61
+ create_index
62
+ compress
63
+ max_sitemap_links
64
+ ])
64
65
  opts[:adapter] ||= SitemapGenerator::FileAdapter.new
65
- opts[:public_path] ||= SitemapGenerator.app.root + 'public/'
66
+ opts[:public_path] ||= SitemapGenerator.app.root + 'public/' # rubocop:disable Style/StringConcatenation
66
67
  # This is a bit of a hack to make the SimpleNamer act like the old SitemapNamer.
67
68
  # It doesn't really make sense to create a default namer like this because the
68
69
  # namer instance should be shared by the location objects of the sitemaps and
69
70
  # sitemap index files. However, this greatly eases testing, so I'm leaving it in
70
71
  # for now.
71
- if !opts[:filename] && !opts[:namer]
72
- opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap, start: 2, zero: 1)
73
- end
74
- opts[:verbose] = !!opts[:verbose]
75
- self.merge!(opts)
72
+ opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap, start: 2, zero: 1) if !opts[:filename] && !opts[:namer]
73
+ opts[:verbose] = !!opts[:verbose] # rubocop:disable Style/DoubleNegation
74
+ merge!(opts)
76
75
  end
77
76
 
78
77
  # Return a new Location instance with the given options merged in
79
78
  def with(opts = {})
80
- self.merge(opts)
79
+ merge(opts)
81
80
  end
82
81
 
83
82
  # Full path to the directory of the file.
@@ -101,18 +100,26 @@ module SitemapGenerator
101
100
  end
102
101
 
103
102
  # Return the size of the file at <tt>path</tt>
104
- def filesize
103
+ def filesize # rubocop:disable Naming/PredicateMethod
105
104
  File.size?(path)
106
105
  end
107
106
 
107
+ def gzip?
108
+ /\.gz$/.match?(filename.to_s)
109
+ end
110
+
111
+ def content_type
112
+ gzip? ? 'application/x-gzip' : 'application/xml'
113
+ end
114
+
108
115
  # Return the filename. Raises an exception if no filename or namer is set.
109
116
  # If using a namer once the filename has been retrieved from the namer its
110
117
  # value is locked so that it is unaffected by further changes to the namer.
111
- def filename
118
+ def filename # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
112
119
  raise SitemapGenerator::SitemapError, 'No filename or namer set' unless self[:filename] || self[:namer]
113
120
 
114
121
  unless self[:filename]
115
- self.send(:[]=, :filename, +self[:namer].to_s, super: true)
122
+ send(:[]=, :filename, +self[:namer].to_s, super: true)
116
123
 
117
124
  # Post-process the filename for our compression settings.
118
125
  # Strip the `.gz` from the extension if we aren't compressing this file.
@@ -120,7 +127,7 @@ module SitemapGenerator
120
127
  # expected. Ultimately I should force using a namer in all circumstances.
121
128
  # Changing the filename here will affect how the FileAdapter writes out the file.
122
129
  if self[:compress] == false ||
123
- (self[:namer] && self[:namer].start? && self[:compress] == :all_but_first)
130
+ (self[:namer]&.start? && self[:compress] == :all_but_first)
124
131
  self[:filename].gsub!(/\.gz$/, '')
125
132
  end
126
133
  end
@@ -153,7 +160,7 @@ module SitemapGenerator
153
160
 
154
161
  # If you set the filename, clear the namer and vice versa.
155
162
  def []=(key, value, opts = {})
156
- if !opts[:super]
163
+ unless opts[:super]
157
164
  case key
158
165
  when :namer
159
166
  super(:filename, nil)
@@ -175,17 +182,18 @@ module SitemapGenerator
175
182
  def summary(link_count)
176
183
  filesize = number_to_human_size(self.filesize)
177
184
  width = self.class::PATH_OUTPUT_WIDTH
178
- path = SitemapGenerator::Utilities.ellipsis(self.path_in_public, width)
179
- "+ #{('%-' + width.to_s + 's') % path} #{'%10s' % link_count} links / #{'%10s' % filesize}"
185
+ path = SitemapGenerator::Utilities.ellipsis(path_in_public, width)
186
+ "+ #{format("%-#{width}s", path)} #{format('%10s', link_count)} links / #{format('%10s', filesize)}"
180
187
  end
181
188
  end
182
189
 
190
+ # SitemapLocation subclass for the sitemap index file.
191
+ # Defaults the namer to +:sitemap+ (no numeric suffix on the first name) and
192
+ # exposes the +create_index+ option.
183
193
  class SitemapIndexLocation < SitemapLocation
184
194
  def initialize(opts = {})
185
- if !opts[:filename] && !opts[:namer]
186
- opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap)
187
- end
188
- super(opts)
195
+ opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap) if !opts[:filename] && !opts[:namer]
196
+ super
189
197
  end
190
198
 
191
199
  # Whether to create a sitemap index. Default `:auto`. See <tt>LinkSet::create_index=</tt>
@@ -201,8 +209,8 @@ module SitemapGenerator
201
209
  def summary(link_count)
202
210
  filesize = number_to_human_size(self.filesize)
203
211
  width = self.class::PATH_OUTPUT_WIDTH - 3
204
- path = SitemapGenerator::Utilities.ellipsis(self.path_in_public, width)
205
- "+ #{('%-' + width.to_s + 's') % path} #{'%10s' % link_count} sitemaps / #{'%10s' % filesize}"
212
+ path = SitemapGenerator::Utilities.ellipsis(path_in_public, width)
213
+ "+ #{format("%-#{width}s", path)} #{format('%10s', link_count)} sitemaps / #{format('%10s', filesize)}"
206
214
  end
207
215
  end
208
216
  end
@@ -36,6 +36,6 @@ namespace :sitemap do
36
36
 
37
37
  desc "Generate sitemaps but don't ping search engines. Alias for refresh:no_ping."
38
38
  task create: :require do
39
- SitemapGenerator::Interpreter.run(config_file: ENV['CONFIG_FILE'], verbose: verbose)
39
+ SitemapGenerator::Interpreter.run(config_file: ENV.fetch('CONFIG_FILE', nil), verbose: verbose)
40
40
  end
41
41
  end
@@ -9,18 +9,17 @@ module SitemapGenerator
9
9
  # Define an accessor method for each template file.
10
10
  class Templates
11
11
  FILES = {
12
- sitemap_sample: 'sitemap.rb',
13
- }
12
+ sitemap_sample: 'sitemap.rb'
13
+ }.freeze
14
14
 
15
15
  # Dynamically define accessors for each key defined in <tt>FILES</tt>
16
16
  attr_writer(*FILES.keys)
17
17
 
18
18
  FILES.each_key do |name|
19
- eval(<<-ACCESSOR, binding, __FILE__, __LINE__ + 1)
20
- define_method(:#{name}) do
21
- @#{name} ||= read_template(:#{name})
22
- end
23
- ACCESSOR
19
+ define_method(name) do
20
+ ivar = :"@#{name}"
21
+ instance_variable_get(ivar) || instance_variable_set(ivar, read_template(name))
22
+ end
24
23
  end
25
24
 
26
25
  def initialize(root = SitemapGenerator.root)
@@ -1,31 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SitemapGenerator
4
- module Utilities
5
- extend self
4
+ # Internal utility functions: hash merging, string manipulation, byte calculation,
5
+ # and warning suppression. Not part of the public API.
6
+ module Utilities # rubocop:disable Metrics/ModuleLength
7
+ module_function
6
8
 
7
9
  # Copy templates/sitemap.rb to config if not there yet.
8
- def install_sitemap_rb(verbose = false)
9
- if File.exist?(SitemapGenerator.app.root + 'config/sitemap.rb')
10
+ def install_sitemap_rb(verbose = false) # rubocop:disable Style/OptionalBooleanParameter
11
+ if File.exist?(SitemapGenerator.app.root + 'config/sitemap.rb') # rubocop:disable Style/StringConcatenation
10
12
  puts 'already exists: config/sitemap.rb, file not copied' if verbose
11
13
  else
12
14
  FileUtils.cp(
13
15
  SitemapGenerator.templates.template_path(:sitemap_sample),
14
- SitemapGenerator.app.root + 'config/sitemap.rb')
16
+ SitemapGenerator.app.root + 'config/sitemap.rb' # rubocop:disable Style/StringConcatenation
17
+ )
15
18
  puts 'created: config/sitemap.rb' if verbose
16
19
  end
17
20
  end
18
21
 
19
22
  # Remove config/sitemap.rb if exists.
20
23
  def uninstall_sitemap_rb
21
- if File.exist?(SitemapGenerator.app.root + 'config/sitemap.rb')
22
- File.rm(SitemapGenerator.app.root + 'config/sitemap.rb')
23
- end
24
+ return unless File.exist?(SitemapGenerator.app.root + 'config/sitemap.rb') # rubocop:disable Style/StringConcatenation
25
+
26
+ File.rm(SitemapGenerator.app.root + 'config/sitemap.rb') # rubocop:disable Style/StringConcatenation
24
27
  end
25
28
 
26
29
  # Clean sitemap files in output directory.
27
30
  def clean_files
28
- FileUtils.rm(Dir[SitemapGenerator.app.root + 'public/sitemap*.xml.gz'])
31
+ FileUtils.rm(Dir[SitemapGenerator.app.root + 'public/sitemap*.xml.gz']) # rubocop:disable Style/StringConcatenation
29
32
  end
30
33
 
31
34
  # Validate all keys in a hash match *valid keys, raising ArgumentError on a
@@ -46,7 +49,11 @@ module SitemapGenerator
46
49
  # to +to_sym+.
47
50
  def symbolize_keys!(hash)
48
51
  hash.keys.each do |key|
49
- hash[(key.to_sym rescue key) || key] = hash.delete(key)
52
+ hash[begin
53
+ key.to_sym
54
+ rescue StandardError
55
+ key
56
+ end || key] = hash.delete(key)
50
57
  end
51
58
  hash
52
59
  end
@@ -97,10 +104,10 @@ module SitemapGenerator
97
104
  other_hash.merge(hash)
98
105
  end
99
106
 
100
- # Performs the opposite of <tt>merge</tt>, with the keys and values from the first hash taking precedence over the second.
101
- # Modifies the receiver in place.
107
+ # Performs the opposite of <tt>merge</tt>, with the keys and values from the first hash taking
108
+ # precedence over the second. Modifies the receiver in place.
102
109
  def reverse_merge!(hash, other_hash)
103
- hash.merge!(other_hash) { |k, o, n| o }
110
+ hash.merge!(other_hash) { |_k, o, _n| o }
104
111
  end
105
112
 
106
113
  # An object is blank if it's false, empty, or a whitespace string.
@@ -135,7 +142,8 @@ module SitemapGenerator
135
142
 
136
143
  # Sets $VERBOSE for the duration of the block and back to its original value afterwards.
137
144
  def with_warnings(flag)
138
- old_verbose, $VERBOSE = $VERBOSE, flag # rubocop:disable Style/ParallelAssignment
145
+ old_verbose = $VERBOSE
146
+ $VERBOSE = flag
139
147
  yield
140
148
  ensure
141
149
  $VERBOSE = old_verbose
@@ -143,7 +151,7 @@ module SitemapGenerator
143
151
 
144
152
  def titleize(string)
145
153
  result = string.tr('_', ' ')
146
- result.gsub!(/\b\w/) { |match| match.upcase }
154
+ result.gsub!(/\b\w/, &:upcase)
147
155
  result
148
156
  end
149
157
 
@@ -160,7 +168,7 @@ module SitemapGenerator
160
168
  def append_slash(path)
161
169
  strpath = path.to_s
162
170
  if !strpath[-1].nil? && strpath[-1].chr != '/'
163
- strpath + '/'
171
+ "#{strpath}/"
164
172
  else
165
173
  strpath
166
174
  end
@@ -170,7 +178,7 @@ module SitemapGenerator
170
178
  # or bigger than max.
171
179
  def ellipsis(string, max)
172
180
  if string.size > max
173
- (string[0, max - 3] || '') + '...'
181
+ "#{string[0, max - 3] || ''}..."
174
182
  else
175
183
  string
176
184
  end
@@ -8,6 +8,8 @@ require 'sitemap_generator/utilities'
8
8
  require 'sitemap_generator/application'
9
9
  require 'sitemap_generator/sitemap_location'
10
10
 
11
+ # Top-level namespace for the gem. +SitemapGenerator::Sitemap+ is the public entry point;
12
+ # adapters, builders, and utilities are nested here.
11
13
  module SitemapGenerator
12
14
  autoload(:Interpreter, 'sitemap_generator/interpreter')
13
15
  autoload(:FileAdapter, 'sitemap_generator/adapters/file_adapter')
@@ -20,24 +22,30 @@ module SitemapGenerator
20
22
  autoload(:BigDecimal, 'sitemap_generator/core_ext/big_decimal')
21
23
  autoload(:Numeric, 'sitemap_generator/core_ext/numeric')
22
24
 
23
- SitemapError = Class.new(StandardError)
24
- SitemapFullError = Class.new(SitemapError)
25
- SitemapFinalizedError = Class.new(SitemapError)
25
+ class SitemapError < StandardError
26
+ end
27
+
28
+ class SitemapFullError < SitemapError
29
+ end
30
+
31
+ class SitemapFinalizedError < SitemapError
32
+ end
26
33
 
27
- Utilities.with_warnings(nil) do
28
- VERSION = File.read(File.dirname(__FILE__) + '/../VERSION').strip
34
+ Utilities.with_warnings(nil) do # rubocop:disable Metrics/BlockLength
35
+ # rubocop:disable Lint/ConstantDefinitionInBlock
36
+ VERSION = File.read("#{File.dirname(__FILE__)}/../VERSION").strip
29
37
  MAX_SITEMAP_FILES = 50_000 # max sitemap links per index file
30
38
  MAX_SITEMAP_LINKS = 50_000 # max links per sitemap
31
39
  MAX_SITEMAP_IMAGES = 1_000 # max images per url
32
40
  MAX_SITEMAP_NEWS = 1_000 # max news sitemap per index_file
33
41
  MAX_SITEMAP_FILESIZE = 50_000_000 # bytes
34
42
  SCHEMAS = {
35
- 'image' => 'http://www.google.com/schemas/sitemap-image/1.1',
36
- 'mobile' => 'http://www.google.com/schemas/sitemap-mobile/1.0',
37
- 'news' => 'http://www.google.com/schemas/sitemap-news/0.9',
43
+ 'image' => 'http://www.google.com/schemas/sitemap-image/1.1',
44
+ 'mobile' => 'http://www.google.com/schemas/sitemap-mobile/1.0',
45
+ 'news' => 'http://www.google.com/schemas/sitemap-news/0.9',
38
46
  'pagemap' => 'http://www.google.com/schemas/sitemap-pagemap/1.0',
39
- 'video' => 'http://www.google.com/schemas/sitemap-video/1.1'
40
- }
47
+ 'video' => 'http://www.google.com/schemas/sitemap-video/1.1'
48
+ }.freeze
41
49
 
42
50
  # Lazy-initialize the LinkSet instance
43
51
  Sitemap = (Config = Class.new do
@@ -57,6 +65,7 @@ module SitemapGenerator
57
65
  (@link_set ||= reset!).respond_to?(name, include_private) || super
58
66
  end
59
67
  end).new
68
+ # rubocop:enable Lint/ConstantDefinitionInBlock
60
69
  end
61
70
 
62
71
  class << self
@@ -73,8 +82,6 @@ module SitemapGenerator
73
82
  true
74
83
  elsif SitemapGenerator::Utilities.falsy?(ENV['VERBOSE'])
75
84
  false
76
- else
77
- nil
78
85
  end
79
86
  else
80
87
  @verbose
@@ -86,8 +93,9 @@ module SitemapGenerator
86
93
  !!@yield_sitemap
87
94
  end
88
95
 
89
- self.root = File.expand_path(File.join(File.dirname(__FILE__), '../')) # Root of the install dir, not the Rails app
90
- self.templates = SitemapGenerator::Templates.new(self.root)
96
+ # Root of the install dir, not the Rails app
97
+ self.root = File.expand_path(File.join(File.dirname(__FILE__), '../'))
98
+ self.templates = SitemapGenerator::Templates.new(root)
91
99
  self.app = SitemapGenerator::Application.new
92
100
  end
93
101
 
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  load(File.expand_path(File.join(File.dirname(__FILE__), '../sitemap_generator/tasks.rb')))
data/rails/install.rb CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Install hook code here
2
4
  SitemapGenerator::Utilities.install_sitemap_rb