Class: Json2::CsvWithHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/json2/csv_with_header.rb

Overview

Turn a Json input into a Csv output with a header.

Instance Method Summary collapse

Constructor Details

#initialize(input, options = {}) ⇒ CsvWithHeader

Creates a new CsvWithHeader instance.

input - A Hash representing a Json file. This is typically

obtained with JSON.parse.

options - Optional. A Hash of options, see Option to get the list.



11
12
13
14
15
16
17
18
19
# File 'lib/json2/csv_with_header.rb', line 11

def initialize(input, options = {})
  @options = options
  @input = input
  @names_stack = []
  @nodes = Hash.new {|hash, key| hash[key] = [] }
  process_input
  @column_size = @nodes.values.map {|node| node.size }.max
  @keys = @nodes.keys.select {|key| @nodes[key].size == @column_size }
end

Instance Method Details

#outputObject

Get the Csv.

Returns the whole document as a single String.



24
25
26
# File 'lib/json2/csv_with_header.rb', line 24

def output
  Header.get(@keys) + Body.get(@nodes, @keys, @column_size)
end