FontDownloader.download_and_extract_font()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1 1
require "tmpdir"
2 1
require_relative "../output"
3 1
require_relative "exceptions"
4 1
require_relative "file_system"
5 1
require_relative "file_fetcher"
6 1
require_relative "content_parser"
7 1
require_relative "font_extractor"
8
9 1
module Resume
10 1
  module CLI
11
    # Module concerned with downloading font files to be used with
12
    # the Japanese language version of the resume.
13
    #
14
    # @author Paul Fioravanti
15 1
    class FontDownloader
16 1
      extend Forwardable
17
18
      # @!attribute fonts [r]
19
      # @return [Hash] The hash of fonts to download.
20 1
      attr_reader :fonts
21
22 1
      def self.local_files_present?(files)
23 6
        files.all? { |file| File.file?(FileSystem.tmpfile_path(file)) }
24
      end
25 1
      private_class_method :local_files_present?
26
27
      # Initialises a new instance of a Font Downloader.
28
      #
29
      # @param fonts [Hash]
30
      #   A hash containing the font file dependencies for the resume.
31
      # @return [FontDownloader]
32
      #   The font downloader object.
33 1
      def initialize(fonts)
34 8
        @fonts = fonts
35
      end
36
37
      # Audits the local system to see if the necessary font files
38
      # are available to use.
39
      #
40
      # @return [Hash] The list of remaining font files to install.
41 1
      def audit_font_dependencies
42 3
        fonts.each do |font|
43 2
          if self.class.__send__(:local_files_present?, font[:files].values)
44 1
            fonts.delete(font)
45
          end
46
        end
47
      end
48
49
      # Outputs a message indicating that a font file must be downloaded
50
      # in order to generate the resume.
51
      #
52
      # @return [nil]
53 1
      def output_font_dependencies
54 2
        return if fonts.none?
55
56 1
        Output.warning(:custom_fonts)
57
      end
58
59
      # Attempts to download the font file and reports back on
60
      # whether it was successful.
61
      #
62
      # @raise [NetworkConnectionError]
63
      #   if an internet connection is unavailable to download the file.
64
      # @return [true]
65
      #   if the font file was successfully downloaded.
66
      # @return [false]
67
      #   if the font file was not successfuly downloaded.
68 1
      def fonts_successfully_downloaded?
69 3
        return true if fonts.none?
70
71 2
        fonts.all? do |font|
72 2
          download_and_extract_font(font)
73 1
          true
74
        end
75
      rescue NetworkConnectionError
76 1
        false
77
      end
78
79 1
      private
80
81 1
      def download_and_extract_font(font)
82 2
        Output.plain(:downloading_font)
83 2
        FileFetcher.fetch(
84
          ContentParser.decode_content(font[:location])
85
        )
86 1
        require "zip"
87 1
        FontExtractor.extract(font)
88
      end
89
    end
90
  end
91
end
92