Module: Wavefront::Mixins

Overview

Various things which help around the place

Instance Method Summary collapse

Instance Method Details

#call_delete(uri) ⇒ Object



87
88
89
90
91
92
93
94
# 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) ⇒ Object



64
65
66
67
68
69
70
71
# 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') ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



52
53
54
55
56
57
58
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
# 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) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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) ⇒ Object



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



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



60
61
62
# File 'lib/wavefront/mixins.rb', line 60

def uri_concat(*args)
  args.join('/').squeeze('/')
end