Sha256: c236199b9570787024dcdd5bcb111c935cc486a687993ca4609508ea0879160e

Contents?: true

Size: 1.55 KB

Versions: 7

Compression:

Stored size: 1.55 KB

Contents

module Reel
  class Request
    # Represents the bodies of Requests
    class Body
      include Enumerable

      def initialize(request)
        @request   = request
        @streaming = nil
        @contents  = nil
      end

      # Read exactly the given amount of data
      def read(length)
        stream!
        @request.read(length)
      end

      # Read up to length bytes, but return any data that's available
      def readpartial(length = nil)
        stream!
        @request.readpartial(length)
      end

      # Iterate over the body, allowing it to be enumerable
      def each
        while chunk = readpartial
          yield chunk
        end
      end

      def empty?
        to_str.empty?
      end

      # Eagerly consume the entire body as a string
      def to_str
        return @contents if @contents
        raise StateError, "body is being streamed" unless @streaming.nil?

        begin
          @streaming = false
          @contents = ""
          while chunk = @request.readpartial
            @contents << chunk
          end
        rescue
          @contents = nil
          raise
        end

        @contents
      end
      alias_method :to_s, :to_str

      # Easier to interpret string inspect
      def inspect
        "#<#{self.class}:#{object_id.to_s(16)} @streaming=#{!!@streaming}>"
      end

      # Assert that the body is actively being streamed
      def stream!
        raise StateError, "body has already been consumed" if @streaming == false
        @streaming = true
      end
      private :stream!
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
reel-0.6.1 lib/reel/request/body.rb
reel-0.6.0 lib/reel/request/body.rb
reel-0.6.0.pre5 lib/reel/request/body.rb
reel-0.6.0.pre4 lib/reel/request/body.rb
reel-0.6.0.pre3 lib/reel/request/body.rb
reel-0.6.0.pre2 lib/reel/request/body.rb
reel-0.6.0.pre1 lib/reel/request/body.rb