FileSystem.open_document()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 4
rs 9.8
c 0
b 0
f 0
1 1
require "tmpdir"
2 1
require "pathname"
3
4 1
module Resume
5 1
  module CLI
6
    # Module containing functions concerning interacting with the
7
    # local file system.
8
    #
9
    # @author Paul Fioravanti
10 1
    module FileSystem
11
      # Represents the ?dl=1 parameter on a Dropbox link.
12 1
      DOWNLOAD_PARAMETER_REGEX = /\?.+\z/.freeze
13 1
      private_constant :DOWNLOAD_PARAMETER_REGEX
14
15 1
      module_function
16
17
      # Attempts to open a file in a system-appropriate way.
18
      #
19
      # @param filename [String] The filename to open.
20
      # @return [true] if the document can be opened successfully.
21
      # @return [false] if the document cannot be opened successfully.
22
      # @return [nil] if it is unknown how to attempt to open the document.
23 1
      def open_document(filename)
24 4
        case RUBY_PLATFORM
25
        when /darwin/
26 1
          system("open", filename)
27
        when /linux/
28 1
          system("xdg-open", filename)
29
        when /windows/
30 1
          system("cmd", "/c", "\"start #{filename}\"")
31
        else
32 1
          Output.warning(:dont_know_how_to_open_resume)
33
        end
34
      end
35
36
      # Derive a system-independent tmpfile path from a `filename`.
37
      #
38
      # @param filename [String] The filename to create a tmpfile path for.
39
      # @return [Pathname] The generated tmpfile pathname for `filename`.
40 1
      def tmpfile_path(filename)
41
        # Ensure that the ?dl=1 parameter is removed
42 28
        Pathname.new(Dir.tmpdir).join(
43
          filename.sub(DOWNLOAD_PARAMETER_REGEX, "")
44
        )
45
      end
46
    end
47
  end
48
end
49