Class: Indexmap::Pinger::IndexNow

Inherits:
Base
  • Object
show all
Defined in:
lib/indexmap/pinger/index_now.rb

Instance Method Summary collapse

Methods inherited from Base

#logger, ping

Constructor Details

#initialize(configuration: Indexmap.configuration, connection: nil) ⇒ IndexNow

Returns a new instance of IndexNow.



10
11
12
13
# File 'lib/indexmap/pinger/index_now.rb', line 10

def initialize(configuration: Indexmap.configuration, connection: nil)
  super(configuration: configuration)
  @connection = connection
end

Instance Method Details

#pingObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/indexmap/pinger/index_now.rb', line 15

def ping
  api_key = read_api_key
  unless api_key
    logger.debug("IndexNow API key is not configured.")
    return
  end

  entries = entries_to_ping
  if entries.empty?
    logger.debug("IndexNow: no URLs matched the current filter.")
    return
  end

  entries.each_slice(max_urls_per_request) do |batch|
    urls = batch.map(&:loc)

    if dry_run?
      logger.debug { "IndexNow dry-run: would ping #{urls.count} URLs." }
      next
    end

    submit_batch(api_key: api_key, urls: urls)
  end
end

#write_key_fileObject



40
41
42
43
44
45
46
47
48
# File 'lib/indexmap/pinger/index_now.rb', line 40

def write_key_file
  key = index_now_configuration.key.to_s.strip
  return if key.empty?

  path = index_now_configuration.key_path(public_path: configuration.public_path)
  FileUtils.mkdir_p(path.dirname)
  File.write(path, "#{key}\n")
  path
end