class WkhtmltopdfRunner::Path

Constants

EXE_NAME

Public Class Methods

get(path = nil) click to toggle source
# File lib/wkhtmltopdf_runner/path.rb, line 5
def self.get(path = nil)
  new(path).call
end
new(path = nil) click to toggle source
# File lib/wkhtmltopdf_runner/path.rb, line 11
def initialize(path = nil)
  @path = path
end

Public Instance Methods

call() click to toggle source
# File lib/wkhtmltopdf_runner/path.rb, line 15
def call
  return @path if WkhtmltopdfRunner::Utils.present?(@path)

  find_wkhtmltopdf_binary_path
end

Private Instance Methods

env_paths() click to toggle source
# File lib/wkhtmltopdf_runner/path.rb, line 45
def env_paths
  possible_locations = [
    ENV['PATH'].split(File::PATH_SEPARATOR),
    %w[/usr/bin /usr/local/bin]
  ]

  possible_locations << %w[~/bin] if ENV.key?('HOME')
  possible_locations.flatten.uniq
end
find_wkhtmltopdf_binary_path() click to toggle source
# File lib/wkhtmltopdf_runner/path.rb, line 23
def find_wkhtmltopdf_binary_path
  path_from_which || path_from_env
end
path_from_env() click to toggle source
# File lib/wkhtmltopdf_runner/path.rb, line 39
def path_from_env
  env_paths
    .map { |l| File.expand_path(File.join(l, EXE_NAME)) }
    .detect { |location| File.exist?(location) }
end
path_from_which() click to toggle source
# File lib/wkhtmltopdf_runner/path.rb, line 27
def path_from_which
  detected_path = if defined?(Bundler)
    Bundler.which(EXE_NAME)
  else
    `which #{EXE_NAME}`.chomp
  end

  WkhtmltopdfRunner::Utils.presence(detected_path)
rescue StandardError
  nil
end