Sha256: 19d9e0ec9eb5767c1b01b425c44c2ea6fe1bdfbc24d02cdec925e99304c42e8c

Contents?: true

Size: 1.08 KB

Versions: 20

Compression:

Stored size: 1.08 KB

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)
          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

        # Returns [Array] the parsed PSV line
        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

        # 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

20 entries across 20 versions & 1 rubygems

Version Path
iostreams-1.10.3 lib/io_streams/tabular/parser/psv.rb
iostreams-1.10.2 lib/io_streams/tabular/parser/psv.rb
iostreams-1.10.1 lib/io_streams/tabular/parser/psv.rb
iostreams-1.10.0 lib/io_streams/tabular/parser/psv.rb
iostreams-1.9.0 lib/io_streams/tabular/parser/psv.rb
iostreams-1.8.0 lib/io_streams/tabular/parser/psv.rb
iostreams-1.7.0 lib/io_streams/tabular/parser/psv.rb
iostreams-1.6.2 lib/io_streams/tabular/parser/psv.rb
iostreams-1.6.1 lib/io_streams/tabular/parser/psv.rb
iostreams-1.6.0 lib/io_streams/tabular/parser/psv.rb
iostreams-1.5.1 lib/io_streams/tabular/parser/psv.rb
iostreams-1.5.0 lib/io_streams/tabular/parser/psv.rb
iostreams-1.4.0 lib/io_streams/tabular/parser/psv.rb
iostreams-1.3.3 lib/io_streams/tabular/parser/psv.rb
iostreams-1.3.2 lib/io_streams/tabular/parser/psv.rb
iostreams-1.3.1 lib/io_streams/tabular/parser/psv.rb
iostreams-1.3.0 lib/io_streams/tabular/parser/psv.rb
iostreams-1.2.1 lib/io_streams/tabular/parser/psv.rb
iostreams-1.2.0 lib/io_streams/tabular/parser/psv.rb
iostreams-1.1.1 lib/io_streams/tabular/parser/psv.rb