Sha256: 869f3f9d6761f387a33fdd7ccace54715c246546379cbb1456959c0c4f3e4a4a

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

module Esse
  module Import
    class RequestBody
      attr_reader :body, :stats

      def initialize(body:)
        @body = body # body may be String or Array<Hash>
        @stats = { index: 0, create: 0, delete: 0 }
      end

      def body?
        !body.empty?
      end
    end

    class RequestBodyRaw < RequestBody
      def initialize
        super(body: '')
      end

      def bytesize
        body.bytesize
      end

      def add(operation, payload)
        stats[operation] += 1
        if @body.empty?
          @body = payload
        else
          @body << "\n" << payload
        end
      end

      def finalize
        @body << "\n"
      end
    end

    class RequestBodyAsJson < RequestBody
      def initialize
        super(body: [])
      end

      def index=(docs)
        @body += docs
        @stats[:index] += docs.size
      end

      def create=(docs)
        @body += docs
        @stats[:create] += docs.size
      end

      def delete=(docs)
        @body += docs
        @stats[:delete] += docs.size
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
esse-0.2.6 lib/esse/import/request_body.rb
esse-0.2.5 lib/esse/import/request_body.rb
esse-0.2.4 lib/esse/import/request_body.rb
esse-0.2.3 lib/esse/import/request_body.rb
esse-0.2.2 lib/esse/import/request_body.rb