Module: Pkg::Util::Misc

Defined in:
lib/packaging/util/misc.rb

Overview

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.

Class Method Summary collapse

Class Method Details

.check_gem(gem) ⇒ Object

Returns boolean for whether or not the gem is installed.

Parameters:

  • gem

    the name of the gem to see if it is installed

Returns:

  • boolean for whether or not the gem is installed



43
44
45
46
47
48
49
# 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) ⇒ Object

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 ¯_(ツ)_/¯

Parameters:

  • gem_name

    the gem to check if you are an owner

Returns:

  • boolean whether or not you own the gem



57
58
59
60
61
62
63
64
65
66
67
# 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.safe_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) ⇒ Object

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

Parameters:

  • file (String)

    Path to the json file

Raises:

  • (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



30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object

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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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