class Fontist::Import::Google::NewFontsFetcher

Constants

REPO_PATH
REPO_URL
SKIPLIST_PATH

Public Class Methods

new(logging: false) click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 12
def initialize(logging: false)
  @logging = logging
end

Public Instance Methods

call() click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 16
def call
  update_repo
  fetch_new_paths
end

Private Instance Methods

downloadable?(name) click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 118
def downloadable?(name)
  retries ||= 0
  retries += 1
  Down.open("https://fonts.google.com/download?family=#{name}")
  true
rescue Down::NotFound
  false
rescue Down::TimeoutError
  retry unless retries >= 3
  false
end
fetch_fonts_paths() click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 40
def fetch_fonts_paths
  Dir[File.join(REPO_PATH, "apache", "*"),
      File.join(REPO_PATH, "ofl", "*"),
      File.join(REPO_PATH, "ufl", "*")].sort
end
fetch_new_paths() click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 32
def fetch_new_paths
  fetch_fonts_paths.select do |path|
    log_font(path) do
      new?(path)
    end
  end
end
fonts_up_to_date?(formula, path) click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 84
def fonts_up_to_date?(formula, path)
  styles = formula_styles(formula)
  repo_fonts(path).all? do |font|
    style = styles.find { |s| s.font == repo_to_archive_name(font) }
    return false unless style

    otfinfo_version(font) == style.version
  end
end
formula(font_name) click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 108
def formula(font_name)
  path = Fontist::Import::Google.formula_path(font_name)
  Formula.new_from_file(path) if File.exist?(path)
end
formula_styles(formula) click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 94
def formula_styles(formula)
  formula.fonts.map(&:styles).flatten
end
in_skiplist?(name) click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 65
def in_skiplist?(name)
  @skiplist ||= YAML.safe_load(File.open(SKIPLIST_PATH))
  @skiplist.include?(name)
end
log_font(path) { || ... } click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 46
def log_font(path)
  return yield unless @logging

  print "#{path}, "
  new = yield
  puts(new ? "new" : "skipped")
  new
end
new?(path) click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 55
def new?(path)
  metadata_name = Google.metadata_name(path)
  return unless metadata_name
  return if in_skiplist?(metadata_name)
  return if up_to_date?(metadata_name, path)
  return unless downloadable?(metadata_name)

  true
end
otfinfo_version(path) click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 113
def otfinfo_version(path)
  info = OtfParser.new(path).call
  Fontist::Import::Google.style_version(info["Version"])
end
repo_digest_up_to_date?(formula, path) click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 78
def repo_digest_up_to_date?(formula, path)
  return unless formula.digest

  formula.digest == Google.digest(path)
end
repo_fonts(path) click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 98
def repo_fonts(path)
  Dir.glob(File.join(path, "*.{ttf,otf}"))
end
repo_to_archive_name(font_path) click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 102
def repo_to_archive_name(font_path)
  File.basename(font_path)
    .sub("[wght]", "-VariableFont_wght")
    .sub("[opsz]", "-Regular-VariableFont_opsz")
end
up_to_date?(metadata_name, path) click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 70
def up_to_date?(metadata_name, path)
  formula = formula(metadata_name)
  return false unless formula

  repo_digest_up_to_date?(formula, path) ||
    fonts_up_to_date?(formula, path)
end
update_repo() click to toggle source
# File lib/fontist/import/google/new_fonts_fetcher.rb, line 23
def update_repo
  if Dir.exist?(REPO_PATH)
    `cd #{REPO_PATH} && git pull`
  else
    FileUtils.mkdir_p(File.dirname(REPO_PATH))
    `git clone --depth 1 #{REPO_URL} #{REPO_PATH}`
  end
end