Class: HTTP::Message::Body::Parts

Inherits:
Object
  • Object
show all
Defined in:
lib/httpclient/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParts

Returns a new instance of Parts.


610
611
612
613
614
615
# File 'lib/httpclient/http.rb', line 610

def initialize
  @body = []
  @sizes = {}
  @size = 0 # total
  @as_stream = false
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size


607
608
609
# File 'lib/httpclient/http.rb', line 607

def size
  @size
end

#sizesObject (readonly)

Returns the value of attribute sizes


608
609
610
# File 'lib/httpclient/http.rb', line 608

def sizes
  @sizes
end

Instance Method Details

#add(part) ⇒ Object


617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/httpclient/http.rb', line 617

def add(part)
  if Message.file?(part)
    @as_stream = true
    @body << part
    if part.respond_to?(:lstat)
      sz = part.lstat.size
      add_size(part, sz)
    elsif part.respond_to?(:size)
      if sz = part.size
        add_size(part, sz)
      else
        @sizes.clear
        @size = nil
      end
    else
      # use chunked upload
      @sizes.clear
      @size = nil
    end
  elsif @body[-1].is_a?(String)
    @body[-1] += part.to_s
    @size += part.to_s.bytesize if @size
  else
    @body << part.to_s
    @size += part.to_s.bytesize if @size
  end
end

#partsObject


645
646
647
648
649
650
651
# File 'lib/httpclient/http.rb', line 645

def parts
  if @as_stream
    @body
  else
    [@body.join]
  end
end