Sha256: 72cc86edeb1e42983d3f575b1f525a172106ba271e6fa091d33bb250e471c20c

Contents?: true

Size: 1.11 KB

Versions: 9

Compression:

Stored size: 1.11 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)

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

          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

9 entries across 9 versions & 1 rubygems

Version Path
iostreams-1.1.0 lib/io_streams/tabular/parser/psv.rb
iostreams-1.0.0 lib/io_streams/tabular/parser/psv.rb
iostreams-1.0.0.beta7 lib/io_streams/tabular/parser/psv.rb
iostreams-1.0.0.beta6 lib/io_streams/tabular/parser/psv.rb
iostreams-1.0.0.beta5 lib/io_streams/tabular/parser/psv.rb
iostreams-1.0.0.beta4 lib/io_streams/tabular/parser/psv.rb
iostreams-1.0.0.beta3 lib/io_streams/tabular/parser/psv.rb
iostreams-1.0.0.beta2 lib/io_streams/tabular/parser/psv.rb
iostreams-1.0.0.beta lib/io_streams/tabular/parser/psv.rb