class Json2xxx::CLI

Public Class Methods

new(args = [], options = {}, config = {}) click to toggle source
Calls superclass method
# File lib/json2xxx/cli.rb, line 13
def initialize(args = [], options = {}, config = {})
  super(args, options, config)
  @global_options = config[:shell].base.options
  return unless File.pipe?(STDIN)
  @data = parse_json(STDIN.read)
  @core = Core.new
  if @global_options['fields']
    @data = @core.extract(@data, @global_options['fields'])
  end
  if @global_options['sort']
    @data = @core.sort(@data, @global_options['sort'])
  end
end

Public Instance Methods

backlog() click to toggle source
# File lib/json2xxx/cli.rb, line 59
def backlog
  puts @core.convert_backlog_wiki(@data)
end
csv() click to toggle source
# File lib/json2xxx/cli.rb, line 44
def csv
  puts @core.convert_csv(@data, ',', options['force_quotes'], options['write_header'])
end
delimiter(delim) click to toggle source
# File lib/json2xxx/cli.rb, line 30
def delimiter(delim)
  puts @core.convert_csv(@data, delim, options['force_quotes'], options['write_header'])
end
excel() click to toggle source
# File lib/json2xxx/cli.rb, line 75
def excel
  @core.convert_excel(@data, options['output'])
end
hash() click to toggle source
# File lib/json2xxx/cli.rb, line 69
def hash
  ap @data, indent: 2, index: false
end
html() click to toggle source
# File lib/json2xxx/cli.rb, line 64
def html
  puts @core.convert_html(@data)
end
markdown() click to toggle source
# File lib/json2xxx/cli.rb, line 54
def markdown
  puts @core.convert_markdown(@data)
end
tsv() click to toggle source
# File lib/json2xxx/cli.rb, line 37
def tsv
  puts @core.convert_csv(@data, "\t", options['force_quotes'], options['write_header'])
end
yaml() click to toggle source
# File lib/json2xxx/cli.rb, line 49
def yaml
  puts YAML.dump(@data)
end

Private Instance Methods

parse_json(buffer) click to toggle source
# File lib/json2xxx/cli.rb, line 81
def parse_json(buffer)
  begin
    buffer = buffer.uncolorize
    data = JSON.parse(buffer)
  rescue => e
    data = []
    buffer.lines.each do |line|
      data << JSON.parse(line)
    end
  end
  data.class == Array ? data : [data]
end