module NewRelicManagement::Util

> Utility Methods

Public Instance Methods

cachier(label, time = 30, &block) click to toggle source

> Cache Return of a Function

# File lib/newrelic-management/util.rb, line 24
def cachier(label, time = 30, &block)
  var = "@_cachier_#{label}"
  cache = instance_variable_get(var) || {}
  return cache['data'] if cache['timestamp'] && Time.now <= cache['timestamp'] + time
  cache['timestamp'] = Time.now
  cache['data'] = block.yield
  instance_variable_set(var, cache)
  cache['data']
end
cachier!(var = nil) click to toggle source

> Clear Cache

# File lib/newrelic-management/util.rb, line 35
def cachier!(var = nil)
  if var && instance_variable_get("@#{var}")
    # => Clear the Single Variable
    remove_instance_variable("@#{var}")
  else
    # => Clear the Whole Damned Cache
    instance_variables.each { |x| remove_instance_variable(x) }
  end
end
common_array(ary) click to toggle source

> Return Common Elements of an Array

# File lib/newrelic-management/util.rb, line 90
def common_array(ary) # rubocop: disable AbcSize
  return ary unless ary.is_a? Array
  count = ary.count
  return ary if count.zero?
  return ary.flatten.uniq if count == 1
  common = ary[0] & ary[1]
  return common if count == 2
  (count - 2).times { |x| common &= ary[x + 2] } if count > 2
  common
end
filestring(file, size = 8192) click to toggle source

> Check if a string is an existing file, and return it's content

# File lib/newrelic-management/util.rb, line 68
def filestring(file, size = 8192)
  return unless file
  return file unless file.is_a?(String) && File.file?(file) && File.size(file) <= size
  File.read(file)
end
parse_json(file = nil, symbolize = true) click to toggle source

> Define JSON Parser

# File lib/newrelic-management/util.rb, line 50
def parse_json(file = nil, symbolize = true)
  return unless file && ::File.exist?(file.to_s)
  begin
    ::JSON.parse(::File.read(file.to_s), symbolize_names: symbolize)
  rescue JSON::ParserError
    return
  end
end
serialize(response) click to toggle source

> Serialization <= #

# File lib/newrelic-management/util.rb, line 78
def serialize(response)
  # => Serialize Object into JSON Array
  JSON.pretty_generate(response.map(&:name).sort_by(&:downcase))
end
serialize_csv(csv) click to toggle source
# File lib/newrelic-management/util.rb, line 83
def serialize_csv(csv)
  # => Serialize a CSV String into an Array
  return unless csv && csv.is_a?(String)
  csv.split(',')
end
write_json(file, object) click to toggle source

> Define JSON Writer

# File lib/newrelic-management/util.rb, line 60
def write_json(file, object)
  return unless file && object
  begin
    File.open(file, 'w') { |f| f.write(JSON.pretty_generate(object)) }
  end
end