Class: Http2::Response

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

Overview

This object will be returned as the response for each request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body: "", debug: false, headers: {}, request:) ⇒ Response

This method should not be called manually.



9
10
11
12
13
14
15
# File 'lib/http2/response.rb', line 9

def initialize(body: "", debug: false, headers: {}, request:)
  @body = body
  @debug = debug
  @headers = headers
  @request = request
  @requested_url = request.path
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



5
6
7
# File 'lib/http2/response.rb', line 5

def body
  @body
end

#charsetObject

Returns the value of attribute charset.



5
6
7
# File 'lib/http2/response.rb', line 5

def charset
  @charset
end

#codeObject

Returns the value of attribute code.



5
6
7
# File 'lib/http2/response.rb', line 5

def code
  @code
end

#content_typeObject



45
46
47
48
49
50
51
# File 'lib/http2/response.rb', line 45

def content_type
  if header?("content-type")
    header("content-type")
  else
    raise "No content-type was given."
  end
end

#headersObject (readonly)

All the data the response contains. Headers, body, cookies, requested URL and more.



4
5
6
# File 'lib/http2/response.rb', line 4

def headers
  @headers
end

#http_versionObject

Returns the value of attribute http_version.



5
6
7
# File 'lib/http2/response.rb', line 5

def http_version
  @http_version
end

#requestObject (readonly)

All the data the response contains. Headers, body, cookies, requested URL and more.



4
5
6
# File 'lib/http2/response.rb', line 4

def request
  @request
end

#request_argsObject (readonly)

All the data the response contains. Headers, body, cookies, requested URL and more.



4
5
6
# File 'lib/http2/response.rb', line 4

def request_args
  @request_args
end

#requested_urlObject (readonly)

All the data the response contains. Headers, body, cookies, requested URL and more.



4
5
6
# File 'lib/http2/response.rb', line 4

def requested_url
  @requested_url
end

Instance Method Details

#content_lengthObject



35
36
37
38
39
40
41
42
43
# File 'lib/http2/response.rb', line 35

def content_length
  if header?("content-length")
    header("content-length").to_i
  elsif @body
    @body.bytesize
  else
    raise "Couldn't calculate content-length."
  end
end

#header(key) ⇒ Object

Returns a certain header by name or false if not found.

Examples

val = res.header(“content-type”)



20
21
22
23
24
# File 'lib/http2/response.rb', line 20

def header(key)
  return false unless headers.key?(key)

  headers.fetch(key).first.to_s
end

#header?(key) ⇒ Boolean

Returns true if a header of the given string exists.

Examples

print “No content-type was given.” if !http.header?(“content-type”)

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/http2/response.rb', line 29

def header?(key)
  return true if headers.key?(key) && !headers[key].first.to_s.empty?

  false
end

#hostObject



68
69
70
# File 'lib/http2/response.rb', line 68

def host
  @request.http2.host
end

#inspectObject



88
89
90
# File 'lib/http2/response.rb', line 88

def inspect
  to_s
end

#jsonObject



64
65
66
# File 'lib/http2/response.rb', line 64

def json
  @json ||= JSON.parse(body)
end

#json?Boolean

Returns true if the result is JSON.

Returns:

  • (Boolean)


60
61
62
# File 'lib/http2/response.rb', line 60

def json?
  content_type == "application/json"
end

#pathObject



80
81
82
# File 'lib/http2/response.rb', line 80

def path
  @request.path
end

#portObject



72
73
74
# File 'lib/http2/response.rb', line 72

def port
  @request.http2.port
end

#ssl?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/http2/response.rb', line 76

def ssl?
  @request.http2.ssl?
end

#to_sObject



84
85
86
# File 'lib/http2/response.rb', line 84

def to_s
  "#<Http::Response host=\"#{host}\" port=#{port} ssl=#{ssl?} path=\"#{path}\">"
end

#validate!Object

Checks the data that has been sat on the object and raises various exceptions, if it does not validate somehow.



54
55
56
57
# File 'lib/http2/response.rb', line 54

def validate!
  puts "Http2: Validating response length." if @debug
  validate_body_versus_content_length!
end