class Inspec::Resources::GemPackage

Attributes

gem_binary[R]

Public Class Methods

new(package_name, gem_binary = nil) click to toggle source
# File lib/inspec/resources/gem.rb, line 19
def initialize(package_name, gem_binary = nil)
  @package_name = package_name
  @gem_binary = case gem_binary # TODO: no. this is not right
                when nil
                  "gem"
                when :chef
                  if inspec.os.windows?
                    # TODO: what about chef-dk or other installs?
                    'c:\opscode\chef\embedded\bin\gem.bat'
                  else
                    "/opt/chef/embedded/bin/gem"
                  end
                when :chef_server
                  "/opt/opscode/embedded/bin/gem"
                else
                  gem_binary
                end
  skip_resource "Unable to retrieve gem information" if info.empty?
end

Public Instance Methods

info() click to toggle source
# File lib/inspec/resources/gem.rb, line 39
def info
  return @info if defined?(@info)

  require "inspec/resources/command"

  cmd = inspec.command("#{@gem_binary} list --local -a -q \^#{@package_name}\$")
  return {} unless cmd.exit_status == 0

  # extract package name and version
  # parses data like winrm (1.3.4, 1.3.3)
  params = /^\s*([^\(]*?)\s*\((.*?)\)\s*$/.match(cmd.stdout.chomp)
  @info = {
    installed: !params.nil?,
    type: "gem",
  }
  return @info unless @info[:installed]

  versions = params[2].split(",").map(&:strip)
  @info[:name] = params[1]
  @info[:version] = versions[0]
  @info[:versions] = versions
  @info
end
installed?() click to toggle source
# File lib/inspec/resources/gem.rb, line 63
def installed?
  info[:installed] == true
end
to_s() click to toggle source
# File lib/inspec/resources/gem.rb, line 76
def to_s
  "gem package #{@package_name}"
end
version() click to toggle source
# File lib/inspec/resources/gem.rb, line 67
def version
  info[:version]
end
versions() click to toggle source

this returns an array of strings

# File lib/inspec/resources/gem.rb, line 72
def versions
  info[:versions]
end