Class: Simple::HTTP::Response

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

Constant Summary collapse

ContentTypeParser =
Simple::HTTP::ContentTypeParser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#media_typeObject (readonly)

e.g “text/plain”



30
31
32
# File 'lib/simple/http/response.rb', line 30

def media_type
  @media_type
end

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
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

#bytesObject



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

def bytes
  @body ? @body.bytesize : 0
end

#checked_content(into:) ⇒ Object



104
105
106
107
# File 'lib/simple/http/response.rb', line 104

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

#content(into: nil) ⇒ Object

evaluate and potentially parse response



57
58
59
60
61
62
63
64
65
# File 'lib/simple/http/response.rb', line 57

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

#to_sObject



52
53
54
# File 'lib/simple/http/response.rb', line 52

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