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.



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

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

Instance Method Details

#ensure_key_fileObject



51
52
53
54
55
56
57
58
59
# File 'lib/indexmap/pinger/index_now.rb', line 51

def ensure_key_file
  configured_key = index_now_configuration.key.to_s.strip
  return write_key_file(key: configured_key) unless configured_key.empty?

  return existing_key_file if existing_key_file

  key = generated_key
  write_key_file(key: key, path: configuration.public_path.join("#{key}.txt"))
end

#pingObject



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

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_file(key: index_now_configuration.key, path: nil) ⇒ Object



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

def write_key_file(key: index_now_configuration.key, path: nil)
  key = key.to_s.strip
  return if key.empty?

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