Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/indexmap/task_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ def create
index_now_key_filename = write_index_now_key if configuration.index_now.write_key_file?
configuration.run_after_create_callbacks

{files: written_files.map(&:to_s), written_files: written_files, index_now_key_filename: index_now_key_filename}
{
files: written_files.map(&:to_s),
written_files: written_files,
sitemaps: sitemap_details(written_files),
index_now_key_filename: index_now_key_filename
}
end

def format
Expand Down Expand Up @@ -60,5 +65,27 @@ def default_output
def sitemap_files
storage.list(prefix: "sitemap", suffix: ".xml")
end

def sitemap_details(files)
files.map do |filename|
{
filename: filename.to_s,
location: sitemap_location(filename),
link_count: sitemap_link_count(filename)
}
end
end

def sitemap_location(filename)
return storage.public_url(filename) if storage.respond_to?(:public_url)

filename.to_s
end

def sitemap_link_count(filename)
document = Nokogiri::XML(storage.read(filename.to_s))
document.remove_namespaces!
document.xpath("//loc").count
end
end
end
10 changes: 9 additions & 1 deletion lib/tasks/indexmap_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ namespace :indexmap do
runner = Indexmap::TaskRunner.new
create_result = runner.create

puts "Created, formatted, and validated #{file_count(create_result[:files])} in #{storage_description(runner)}."
puts "Created, formatted, and validated #{file_count(create_result[:files])}."
puts "Files created:"
create_result[:sitemaps].each do |sitemap|
puts " - #{sitemap[:location]} (#{link_count(sitemap[:link_count])})"
end
puts "IndexNow key file: #{create_result[:index_now_key_filename]}" if create_result[:index_now_key_filename]
end

Expand Down Expand Up @@ -91,6 +95,10 @@ namespace :indexmap do
runner.storage
end

def link_count(count)
"#{count} #{(count == 1) ? "link" : "links"}"
end

def format_google_ping_failure(failure)
case failure[:reason]
when :unauthorized
Expand Down
14 changes: 13 additions & 1 deletion test/indexmap/task_runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def read_file(filename)
end

def test_create_writes_new_sitemap_and_key_file_without_deleting_unrelated_files
storage = Indexmap::Storage::Memory.new
storage = Indexmap::Storage::Memory.new(public_url: "https://cdn.example.com/sitemaps")
storage.write("sitemap-pages.xml.gz", "old")
storage.write("sitemap-extra.xml", "existing")

Expand All @@ -31,6 +31,18 @@ def test_create_writes_new_sitemap_and_key_file_without_deleting_unrelated_files
assert_equal VALID_KEY, storage.read("#{VALID_KEY}.txt")
assert_equal ["sitemap-pages.xml", "sitemap.xml"], result[:files]
assert_equal ["sitemap-pages.xml", "sitemap.xml"], result[:written_files]
assert_equal [
{
filename: "sitemap-pages.xml",
location: "https://cdn.example.com/sitemaps/sitemap-pages.xml",
link_count: 1
},
{
filename: "sitemap.xml",
location: "https://cdn.example.com/sitemaps/sitemap.xml",
link_count: 1
}
], result[:sitemaps]
assert_equal "#{VALID_KEY}.txt", result[:index_now_key_filename]
end

Expand Down
Loading