class Fontist::Import::GoogleImport

Public Instance Methods

call() click to toggle source
# File lib/fontist/import/google_import.rb, line 9
def call
  fonts = new_fonts
  create_formulas(fonts)
  rebuild_index
end

Private Instance Methods

create_formula(font_path) click to toggle source
# File lib/fontist/import/google_import.rb, line 30
def create_formula(font_path)
  puts font_path

  path = Fontist::Import::CreateFormula.new(
    url(font_path),
    name: Google.metadata_name(font_path),
    formula_dir: formula_dir,
    skip_sha: variable_style?(font_path),
    digest: Google.digest(font_path),
  ).call

  Fontist.ui.success("Formula has been successfully created: #{path}")
end
create_formulas(fonts) click to toggle source
# File lib/fontist/import/google_import.rb, line 21
def create_formulas(fonts)
  return puts("Nothing to update") if fonts.empty?

  puts "Creating formulas..."
  fonts.each do |path|
    create_formula(path)
  end
end
formula_dir() click to toggle source
# File lib/fontist/import/google_import.rb, line 49
def formula_dir
  @formula_dir ||= Fontist.formulas_path.join("google").tap do |path|
    FileUtils.mkdir_p(path) unless File.exist?(path)
  end
end
new_fonts() click to toggle source
# File lib/fontist/import/google_import.rb, line 17
def new_fonts
  Fontist::Import::Google::NewFontsFetcher.new(logging: true).call
end
rebuild_index() click to toggle source
# File lib/fontist/import/google_import.rb, line 62
def rebuild_index
  Fontist::Index.rebuild
end
url(path) click to toggle source
# File lib/fontist/import/google_import.rb, line 44
def url(path)
  name = Google.metadata_name(path)
  "https://fonts.google.com/download?family=#{ERB::Util.url_encode(name)}"
end
variable_style?(path) click to toggle source
# File lib/fontist/import/google_import.rb, line 55
def variable_style?(path)
  fonts = Dir.glob(File.join(path, "*.{ttf,otf}"))
  fonts.any? do |font|
    File.basename(font).match?(/\[(.+,)?(wght|opsz)\]/)
  end
end