Sha256: db8293a3be24182bbbd53f4582a6a83cb3234e4a0fb1584b5dad373a6828e95b

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require 'httparty'

require 'stream_lines/error'

module StreamLines
  module Reading
    class Stream
      include Enumerable
      include HTTParty

      raise_on 400..599

      def initialize(url)
        @url = url
        @buffer = StringIO.new
      end

      def each(&block)
        stream_lines(&block)
      rescue HTTParty::Error => e
        raise Error, "Failed to download #{url} with code: #{e.response.code}"
      end

      private

      attr_reader :url

      def stream_lines(&block)
        self.class.get(url, stream_body: true) do |chunk|
          lines = extract_lines(chunk)
          lines.each { |line| block.call(line) }
        end

        @buffer.rewind
        block.call(@buffer.read) if @buffer.size.positive?
      end

      def extract_lines(chunk)
        lines = chunk.split($INPUT_RECORD_SEPARATOR, -1)

        if lines.length > 1
          @buffer.rewind
          lines.first.prepend(@buffer.read)
          @buffer = StringIO.new
        end

        @buffer << lines.pop
        lines
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stream_lines-0.3.1 lib/stream_lines/reading/stream.rb