class Turnstile::Commands::Show

Public Instance Methods

csv(data, delimiter = nil) click to toggle source

CSV Format

# File lib/turnstile/commands/show.rb, line 39
def csv(data, delimiter = nil)
  build_string(data) do |key, value, *|
    string = [key, value].to_csv
    string.gsub!(/,/, delimiter) if delimiter
    string.strip
  end
end
execute(format = :json, delimiter = nil) click to toggle source
# File lib/turnstile/commands/show.rb, line 8
def execute(format = :json, delimiter = nil)
  STDOUT.puts render_totals(format, delimiter)
end
json(data, *) click to toggle source

Formats supported for the output JSON

# File lib/turnstile/commands/show.rb, line 25
def json(data, *)
  build_string(data, "\n", '{', '}') do |key, value, index:, last:, first:|
    json_row(key, value, index: index, first: first, last: last)
  end
end
nad(data, *) click to toggle source

NAD format for Circonus

# File lib/turnstile/commands/show.rb, line 33
def nad(data, *)
  build_string(data) { |key, value, *| nad_row(key, value) }
end
render_totals(format, delimiter = nil) click to toggle source
# File lib/turnstile/commands/show.rb, line 12
def render_totals(format, delimiter = nil)
  unless self.respond_to?(format)
    raise ArgumentError, "Format #{format} is not supported"
  end
  self.send(format, aggregate, delimiter)
end
yaml(data, *) click to toggle source
# File lib/turnstile/commands/show.rb, line 19
def yaml(data, *)
  build_string(data, "\n", "---\nturnstile:") { |key, value, *| yaml_row(key, value) }
end

Private Instance Methods

build_string(data, joiner = "\n", prefix = nil, suffix = nil) { |key, value, index: index, first: (index == 0), last: (index == count - 1)| ... } click to toggle source

This method is used to build a string with opening/closing parts and looping contents inside.

# File lib/turnstile/commands/show.rb, line 52
def build_string(data,
                 joiner = "\n",
                 prefix = nil,
                 suffix = nil,
                 &_block)
  slices = []
  slices << prefix if prefix
  index = 0; count = data.size
  data.each_pair do |key, value|
    slices << yield(
      key,
        value,
        index: index,
        first: (index == 0),
        last: (index == count - 1)
    ).to_s
    index += 1
  end
  slices << suffix if suffix
  slices.compact.join(joiner).strip
end
json_row(key, value, last: false, **) click to toggle source
# File lib/turnstile/commands/show.rb, line 74
def json_row(key, value, last: false, **)
  %Q(  "#{key}": #{value}#{ last ? '' : ',' })
end
nad_row(key, value) click to toggle source
# File lib/turnstile/commands/show.rb, line 79
def nad_row(key, value)
  %Q(turnstile:#{key}#{"\tn\t"}#{value})
end
yaml_row(key, value) click to toggle source
# File lib/turnstile/commands/show.rb, line 83
def yaml_row(key, value)
  %Q(  #{key}: #{value})
end