class HTTP::Request::Writer

def add_body_type_headers

with
Adds the headers to the header array for the given request body we are working
def add_body_type_headers
  if @body.is_a?(String) && !@headers[Headers::CONTENT_LENGTH]
    @request_header << "#{Headers::CONTENT_LENGTH}: #{@body.bytesize}"
  elsif @body.is_a?(Enumerable) && CHUNKED != @headers[Headers::TRANSFER_ENCODING]
    fail(RequestError, "invalid transfer encoding")
  end
end

def add_headers

Adds headers to the request header from the headers array
def add_headers
  @headers.each do |field, value|
    @request_header << "#{field}: #{value}"
  end
end

def connect_through_proxy

Send headers needed to connect through proxy
def connect_through_proxy
  add_headers
  write(join_headers)
end

def initialize(socket, body, headers, headline) # rubocop:disable ParameterLists

rubocop:disable ParameterLists
def initialize(socket, body, headers, headline) # rubocop:disable ParameterLists
  @body           = body
  @socket         = socket
  @headers        = headers
  @request_header = [headline]
  validate_body_type!
end

def join_headers

http request header string
Joins the headers specified in the request into a correctly formatted
def join_headers
  # join the headers array with crlfs, stick two on the end because
  # that ends the request header
  @request_header.join(CRLF) + (CRLF) * 2
end

def send_request_body

def send_request_body
  if @body.is_a?(String)
    write(@body)
  elsif @body.is_a?(Enumerable)
    @body.each do |chunk|
      write(chunk.bytesize.to_s(16) << CRLF)
      write(chunk << CRLF)
    end
    write(CHUNKED_END)
  end
end

def send_request_header

def send_request_header
  add_headers
  add_body_type_headers
  write(join_headers)
end

def stream

Stream the request to a socket
def stream
  send_request_header
  send_request_body
end

def validate_body_type!

def validate_body_type!
  return if VALID_BODY_TYPES.any? { |type| @body.is_a? type }
  fail RequestError, "body of wrong type: #{@body.class}"
end

def write(data)

def write(data)
  while data.present?
    length = @socket.write(data)
    if data.length > length
      data = data[length..-1]
    else
      break
    end
  end
end