Sha256: b37d5ee0e2b80fbfbacbc3ecadadd710058300d4e7aad4c683dfd9660b99bd10

Contents?: true

Size: 1017 Bytes

Versions: 1

Compression:

Stored size: 1017 Bytes

Contents

module IOStreams
  class Tabular
    module Parser
      # For parsing a single line of Pipe-separated values
      class Psv < Base
        # Returns [Array<String>] the header row.
        # Returns nil if the row is blank.
        def parse_header(row)
          unless row.is_a?(String)
            raise(Tabular::Errors::InvalidHeader, "Format is :psv. Invalid input header: #{row.class.name}")
          end

          row.split('|')
        end

        # Returns [Array] the parsed PSV line
        def parse(row)
          raise(Tabular::Errors::TypeMismatch, "Format is :psv. Invalid input: #{row.class.name}") unless row.is_a?(String)

          row.split('|')
        end

        # Return the supplied array as a single line JSON string.
        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
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
iostreams-0.15.0 lib/io_streams/tabular/parser/psv.rb