RubyVersionChecker.check_ruby_version()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 10
ccs 4
cts 4
cp 1
crap 3
rs 9.9
c 0
b 0
f 0
1 1
module Resume
2
  # Ensures that the version of Ruby being used is recent enough
3
  # to generate the resume.
4
  #
5
  # @author Paul Fioravanti
6 1
  module RubyVersionChecker
7
    # Required Ruby version for resume to work
8 1
    REQUIRED_RUBY_VERSION = ENV.fetch("CUSTOM_RUBY_VERSION", "2.6.3").freeze
9 1
    private_constant :REQUIRED_RUBY_VERSION
10
    # Ruby version command
11 1
    RUBY_VERSION_COMMAND = "ruby -v".freeze
12 1
    private_constant :RUBY_VERSION_COMMAND
13
    # Ruby version output regex.
14
    # For example, this will extract `2.6.3` from a string like:
15
    # ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
16 1
    RUBY_VERSION_REGEX = /\Aruby ([\d\.][^p]+)/.freeze
17 1
    private_constant :RUBY_VERSION_REGEX
18
19 1
    module_function
20
21
    # Checks the Ruby version being used to generate the resume.
22
    #
23
    # @return [nil]
24
    #   if rubygems is successfully required and the Ruby version is either
25
    #   installed or not installed.
26
    # @note On the one-sheet resume, this check will only work for
27
    #   >= Ruby 2.1  Earlier versions will throw a syntax error.
28 1
    def check_ruby_version
29
      # require "rubygems" only needed for ruby pre-1.9.0
30
      # but it's safe for later versions (evaluates to false).
31 4
      require "rubygems"
32 3
      request_to_install_latest_ruby if old_ruby_version?
33
    rescue LoadError
34
      # NOTE: LoadError could occur if someone attempts to run
35
      # this with Ruby 1.8.7 and they don't have rubygems installed
36 1
      request_to_install_latest_ruby
37
    end
38
39 1
    def old_ruby_version?
40 3
      Gem::Version.new(RUBY_VERSION.dup) <
41
        Gem::Version.new(REQUIRED_RUBY_VERSION)
42
    end
43 1
    private_class_method :old_ruby_version?
44
45 1
    def request_to_install_latest_ruby
46 3
      puts "Please install Ruby version #{REQUIRED_RUBY_VERSION} "\
47
           "or higher to generate resume."
48 3
      require "open3"
49 2
      puts "Your Ruby version is #{user_ruby_version}"
50
    rescue LoadError
51
      # NOTE: LoadError will likely occur if someone attempts to run
52
      # this with Ruby 1.8.7 and they don't have rubygems installed
53 1
      puts "Your Ruby version is likely far too old to generate the resume."
54
    ensure
55 3
      exit(1)
56
    end
57 1
    private_class_method :request_to_install_latest_ruby
58
59 1
    def user_ruby_version
60
      ruby_version =
61 2
        Open3.popen3(RUBY_VERSION_COMMAND) do |_stdin, stdout, _stderr, _wait|
62 2
          stdout.read
63
        end
64 2
      ruby_version.match(RUBY_VERSION_REGEX)[0]
65
    end
66 1
    private_class_method :user_ruby_version
67
  end
68
end
69