sitemap_generator 6.2.1 → 7.0.1

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 (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +23 -0
  3. data/README.md +43 -37
  4. data/VERSION +1 -1
  5. data/lib/sitemap_generator/adapters/active_storage_adapter.rb +26 -0
  6. data/lib/sitemap_generator/adapters/aws_sdk_adapter.rb +14 -8
  7. data/lib/sitemap_generator/adapters/file_adapter.rb +2 -0
  8. data/lib/sitemap_generator/adapters/fog_adapter.rb +7 -5
  9. data/lib/sitemap_generator/adapters/google_storage_adapter.rb +7 -3
  10. data/lib/sitemap_generator/adapters/s3_adapter.rb +10 -6
  11. data/lib/sitemap_generator/adapters/wave_adapter.rb +3 -1
  12. data/lib/sitemap_generator/application.rb +4 -0
  13. data/lib/sitemap_generator/builder/sitemap_file.rb +17 -12
  14. data/lib/sitemap_generator/builder/sitemap_index_file.rb +12 -9
  15. data/lib/sitemap_generator/builder/sitemap_index_url.rb +6 -4
  16. data/lib/sitemap_generator/builder/sitemap_url.rb +59 -55
  17. data/lib/sitemap_generator/builder.rb +3 -1
  18. data/lib/sitemap_generator/core_ext/big_decimal.rb +2 -0
  19. data/lib/sitemap_generator/core_ext/numeric.rb +2 -0
  20. data/lib/sitemap_generator/core_ext.rb +2 -0
  21. data/lib/sitemap_generator/helpers/number_helper.rb +35 -33
  22. data/lib/sitemap_generator/interpreter.rb +6 -4
  23. data/lib/sitemap_generator/link_set.rb +65 -63
  24. data/lib/sitemap_generator/railtie.rb +3 -1
  25. data/lib/sitemap_generator/simple_namer.rb +8 -5
  26. data/lib/sitemap_generator/sitemap_location.rb +13 -9
  27. data/lib/sitemap_generator/tasks.rb +10 -8
  28. data/lib/sitemap_generator/templates.rb +8 -5
  29. data/lib/sitemap_generator/utilities.rb +12 -9
  30. data/lib/sitemap_generator.rb +13 -9
  31. data/templates/sitemap.rb +1 -1
  32. metadata +6 -130
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'builder'
2
4
 
3
5
  # A LinkSet provisions a bunch of links to sitemap files. It also writes the index file
4
6
  # which lists all the sitemap files written.
5
7
  module SitemapGenerator
6
8
  class LinkSet
7
- @@requires_finalization_opts = [:filename, :sitemaps_path, :sitemaps_host, :namer]
8
- @@new_location_opts = [:filename, :sitemaps_path, :namer]
9
+ @@requires_finalization_opts = %i[filename sitemaps_path sitemaps_host namer]
10
+ @@new_location_opts = %i[filename sitemaps_path namer]
9
11
 
10
12
  attr_reader :default_host, :sitemaps_path, :filename, :create_index
11
13
  attr_accessor :include_root, :include_index, :adapter, :yield_sitemap, :max_sitemap_links
@@ -30,17 +32,17 @@ module SitemapGenerator
30
32
  # If you are calling +create+ more than once in your sitemap configuration file,
31
33
  # make sure that you set a different +sitemaps_path+ or +filename+ for each call otherwise
32
34
  # the sitemaps may be overwritten.
33
- def create(opts={}, &block)
35
+ def create(opts = {}, &block) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
34
36
  reset!
35
37
  set_options(opts)
36
38
  if verbose
37
39
  start_time = Time.now
38
40
  puts "In '#{sitemap_index.location.public_path}':"
39
41
  end
40
- interpreter.eval(:yield_sitemap => yield_sitemap?, &block)
41
- finalize!
42
+ interpreter.eval(yield_sitemap: yield_sitemap?, &block)
43
+ finalize! if block_given?
42
44
  end_time = Time.now if verbose
43
- output(sitemap_index.stats_summary(:time_taken => end_time - start_time)) if verbose
45
+ output(sitemap_index.stats_summary(time_taken: end_time - start_time)) if verbose
44
46
  self
45
47
  end
46
48
 
@@ -117,22 +119,19 @@ module SitemapGenerator
117
119
  #
118
120
  # Note: When adding a new option be sure to include it in `options_for_group()` if
119
121
  # the option should be inherited by groups.
120
- def initialize(options={})
122
+ def initialize(options = {})
121
123
  @default_host, @sitemaps_host, @yield_sitemap, @sitemaps_path, @adapter, @verbose, @protect_index, @sitemap_index, @added_default_links, @created_group, @sitemap = nil
122
124
 
123
125
  options = SitemapGenerator::Utilities.reverse_merge(options,
124
- :include_root => true,
125
- :include_index => false,
126
- :filename => :sitemap,
127
- :search_engines => {
128
- :google => "http://www.google.com/webmasters/tools/ping?sitemap=%s",
129
- :bing => "http://www.bing.com/webmaster/ping.aspx?sitemap=%s"
130
- },
131
- :create_index => :auto,
132
- :compress => true,
133
- :max_sitemap_links => SitemapGenerator::MAX_SITEMAP_LINKS
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
134
133
  )
135
- options.each_pair { |k, v| instance_variable_set("@#{k}".to_sym, v) }
134
+ options.each_pair { |k, v| instance_variable_set(:"@#{k}", v) }
136
135
 
137
136
  # If an index is passed in, protect it from modification.
138
137
  # Sitemaps can be added to the index but nothing else can be changed.
@@ -147,9 +146,9 @@ module SitemapGenerator
147
146
  # link - string link e.g. '/merchant', '/article/1' or whatever.
148
147
  # options - see README.
149
148
  # host - host for the link, defaults to your <tt>default_host</tt>.
150
- def add(link, options={})
151
- add_default_links if !@added_default_links
152
- sitemap.add(link, SitemapGenerator::Utilities.reverse_merge(options, :host => @default_host))
149
+ def add(link, options = {})
150
+ add_default_links unless @added_default_links
151
+ sitemap.add(link, SitemapGenerator::Utilities.reverse_merge(options, host: @default_host))
153
152
  rescue SitemapGenerator::SitemapFullError
154
153
  finalize_sitemap!
155
154
  retry
@@ -164,8 +163,8 @@ module SitemapGenerator
164
163
  #
165
164
  # The `:host` option defaults to the value of `sitemaps_host` which is the host where your
166
165
  # sitemaps reside. If no `sitemaps_host` is set, the `default_host` is used.
167
- def add_to_index(link, options={})
168
- sitemap_index.add(link, SitemapGenerator::Utilities.reverse_merge(options, :host => sitemaps_host))
166
+ def add_to_index(link, options = {})
167
+ sitemap_index.add(link, SitemapGenerator::Utilities.reverse_merge(options, host: sitemaps_host))
169
168
  end
170
169
 
171
170
  # Create a new group of sitemap files.
@@ -195,7 +194,7 @@ module SitemapGenerator
195
194
  # Options like <tt>:default_host</tt> can be used and it will only affect the links
196
195
  # within the group. Links added outside of the group will revert to the previous
197
196
  # +default_host+.
198
- def group(opts={}, &block)
197
+ def group(opts = {}, &block) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
199
198
  @created_group = true
200
199
  original_opts = opts.dup
201
200
 
@@ -218,7 +217,7 @@ module SitemapGenerator
218
217
  @original_location = @sitemap.location.dup
219
218
  @sitemap.location.merge!(@group.sitemap_location)
220
219
  if block_given?
221
- @group.interpreter.eval(:yield_sitemap => @yield_sitemap || SitemapGenerator.yield_sitemap?, &block)
220
+ @group.interpreter.eval(yield_sitemap: @yield_sitemap || SitemapGenerator.yield_sitemap?, &block)
222
221
  @group.finalize_sitemap!
223
222
  @sitemap.location.merge!(@original_location)
224
223
  end
@@ -237,7 +236,7 @@ module SitemapGenerator
237
236
  @group.send(:create_index=, true, true) if @group.create_index != false
238
237
 
239
238
  if block_given?
240
- @group.interpreter.eval(:yield_sitemap => @yield_sitemap || SitemapGenerator.yield_sitemap?, &block)
239
+ @group.interpreter.eval(yield_sitemap: @yield_sitemap || SitemapGenerator.yield_sitemap?, &block)
241
240
  @group.finalize_sitemap!
242
241
  end
243
242
  end
@@ -279,28 +278,28 @@ module SitemapGenerator
279
278
  # Is equivalent to:
280
279
  #
281
280
  # SitemapGenerator::Sitemap.ping_search_engines('http://example.com/sitemap.xml.gz', :super_engine => 'http://superengine.com/ping?url=%s')
282
- def ping_search_engines(*args)
283
- require 'cgi/session'
281
+ def ping_search_engines(*args) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
284
282
  require 'open-uri'
285
283
  require 'timeout'
284
+ require 'uri'
286
285
 
287
286
  engines = args.last.is_a?(Hash) ? args.pop : {}
288
287
  unescaped_url = args.shift || sitemap_index_url
289
- index_url = CGI.escape(unescaped_url)
288
+ index_url = URI.encode_www_form_component(unescaped_url)
290
289
 
291
290
  output("\n")
292
291
  output("Pinging with URL '#{unescaped_url}':")
293
292
  search_engines.merge(engines).each do |engine, link|
294
- link = link % index_url
293
+ link %= index_url
295
294
  name = Utilities.titleize(engine.to_s)
296
295
  begin
297
- Timeout::timeout(10) {
296
+ Timeout.timeout(10) do
298
297
  if URI.respond_to?(:open) # Available since Ruby 2.5
299
298
  URI.open(link)
300
299
  else
301
300
  open(link) # using Kernel#open became deprecated since Ruby 2.7. See https://bugs.ruby-lang.org/issues/15893
302
301
  end
303
- }
302
+ end
304
303
  output(" Successful ping of #{name}")
305
304
  rescue Timeout::Error, StandardError => e
306
305
  output("Ping failed for #{name}: #{e.inspect} (URL #{link})")
@@ -385,26 +384,26 @@ module SitemapGenerator
385
384
  #
386
385
  # If both `filename` and `namer` are passed, set filename first so it
387
386
  # doesn't override the latter.
388
- def set_options(opts={})
387
+ def set_options(opts = {})
389
388
  opts = opts.dup
390
- %w(filename namer).each do |key|
391
- if value = opts.delete(key.to_sym)
392
- send("#{key}=", value)
389
+ %w[filename namer].each do |key|
390
+ if (value = opts.delete(key.to_sym))
391
+ send(:"#{key}=", value)
393
392
  end
394
393
  end
395
394
  opts.each_pair do |key, value|
396
- send("#{key}=", value)
395
+ send(:"#{key}=", value)
397
396
  end
398
397
  end
399
398
 
400
399
  # Given +opts+, modify it and return it prepped for creating a new group from this LinkSet.
401
400
  # If <tt>:public_path</tt> is present in +opts+ it is removed because groups cannot
402
401
  # change the public path.
403
- def options_for_group(opts)
402
+ def options_for_group(opts) # rubocop:disable Metrics/MethodLength
404
403
  opts = SitemapGenerator::Utilities.reverse_merge(opts,
405
- :include_index => false,
406
- :include_root => false,
407
- :sitemap_index => sitemap_index
404
+ include_index: false,
405
+ include_root: false,
406
+ sitemap_index: sitemap_index
408
407
  )
409
408
  opts.delete(:public_path)
410
409
 
@@ -438,7 +437,7 @@ module SitemapGenerator
438
437
  # in an instance variable.
439
438
  def add_default_links
440
439
  @added_default_links = true
441
- link_options = { :lastmod => Time.now, :priority => 1.0 }
440
+ link_options = { lastmod: Time.now, priority: 1.0 }
442
441
  if include_root?
443
442
  add('/', link_options)
444
443
  end
@@ -458,7 +457,8 @@ module SitemapGenerator
458
457
  # block passed to create() is empty the default links are still included in the
459
458
  # sitemap.
460
459
  def finalize_sitemap!
461
- return if sitemap.finalized? || sitemap.empty? && @created_group
460
+ return if sitemap.finalized? || (sitemap.empty? && @created_group)
461
+
462
462
  add_default_links if !@added_default_links && !@created_group
463
463
  # This will finalize it. We add to the index even if not creating an index because
464
464
  # the index keeps track of how many links are in our sitemaps and we need this info
@@ -471,6 +471,7 @@ module SitemapGenerator
471
471
  # been finalized.
472
472
  def finalize_sitemap_index!
473
473
  return if @protect_index || sitemap_index.finalized?
474
+
474
475
  sitemap_index.finalize!
475
476
  sitemap_index.write
476
477
  end
@@ -478,7 +479,7 @@ module SitemapGenerator
478
479
  # Return the interpreter linked to this instance.
479
480
  def interpreter
480
481
  require 'sitemap_generator/interpreter'
481
- @interpreter ||= SitemapGenerator::Interpreter.new(:link_set => self)
482
+ @interpreter ||= SitemapGenerator::Interpreter.new(link_set: self)
482
483
  end
483
484
 
484
485
  # Reset this instance. Keep the same options, but return to the same state
@@ -494,6 +495,7 @@ module SitemapGenerator
494
495
  # evaluated and some info output to STDOUT in a lazy fasion.
495
496
  def output(string)
496
497
  return unless verbose
498
+
497
499
  puts string
498
500
  end
499
501
 
@@ -577,28 +579,28 @@ module SitemapGenerator
577
579
  # Return a new +SitemapLocation+ instance with the current options included
578
580
  def sitemap_location
579
581
  SitemapGenerator::SitemapLocation.new(
580
- :host => sitemaps_host,
581
- :namer => namer,
582
- :public_path => public_path,
583
- :sitemaps_path => @sitemaps_path,
584
- :adapter => @adapter,
585
- :verbose => verbose,
586
- :compress => @compress,
587
- :max_sitemap_links => max_sitemap_links
582
+ host: sitemaps_host,
583
+ namer: namer,
584
+ public_path: public_path,
585
+ sitemaps_path: @sitemaps_path,
586
+ adapter: @adapter,
587
+ verbose: verbose,
588
+ compress: @compress,
589
+ max_sitemap_links: max_sitemap_links
588
590
  )
589
591
  end
590
592
 
591
593
  # Return a new +SitemapIndexLocation+ instance with the current options included
592
594
  def sitemap_index_location
593
595
  SitemapGenerator::SitemapLocation.new(
594
- :host => sitemaps_host,
595
- :namer => namer,
596
- :public_path => public_path,
597
- :sitemaps_path => @sitemaps_path,
598
- :adapter => @adapter,
599
- :verbose => verbose,
600
- :create_index => @create_index,
601
- :compress => @compress
596
+ host: sitemaps_host,
597
+ namer: namer,
598
+ public_path: public_path,
599
+ sitemaps_path: @sitemaps_path,
600
+ adapter: @adapter,
601
+ verbose: verbose,
602
+ create_index: @create_index,
603
+ compress: @compress
602
604
  )
603
605
  end
604
606
 
@@ -610,7 +612,7 @@ module SitemapGenerator
610
612
  # are in your sitemap. If `false` an index file is never created.
611
613
  # If `:auto` an index file is created only if your sitemap has more than
612
614
  # one sitemap file.
613
- def create_index=(value, force=false)
615
+ def create_index=(value, force = false)
614
616
  @create_index = value
615
617
  # Allow overriding the protected status of the index when we are creating a group.
616
618
  # Because sometimes we need to force an index in that case. But generally we don't
@@ -659,8 +661,8 @@ module SitemapGenerator
659
661
 
660
662
  # Update the given attribute on the current sitemap index and sitemap file location objects.
661
663
  # But don't create the index or sitemap files yet if they are not already created.
662
- def update_location_info(attribute, value, opts={})
663
- opts = SitemapGenerator::Utilities.reverse_merge(opts, :include_index => !@protect_index)
664
+ def update_location_info(attribute, value, opts = {})
665
+ opts = SitemapGenerator::Utilities.reverse_merge(opts, include_index: !@protect_index)
664
666
  @sitemap_index.location[attribute] = value if opts[:include_index] && @sitemap_index && !@sitemap_index.finalized?
665
667
  @sitemap.location[attribute] = value if @sitemap && !@sitemap.finalized?
666
668
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SitemapGenerator
2
4
  class Railtie < Rails::Railtie
3
5
  rake_tasks do
4
- load "tasks/sitemap_generator_tasks.rake"
6
+ load 'tasks/sitemap_generator_tasks.rake'
5
7
  end
6
8
  end
7
9
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SitemapGenerator
2
4
  # A class for generating sitemap filenames.
3
5
  #
@@ -26,11 +28,11 @@ module SitemapGenerator
26
28
  # is used, and subsequent names would be 'sitemap1.xml.gz', 'sitemap2.xml.gz', etc.
27
29
  # In these examples the `base` string is assumed to be 'sitemap'.
28
30
  class SimpleNamer
29
- def initialize(base, options={})
31
+ def initialize(base, options = {})
30
32
  @options = SitemapGenerator::Utilities.reverse_merge(options,
31
- :zero => nil, # identifies the marker for the start of the series
32
- :extension => '.xml.gz',
33
- :start => 1
33
+ zero: nil, # identifies the marker for the start of the series
34
+ extension: '.xml.gz',
35
+ start: 1
34
36
  )
35
37
  @base = base
36
38
  reset
@@ -63,7 +65,8 @@ module SitemapGenerator
63
65
 
64
66
  # Return this instance set to the previous name
65
67
  def previous
66
- raise NameError, "Already at the start of the series" if start?
68
+ raise NameError, 'Already at the start of the series' if start?
69
+
67
70
  if @count <= @options[:start]
68
71
  @count = @options[:zero]
69
72
  else
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'sitemap_generator/helpers/number_helper'
2
4
 
3
5
  module SitemapGenerator
@@ -12,6 +14,7 @@ module SitemapGenerator
12
14
  [:host, :adapter].each do |method|
13
15
  define_method(method) do
14
16
  raise SitemapGenerator::SitemapError, "No value set for #{method}" unless self[method]
17
+
15
18
  self[method]
16
19
  end
17
20
  end
@@ -45,7 +48,7 @@ module SitemapGenerator
45
48
  # stripped from the filename. If `:all_but_first`, only the `.gz` extension of the first
46
49
  # filename is stripped off. If `true` the extensions are left unchanged.
47
50
  # * <tt>max_sitemap_links</tt> - The maximum number of links to put in each sitemap.
48
- def initialize(opts={})
51
+ def initialize(opts = {})
49
52
  SitemapGenerator::Utilities.assert_valid_keys(opts, [
50
53
  :adapter,
51
54
  :public_path,
@@ -66,14 +69,14 @@ module SitemapGenerator
66
69
  # sitemap index files. However, this greatly eases testing, so I'm leaving it in
67
70
  # for now.
68
71
  if !opts[:filename] && !opts[:namer]
69
- opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap, :start => 2, :zero => 1)
72
+ opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap, start: 2, zero: 1)
70
73
  end
71
74
  opts[:verbose] = !!opts[:verbose]
72
75
  self.merge!(opts)
73
76
  end
74
77
 
75
78
  # Return a new Location instance with the given options merged in
76
- def with(opts={})
79
+ def with(opts = {})
77
80
  self.merge(opts)
78
81
  end
79
82
 
@@ -106,9 +109,10 @@ module SitemapGenerator
106
109
  # If using a namer once the filename has been retrieved from the namer its
107
110
  # value is locked so that it is unaffected by further changes to the namer.
108
111
  def filename
109
- raise SitemapGenerator::SitemapError, "No filename or namer set" unless self[:filename] || self[:namer]
112
+ raise SitemapGenerator::SitemapError, 'No filename or namer set' unless self[:filename] || self[:namer]
113
+
110
114
  unless self[:filename]
111
- self.send(:[]=, :filename, self[:namer].to_s, :super => true)
115
+ self.send(:[]=, :filename, +self[:namer].to_s, super: true)
112
116
 
113
117
  # Post-process the filename for our compression settings.
114
118
  # Strip the `.gz` from the extension if we aren't compressing this file.
@@ -148,7 +152,7 @@ module SitemapGenerator
148
152
  end
149
153
 
150
154
  # If you set the filename, clear the namer and vice versa.
151
- def []=(key, value, opts={})
155
+ def []=(key, value, opts = {})
152
156
  if !opts[:super]
153
157
  case key
154
158
  when :namer
@@ -172,12 +176,12 @@ module SitemapGenerator
172
176
  filesize = number_to_human_size(self.filesize)
173
177
  width = self.class::PATH_OUTPUT_WIDTH
174
178
  path = SitemapGenerator::Utilities.ellipsis(self.path_in_public, width)
175
- "+ #{('%-'+width.to_s+'s') % path} #{'%10s' % link_count} links / #{'%10s' % filesize}"
179
+ "+ #{('%-' + width.to_s + 's') % path} #{'%10s' % link_count} links / #{'%10s' % filesize}"
176
180
  end
177
181
  end
178
182
 
179
183
  class SitemapIndexLocation < SitemapLocation
180
- def initialize(opts={})
184
+ def initialize(opts = {})
181
185
  if !opts[:filename] && !opts[:namer]
182
186
  opts[:namer] = SitemapGenerator::SimpleNamer.new(:sitemap)
183
187
  end
@@ -198,7 +202,7 @@ module SitemapGenerator
198
202
  filesize = number_to_human_size(self.filesize)
199
203
  width = self.class::PATH_OUTPUT_WIDTH - 3
200
204
  path = SitemapGenerator::Utilities.ellipsis(self.path_in_public, width)
201
- "+ #{('%-'+width.to_s+'s') % path} #{'%10s' % link_count} sitemaps / #{'%10s' % filesize}"
205
+ "+ #{('%-' + width.to_s + 's') % path} #{'%10s' % link_count} sitemaps / #{'%10s' % filesize}"
202
206
  end
203
207
  end
204
208
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # require this file to load the tasks
2
4
  require 'rake'
3
5
 
@@ -28,18 +30,18 @@ namespace :sitemap do
28
30
  require 'sitemap_generator'
29
31
  end
30
32
 
31
- desc "Install a default config/sitemap.rb file"
32
- task :install => ['sitemap:require'] do
33
+ desc 'Install a default config/sitemap.rb file'
34
+ task install: ['sitemap:require'] do
33
35
  SitemapGenerator::Utilities.install_sitemap_rb(verbose)
34
36
  end
35
37
 
36
- desc "Delete all Sitemap files in public/ directory"
37
- task :clean => ['sitemap:require'] do
38
+ desc 'Delete all Sitemap files in public/ directory'
39
+ task clean: ['sitemap:require'] do
38
40
  SitemapGenerator::Utilities.clean_files
39
41
  end
40
42
 
41
- desc "Generate sitemaps and ping search engines."
42
- task :refresh => ['sitemap:create'] do
43
+ desc 'Generate sitemaps and ping search engines.'
44
+ task refresh: ['sitemap:create'] do
43
45
  SitemapGenerator::Sitemap.ping_search_engines
44
46
  end
45
47
 
@@ -47,7 +49,7 @@ namespace :sitemap do
47
49
  task 'refresh:no_ping' => ['sitemap:create']
48
50
 
49
51
  desc "Generate sitemaps but don't ping search engines. Alias for refresh:no_ping."
50
- task :create => ['sitemap:require_environment'] do
51
- SitemapGenerator::Interpreter.run(:config_file => ENV["CONFIG_FILE"], :verbose => verbose)
52
+ task create: ['sitemap:require_environment'] do
53
+ SitemapGenerator::Interpreter.run(config_file: ENV['CONFIG_FILE'], verbose: verbose)
52
54
  end
53
55
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SitemapGenerator
2
4
  # Provide convenient access to template files. E.g.
3
5
  #
@@ -7,17 +9,18 @@ module SitemapGenerator
7
9
  # Define an accessor method for each template file.
8
10
  class Templates
9
11
  FILES = {
10
- :sitemap_sample => 'sitemap.rb',
12
+ sitemap_sample: 'sitemap.rb',
11
13
  }
12
14
 
13
15
  # Dynamically define accessors for each key defined in <tt>FILES</tt>
14
- attr_accessor(*FILES.keys)
15
- FILES.keys.each do |name|
16
- eval <<-END
16
+ attr_writer(*FILES.keys)
17
+
18
+ FILES.each_key do |name|
19
+ eval(<<-ACCESSOR, binding, __FILE__, __LINE__ + 1)
17
20
  define_method(:#{name}) do
18
21
  @#{name} ||= read_template(:#{name})
19
22
  end
20
- END
23
+ ACCESSOR
21
24
  end
22
25
 
23
26
  def initialize(root = SitemapGenerator.root)
@@ -1,16 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SitemapGenerator
2
4
  module Utilities
3
5
  extend self
4
6
 
5
7
  # Copy templates/sitemap.rb to config if not there yet.
6
- def install_sitemap_rb(verbose=false)
8
+ def install_sitemap_rb(verbose = false)
7
9
  if File.exist?(SitemapGenerator.app.root + 'config/sitemap.rb')
8
- puts "already exists: config/sitemap.rb, file not copied" if verbose
10
+ puts 'already exists: config/sitemap.rb, file not copied' if verbose
9
11
  else
10
12
  FileUtils.cp(
11
13
  SitemapGenerator.templates.template_path(:sitemap_sample),
12
14
  SitemapGenerator.app.root + 'config/sitemap.rb')
13
- puts "created: config/sitemap.rb" if verbose
15
+ puts 'created: config/sitemap.rb' if verbose
14
16
  end
15
17
  end
16
18
 
@@ -31,7 +33,7 @@ module SitemapGenerator
31
33
  # strings for keys but assert symbols as keys, this will fail.
32
34
  def assert_valid_keys(hash, *valid_keys)
33
35
  unknown_keys = hash.keys - [valid_keys].flatten
34
- raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
36
+ raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(', ')}") unless unknown_keys.empty?
35
37
  end
36
38
 
37
39
  # Return a new hash with all keys converted to symbols, as long as
@@ -69,7 +71,7 @@ module SitemapGenerator
69
71
  # x.round(2) # => 1.34
70
72
  def round(float, precision = nil)
71
73
  if precision
72
- magnitude = 10.0 ** precision
74
+ magnitude = 10.0**precision
73
75
  (float * magnitude).round / magnitude
74
76
  else
75
77
  float.round
@@ -98,7 +100,7 @@ module SitemapGenerator
98
100
  # Performs the opposite of <tt>merge</tt>, with the keys and values from the first hash taking precedence over the second.
99
101
  # Modifies the receiver in place.
100
102
  def reverse_merge!(hash, other_hash)
101
- hash.merge!( other_hash ){|k,o,n| o }
103
+ hash.merge!(other_hash) { |k, o, n| o }
102
104
  end
103
105
 
104
106
  # An object is blank if it's false, empty, or a whitespace string.
@@ -111,7 +113,7 @@ module SitemapGenerator
111
113
  # ...to:
112
114
  #
113
115
  # if !address.blank?
114
- def blank?(object)
116
+ def blank?(object) # rubocop:disable Metrics/MethodLength
115
117
  case object
116
118
  when NilClass, FalseClass
117
119
  true
@@ -133,13 +135,14 @@ module SitemapGenerator
133
135
 
134
136
  # Sets $VERBOSE for the duration of the block and back to its original value afterwards.
135
137
  def with_warnings(flag)
136
- old_verbose, $VERBOSE = $VERBOSE, flag
138
+ old_verbose, $VERBOSE = $VERBOSE, flag # rubocop:disable Style/ParallelAssignment
137
139
  yield
138
140
  ensure
139
141
  $VERBOSE = old_verbose
140
142
  end
141
143
 
142
144
  def titleize(string)
145
+ string = string.dup if string.frozen?
143
146
  string.gsub!(/_/, ' ')
144
147
  string.split(/(\W)/).map(&:capitalize).join
145
148
  end
@@ -156,7 +159,7 @@ module SitemapGenerator
156
159
  # Returns a string. Expects a string or Pathname object.
157
160
  def append_slash(path)
158
161
  strpath = path.to_s
159
- if strpath[-1] != nil && strpath[-1].chr != '/'
162
+ if !strpath[-1].nil? && strpath[-1].chr != '/'
160
163
  strpath + '/'
161
164
  else
162
165
  strpath
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'sitemap_generator/simple_namer'
2
4
  require 'sitemap_generator/builder'
3
5
  require 'sitemap_generator/link_set'
@@ -9,6 +11,7 @@ require 'sitemap_generator/sitemap_location'
9
11
  module SitemapGenerator
10
12
  autoload(:Interpreter, 'sitemap_generator/interpreter')
11
13
  autoload(:FileAdapter, 'sitemap_generator/adapters/file_adapter')
14
+ autoload(:ActiveStorageAdapter, 'sitemap_generator/adapters/active_storage_adapter') if defined?(::ActiveStorage)
12
15
  autoload(:S3Adapter, 'sitemap_generator/adapters/s3_adapter')
13
16
  autoload(:AwsSdkAdapter, 'sitemap_generator/adapters/aws_sdk_adapter')
14
17
  autoload(:WaveAdapter, 'sitemap_generator/adapters/wave_adapter')
@@ -22,7 +25,7 @@ module SitemapGenerator
22
25
  SitemapFinalizedError = Class.new(SitemapError)
23
26
 
24
27
  Utilities.with_warnings(nil) do
25
- VERSION = File.read(File.dirname(__FILE__) + "/../VERSION").strip
28
+ VERSION = File.read(File.dirname(__FILE__) + '/../VERSION').strip
26
29
  MAX_SITEMAP_FILES = 50_000 # max sitemap links per index file
27
30
  MAX_SITEMAP_LINKS = 50_000 # max links per sitemap
28
31
  MAX_SITEMAP_IMAGES = 1_000 # max images per url
@@ -62,13 +65,14 @@ module SitemapGenerator
62
65
  # Global default for the verbose setting.
63
66
  def self.verbose
64
67
  if @verbose.nil?
65
- @verbose = if SitemapGenerator::Utilities.truthy?(ENV['VERBOSE'])
66
- true
67
- elsif SitemapGenerator::Utilities.falsy?(ENV['VERBOSE'])
68
- false
69
- else
70
- nil
71
- end
68
+ @verbose =
69
+ if SitemapGenerator::Utilities.truthy?(ENV['VERBOSE'])
70
+ true
71
+ elsif SitemapGenerator::Utilities.falsy?(ENV['VERBOSE'])
72
+ false
73
+ else
74
+ nil
75
+ end
72
76
  else
73
77
  @verbose
74
78
  end
@@ -79,7 +83,7 @@ module SitemapGenerator
79
83
  !!@yield_sitemap
80
84
  end
81
85
 
82
- self.root = File.expand_path(File.join(File.dirname(__FILE__), '../')) # Root of the install dir, not the Rails app
86
+ self.root = File.expand_path(File.join(File.dirname(__FILE__), '../')) # Root of the install dir, not the Rails app
83
87
  self.templates = SitemapGenerator::Templates.new(self.root)
84
88
  self.app = SitemapGenerator::Application.new
85
89
  end
data/templates/sitemap.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # Set the host name for URL creation
2
- SitemapGenerator::Sitemap.default_host = "http://www.example.com"
2
+ SitemapGenerator::Sitemap.default_host = 'http://www.example.com'
3
3
 
4
4
  SitemapGenerator::Sitemap.create do
5
5
  # Put links creation logic here.