Class: Cloudflare::HTTPResponse

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

Overview

Lightweight response wrapper around the JS Response object. The body is read eagerly (text()) so callers see a plain Ruby String. ‘headers` is a frozen Hash with lowercased string keys.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, headers:, body:, url:) ⇒ HTTPResponse

Returns a new instance of HTTPResponse.



36
37
38
39
40
41
# File 'lib/cloudflare_workers/http.rb', line 36

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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



34
35
36
# File 'lib/cloudflare_workers/http.rb', line 34

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



34
35
36
# File 'lib/cloudflare_workers/http.rb', line 34

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



34
35
36
# File 'lib/cloudflare_workers/http.rb', line 34

def status
  @status
end

#urlObject (readonly)

Returns the value of attribute url.



34
35
36
# File 'lib/cloudflare_workers/http.rb', line 34

def url
  @url
end

Instance Method Details

#[](name) ⇒ Object



51
52
53
# File 'lib/cloudflare_workers/http.rb', line 51

def [](name)
  @headers[name.to_s.downcase]
end

#jsonObject



47
48
49
# File 'lib/cloudflare_workers/http.rb', line 47

def json
  JSON.parse(@body)
end

#ok?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/cloudflare_workers/http.rb', line 43

def ok?
  @status >= 200 && @status < 300
end