Class: Simple::HTTP::Response

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

Constant Summary collapse

BodyBuilder =
Simple::HTTP::BodyBuilder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



26
27
28
# File 'lib/simple/http/response.rb', line 26

def headers
  @headers
end

#messageObject (readonly)

Returns the value of attribute message.



25
26
27
# File 'lib/simple/http/response.rb', line 25

def message
  @message
end

#original_bodyObject (readonly)

Returns the value of attribute original_body.



27
28
29
# File 'lib/simple/http/response.rb', line 27

def original_body
  @original_body
end

#requestObject (readonly)

Returns the value of attribute request.



23
24
25
# File 'lib/simple/http/response.rb', line 23

def request
  @request
end

#statusObject (readonly)

Returns the value of attribute status.



24
25
26
# File 'lib/simple/http/response.rb', line 24

def status
  @status
end

Class Method Details

.body_permitted?(verb) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
# File 'lib/simple/http/response.rb', line 13

def body_permitted?(verb)
  return false if verb == :HEAD
  return false if verb == :OPTIONS

  true
end

.build(request:, body:, headers:, status:, message:) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/simple/http/response.rb', line 5

def build(request:, body:, headers:, status:, message:)
  unless body_permitted?(request.verb)
    body = nil
  end

  new request, body, headers, status, message
end

Instance Method Details

#bodyObject

returns the body

This method reencodes the text body into UTF-8. Non-text bodies should be encoded as ASCII-8BIT (a.k.a. “BINARY”)



57
58
59
# File 'lib/simple/http/response.rb', line 57

def body
  @body ||= @body_builder.reencode(@original_body)
end

#bytesObject



61
62
63
# File 'lib/simple/http/response.rb', line 61

def bytes
  @original_body&.bytesize || 0
end

#checked_content(into:) ⇒ Object



117
118
119
120
# File 'lib/simple/http/response.rb', line 117

def checked_content(into:)
  check_response_status!
  content(into: into)
end

#content(into: nil) ⇒ Object

evaluate and potentially parse response



70
71
72
73
74
75
76
77
78
# File 'lib/simple/http/response.rb', line 70

def content(into: nil)
  return parsed_content if into.nil?

  if parsed_content.is_a?(Array)
    parsed_content.map { |entry| convert_into(entry, into: into) }
  else
    convert_into(parsed_content, into: into)
  end
end

#media_typeObject

e.g “text/plain”



49
50
51
# File 'lib/simple/http/response.rb', line 49

def media_type
  @body_builder.media_type
end

#to_sObject



65
66
67
# File 'lib/simple/http/response.rb', line 65

def to_s
  "#{status} #{message.gsub(/\s+$/, "")} (#{bytes} byte)"
end