Sha256: 026907819efff4826f2fb95e8799a9f1f752f2d6eb5f4d24030c4cd7e56a7593

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

module Feedzirra
  ##
  # Contrary to Feedzirra::Feed, Feedzirra::PushParser doesn't expect
  # the whole document to be given in one String, but allows
  # subsequent parsing of chunks.
  class PushParser
    ##
    # How many bytes to buffer before starting to parse, helps
    # Feedzirra's content detection.
    BUF_MIN_THRESHOLD = 1000

    ##
    # Just resets instance variables
    def initialize
      @buf = ''
      @parser = nil
    end

    ##
    # Either buffer up til BUF_MIN_THRESHOLD or, if reached, actually
    # parse a chunk
    def push(chunk)
      if @parser
        @parser.parse(chunk)
      else
        @buf += chunk
        if @buf.size > BUF_MIN_THRESHOLD
          start_parsing
        end
      end
    end

    ##
    # Really start parsing, if BUF_MIN_THRESHOLD wasn't reached yet,
    # finalize, and return the actual parser/document
    def finish
      # TODO: if we haven't started yet we won't even need a
      # PushParser
      start_parsing unless @parser

      @parser.parse_finish
      @parser
    end

    private

    def start_parsing
      unless klass = Feed::determine_feed_parser_for_xml(@buf)
        raise NoParserAvailable.new("No valid parser for XML.")
      end
      @parser = klass.new
      @parser.parse(@buf)
      @buf = nil
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
astro-feedzirra-0.0.12 lib/feedzirra/push_parser.rb
julien51-feedzirra-0.0.13 lib/feedzirra/push_parser.rb
julien51-feedzirra-0.0.14 lib/feedzirra/push_parser.rb
julien51-feedzirra-0.0.16 lib/feedzirra/push_parser.rb