Class: Fetch::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, status:, headers:, body:, redirected:) ⇒ Response

Returns a new instance of Response.



6
7
8
9
10
11
12
# File 'lib/fetch/response.rb', line 6

def initialize(url:, status:, headers:, body:, redirected:)
  @url        = url
  @status     = status
  @headers    = headers
  @body       = body
  @redirected = redirected
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



14
15
16
# File 'lib/fetch/response.rb', line 14

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



14
15
16
# File 'lib/fetch/response.rb', line 14

def headers
  @headers
end

#redirectedObject (readonly)

Returns the value of attribute redirected.



14
15
16
# File 'lib/fetch/response.rb', line 14

def redirected
  @redirected
end

#statusObject (readonly)

Returns the value of attribute status.



14
15
16
# File 'lib/fetch/response.rb', line 14

def status
  @status
end

#urlObject (readonly)

Returns the value of attribute url.



14
15
16
# File 'lib/fetch/response.rb', line 14

def url
  @url
end

Instance Method Details

#json(**json_parse_options) ⇒ Object



24
25
26
27
28
# File 'lib/fetch/response.rb', line 24

def json(**json_parse_options)
  return nil unless body

  JSON.parse(body, **Fetch.config.json_parse_options, **json_parse_options)
end

#okObject



16
17
18
# File 'lib/fetch/response.rb', line 16

def ok
  status.between?(200, 299)
end

#status_textObject



20
21
22
# File 'lib/fetch/response.rb', line 20

def status_text
  Rack::Utils::HTTP_STATUS_CODES[status]
end