module Pkg::Util::Misc

A collection of utility methods that don't belong in any of our other top level Pkg::Util modules and probably won't anytime soon.

Public Class Methods

check_gem(gem) click to toggle source

@param gem the name of the gem to see if it is installed @return boolean for whether or not the gem is installed

# File lib/packaging/util/misc.rb, line 43
def check_gem(gem)
  if %x(gem list -q #{gem}).chomp.empty?
    return false
  else
    return true
  end
end
check_rubygems_ownership(gem_name) click to toggle source

This loads your ~/.gem/credentials file and uses your api key to query rubygems.org for which gems you own. There may be better ways to query this but having to pull this information using curl is sort of gross. It works though, so ¯_(ツ)_/¯ @param gem_name the gem to check if you are an owner @return boolean whether or not you own the gem

# File lib/packaging/util/misc.rb, line 57
def check_rubygems_ownership(gem_name)
  require 'yaml'
  credentials = YAML.load_file("#{ENV['HOME']}/.gem/credentials")
  gems = YAML.load(%x(curl -H 'Authorization:#{credentials[:rubygems_api_key]}' https://rubygems.org/api/v1/gems.yaml))
  gems.each do |gem|
    if gem['name'] == gem_name
      return true
    end
  end
  return false
end
load_from_json(file) click to toggle source

Loads and parses json from a file. Will treat the keys in the json as methods to invoke on the component in question

@param file [String] Path to the json file @raise [RuntimeError] exceptions are raised if there is no file, if it refers to methods that don't exist, or if it does not contain a Hash

# File lib/packaging/util/misc.rb, line 30
def load_from_json(file)
  data = JSON.parse(File.read(file))
  unless data.is_a?(Hash)
    raise "Hash required. Got '#{data.class}' when parsing '#{file}'"
  end
  # We explicity return data here b/c the unless clause above will cause the
  # Function to return nil.
  #               -Sean P. M. 05/11/2016
  data
end
search_and_replace(search_string, replacements) click to toggle source

This method takes a string and a list of tokens and variables and it replaces the listed tokens with the matched variable if it exists. All values will be explicitly coerced to strings.

# File lib/packaging/util/misc.rb, line 9
def search_and_replace(search_string, replacements)
  raise ArgumentError "replacements must respond to #each_pair" unless
    replacements.respond_to? :each_pair

  replacements.each_pair do |token, value|
    unless value
      warn "replacement value for '#{token}' probably shouldn't be nil"
      next
    end

    search_string.gsub!(token.to_s, value.to_s)
  end

  search_string
end