Class: Protocol::HTTP::Body::Head

Inherits:
Readable
  • Object
show all
Defined in:
lib/protocol/http/body/head.rb

Overview

Represents a body suitable for HEAD requests, in other words, a body that is empty and has a known length.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Readable

#as_json, #buffered, #call, #close, #discard, #each, #finish, #join, #read, #rewind, #rewindable?, #stream?, #to_json

Constructor Details

#initialize(length) ⇒ Head

Initialize the head body with the given length.



38
39
40
# File 'lib/protocol/http/body/head.rb', line 38

def initialize(length)
  @length = length
end

Class Method Details

.for(body, length = nil) ⇒ Object

Create a head body for the given body, capturing its length and then closing it.

If a body is provided, the length is determined from the body, and the body is closed. If no body is provided, and the content length is provided, a head body is created with that length. This is useful for creating a head body when you only know the content length but not the actual body, which may happen in adapters for HTTP applications where the application may not provide a body for HEAD requests, but the content length is known.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/protocol/http/body/head.rb', line 23

def self.for(body, length = nil)
  if body
    head = self.new(body.length)
    body.close
    return head
  elsif length
    return self.new(length)
  end
  
  return nil
end

Instance Method Details

#empty?Boolean



43
44
45
# File 'lib/protocol/http/body/head.rb', line 43

def empty?
  true
end

#inspectObject

Inspect the head body.



60
61
62
# File 'lib/protocol/http/body/head.rb', line 60

def inspect
  "#<#{self.class} #{@length} bytes (empty)>"
end

#lengthObject



53
54
55
# File 'lib/protocol/http/body/head.rb', line 53

def length
  @length
end

#ready?Boolean



48
49
50
# File 'lib/protocol/http/body/head.rb', line 48

def ready?
  true
end