class IOStreams::Tabular::Parser::Psv

For parsing a single line of Pipe-separated values

Public Instance Methods

parse(row) click to toggle source

Returns [Array] the parsed PSV line

# File lib/io_streams/tabular/parser/psv.rb, line 19
def parse(row)
  return row if row.is_a?(::Array)

  raise(IOStreams::Errors::TypeMismatch, "Format is :psv. Invalid input: #{row.class.name}") unless row.is_a?(String)

  row.split("|")
end
parse_header(row) click to toggle source

Returns [Array<String>] the header row. Returns nil if the row is blank.

# File lib/io_streams/tabular/parser/psv.rb, line 8
def parse_header(row)
  return row if row.is_a?(::Array)

  unless row.is_a?(String)
    raise(IOStreams::Errors::InvalidHeader, "Format is :psv. Invalid input header: #{row.class.name}")
  end

  row.split("|")
end
render(row, header) click to toggle source

Return the supplied array as a single line JSON string.

# File lib/io_streams/tabular/parser/psv.rb, line 28
def render(row, header)
  array          = header.to_array(row)
  cleansed_array = array.collect do |i|
    i.is_a?(String) ? i.tr("|", ":") : i
  end
  cleansed_array.join("|")
end