module Wavefront::Mixins

Various things which help around the place

Public Instance Methods

call_delete(uri) click to toggle source
# File lib/wavefront/mixins.rb, line 87
def call_delete(uri)
  if verbose || noop
    puts 'DELETE ' + uri.to_s
    puts 'HEADERS ' + headers.to_s
  end
  return if noop
  RestClient.delete(uri.to_s, headers)
end
call_get(uri) click to toggle source
# File lib/wavefront/mixins.rb, line 64
def call_get(uri)
  if verbose || noop
    puts 'GET ' + uri.to_s
    puts 'HEADERS ' + headers.to_s
  end
  return if noop
  RestClient.get(uri.to_s, headers)
end
call_post(uri, query = nil, ctype = 'text/plain') click to toggle source
# File lib/wavefront/mixins.rb, line 73
def call_post(uri, query = nil, ctype = 'text/plain')
  h = headers
  if verbose || noop
    puts 'POST ' + uri.to_s
    puts 'QUERY ' + query if query
    puts 'HEADERS ' + h.to_s
  end
  return if noop

  RestClient.post(uri.to_s, query,
                  h.merge(:'Content-Type' => ctype,
                          :Accept         => 'application/json'))
end
hash_to_qs(payload) click to toggle source
# File lib/wavefront/mixins.rb, line 52
def hash_to_qs(payload)
  #
  # Make a properly escaped query string out of a key: value
  # hash.
  #
  URI.escape(payload.map { |k, v| [k, v].join('=') }.join('&'))
end
interpolate_schema(label, host, prefix_length) click to toggle source
# File lib/wavefront/mixins.rb, line 22
def interpolate_schema(label, host, prefix_length)
  label_parts = label.split('.')
  interpolated = []
  interpolated << label_parts.shift(prefix_length)
  interpolated << host
  interpolated << label_parts
  interpolated.flatten!
  interpolated.join('.')
end
load_file(path) click to toggle source
# File lib/wavefront/mixins.rb, line 96
def load_file(path)
  #
  # Give it a path to a file (as a string) and it will return the
  # contents of that file as a Ruby object. Automatically detects
  # JSON and YAML. Raises an exception if it doesn't look like
  # either.
  #
  file = Pathname.new(path)
  raise 'Import file does not exist.' unless file.exist?

  if file.extname == '.json'
    JSON.parse(IO.read(file))
  elsif file.extname == '.yaml' || file.extname == '.yml'
    YAML.load(IO.read(file))
  else
    raise 'Unsupported file format.'
  end
end
parse_time(t) click to toggle source
# File lib/wavefront/mixins.rb, line 32
def parse_time(t)
  #
  # Return a time as an integer, however it might come in.
  #
  return t if t.is_a?(Integer)
  return t.to_i if t.is_a?(Time)
  return t.to_i if t.is_a?(String) && t.match(/^\d+$/)
  DateTime.parse("#{t} #{Time.now.getlocal.zone}").to_time.utc.to_i
rescue
  raise "cannot parse timestamp '#{t}'."
end
time_to_ms(t) click to toggle source
# File lib/wavefront/mixins.rb, line 44
def time_to_ms(t)
  #
  # Return the time as milliseconds since the epoch
  #
  return false unless t.is_a?(Integer)
  (t.to_f * 1000).round
end
uri_concat(*args) click to toggle source
# File lib/wavefront/mixins.rb, line 60
def uri_concat(*args)
  args.join('/').squeeze('/')
end