Sha256: 5dcd144d967b53c72c0033507e0b625fec73da57d90dea4622dc687cd18a2dd4

Contents?: true

Size: 946 Bytes

Versions: 19

Compression:

Stored size: 946 Bytes

Contents

module GnipApi
  module Apis
    module PowerTrack
      class Buffer
        attr_reader :terminator, :data

        def initialize options={}
          @terminator = options.delete(:terminator) || "\r\n"
          @data = ""
        end
        
        def size
          @data.size
        end
        
        def insert! chunk
          @data << chunk
        end

        def read!
          objects = @data.split(terminator)[0..-2]
          unless objects.empty?
            # Get the number of chars to read from buffer, counting
            # the size of each splited chunk plus the number of chunks by
            # terminator size, since it's still present in the buffer
            size = objects.map(&:size).reduce(:+) + objects.size * terminator.size
            consume!(size)
          end
          return objects
        end

        def consume! chars
          @data[0..chars-1] = ''
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
gnip_api-1.1.3 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-1.1.2 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-1.1.1 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-1.1.0 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-1.0.6 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-1.0.5 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-1.0.4 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-1.0.3 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-1.0.2 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-1.0.1 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-1.0.0 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-0.0.9 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-0.0.8 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-0.0.7 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-0.0.6 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-0.0.5 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-0.0.4 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-0.0.3 lib/gnip_api/apis/power_track/buffer.rb
gnip_api-0.0.2 lib/gnip_api/apis/power_track/buffer.rb