Class: Harbinger::EolFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/harbinger/eol_fetcher.rb

Constant Summary collapse

CACHE_EXPIRY_SECONDS =

24 hours

24 * 60 * 60
API_BASE_URL =
"https://endoflife.date/api"

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir: default_cache_dir) ⇒ EolFetcher

Returns a new instance of EolFetcher.



12
13
14
15
# File 'lib/harbinger/eol_fetcher.rb', line 12

def initialize(cache_dir: default_cache_dir)
  @cache_dir = cache_dir
  FileUtils.mkdir_p(@cache_dir)
end

Instance Method Details

#eol_date_for(product, version) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/harbinger/eol_fetcher.rb', line 34

def eol_date_for(product, version)
  data = fetch(product)
  return nil unless data

  # Extract major.minor from version (e.g., "3.2.1" -> "3.2")
  version_parts = version.split(".")
  major = version_parts[0]
  major_minor = version_parts[1] ? "#{major}.#{version_parts[1]}" : nil

  # Try exact major.minor first (e.g., "8.0" for MySQL, "3.2" for Ruby)
  if major_minor
    entry = data.find { |item| item["cycle"] == major_minor }
    return entry["eol"] if entry
  end

  # Try major only (e.g., "16" for PostgreSQL)
  entry = data.find { |item| item["cycle"] == major }
  return entry["eol"] if entry

  # For major-only versions, find the latest minor version in that major series
  # (e.g., version "7" should match "7.4" which is the latest 7.x)
  matching_entries = data.select { |item| item["cycle"].to_s.start_with?("#{major}.") }
  return nil if matching_entries.empty?

  # Sort by cycle version and get the latest (highest minor version)
  latest = matching_entries.max_by { |item| item["cycle"].to_s.split(".").map(&:to_i) }
  latest ? latest["eol"] : nil
end

#fetch(product) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/harbinger/eol_fetcher.rb', line 17

def fetch(product)
  cache_file = cache_file_path(product)

  # Return fresh cache if available
  return read_cache(cache_file) if cache_fresh?(cache_file)

  # Try to fetch from API
  begin
    data = fetch_from_api(product)
    write_cache(cache_file, data)
    data
  rescue StandardError
    # Fall back to stale cache if API fails
    read_cache(cache_file) if File.exist?(cache_file)
  end
end