Class: Indexmap::Pinger::IndexNow

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

Constant Summary collapse

KEY_FORMAT =
/\A[a-f0-9]{32}\z/

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.



13
14
15
16
# File 'lib/indexmap/pinger/index_now.rb', line 13

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

Instance Method Details

#ensure_key_fileObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/indexmap/pinger/index_now.rb', line 55

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

  existing_path = existing_key_file
  return existing_path if existing_path

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

#pingObject



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

def ping
  api_key = read_api_key
  unless api_key
    logger.debug("IndexNow API key is not configured.")
    return {status: :skipped, reason: :missing_key}
  end

  entries = entries_to_ping
  if entries.empty?
    logger.debug("IndexNow: no URLs matched the current filter.")
    return {status: :skipped, reason: :no_urls}
  end

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

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

    submit_batch(api_key: api_key, urls: urls)
  end

  summarize_results(results)
end

#write_key_file(key: index_now_configuration.key, path: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/indexmap/pinger/index_now.rb', line 45

def write_key_file(key: index_now_configuration.key, path: nil)
  key = normalized_configured_key(key)
  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)
  path
end