Sha256: 8372ec2e34e2ffc9b65538522fe3d143e250757dd1f5338bdf5bd4b3b989b5df

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

require "fontist/import"
require_relative "recursive_extraction"
require_relative "helpers/hash_helper"
require_relative "formula_builder"

module Fontist
  module Import
    class CreateFormula
      def initialize(url, options = {})
        @url = url
        @options = options
      end

      def call
        save(builder)
      end

      private

      def builder
        builder = FormulaBuilder.new
        setup_strings(builder, archive)
        setup_files(builder)
        builder
      end

      def setup_strings(builder, archive)
        builder.archive = archive
        builder.url = @url
        builder.options = @options
      end

      def setup_files(builder)
        builder.extractor = extractor
        builder.font_files = extractor.font_files
        builder.font_collection_files = extractor.font_collection_files
        builder.license_text = extractor.license_text
      end

      def extractor
        @extractor ||=
          RecursiveExtraction.new(archive,
                                  subarchive: @options[:subarchive],
                                  subdir: @options[:subdir])
      end

      def archive
        @archive ||= download(@url)
      end

      def download(url)
        return url if File.exist?(url)

        Fontist::Utils::Downloader.download(url, progress_bar: true).path
      end

      def save(builder)
        filename = Import.name_to_filename(builder.name)
        path = @options[:formula_dir] ? File.join(@options[:formula_dir], filename) : filename
        yaml = YAML.dump(Helpers::HashHelper.stringify_keys(builder.formula))
        File.write(path, yaml)
        path
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fontist-1.13.2 lib/fontist/import/create_formula.rb
fontist-1.13.1 lib/fontist/import/create_formula.rb
fontist-1.13.0 lib/fontist/import/create_formula.rb
fontist-1.12.0 lib/fontist/import/create_formula.rb