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.



581
582
583
584
585
# File 'lib/httpclient/http.rb', line 581

def initialize
  @body = []
  @size = 0
  @as_stream = false
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



579
580
581
# File 'lib/httpclient/http.rb', line 579

def size
  @size
end

Instance Method Details

#add(part) ⇒ Object



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/httpclient/http.rb', line 587

def add(part)
  if Message.file?(part)
    @as_stream = true
    @body << part
    if part.respond_to?(:size)
      if sz = part.size
        @size += sz
      else
        @size = nil
      end
    elsif part.respond_to?(:lstat)
      @size += part.lstat.size
    else
      # use chunked upload
      @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



612
613
614
615
616
617
618
# File 'lib/httpclient/http.rb', line 612

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