Sha256: f49429ce0452fb2dae2d09574e43b98f1fa64d9d3604a681747dcb2e4b81a286

Contents?: true

Size: 1021 Bytes

Versions: 1

Compression:

Stored size: 1021 Bytes

Contents

require 'zlib'
require 'digest'

module NFAgent
  class Chunk
    attr_reader :created_at

    ::DEFAULT_TIME_OUT = 60

    def initialize(max_size = 500)
      @max_size = max_size
      @created_at = Time.now
      @array = []
      @submitter = Submitter.new(Config.client_key)
    end

    def <<(line)
      @array << line
    end

    def full?
      @array.size >= @max_size
    end

    def expired?
      (Time.now - @created_at > ::DEFAULT_TIME_OUT) && !@array.empty?
    end

    # TODO: Is this the right place for compression, encoding and check summing? Perhaps it should go into the submitter to that it can be deferred
    def dump
      Payload.new do |payload|
        Log.info("Dumping payload from chunk (#{@array.size} lines)")
        payload.data = Encoder.encode64url(Zlib::Deflate.deflate(@array.join("\n"), Zlib::BEST_COMPRESSION))
        payload.checksum = Digest::SHA1.hexdigest(payload.data)
      end
    end

    def clear
      @array.clear
      @created_at = Time.now
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nfagent-0.9.11 lib/nfagent/chunk.rb