Class: Json2::CsvWithHeader
- Inherits:
-
Object
- Object
- Json2::CsvWithHeader
- Defined in:
- lib/json2/csv_with_header.rb
Overview
Turn a Json input into a Csv output with a header.
Instance Method Summary collapse
-
#initialize(input, options = {}) ⇒ CsvWithHeader
constructor
Creates a new CsvWithHeader instance.
-
#output ⇒ Object
Get the Csv.
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, = {}) = @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
#output ⇒ Object
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 |