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(args = {}) ⇒ Response

This method should not be called manually.



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

def initialize(args = {})
  @args = args
  @args[:headers] ||= {}
  @body = args[:body] || ""
  @debug = args[:debug]
  @request = args.fetch(:request)
end

Instance Attribute Details

#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 args
  @args
end

#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

Returns the value of attribute content_type.



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

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

Instance Method Details

#content_lengthObject



39
40
41
42
43
44
45
46
47
# File 'lib/http2/response.rb', line 39

def content_length
  if header?("content-length")
    header("content-length").to_i
  elsif @body
    return @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”)



26
27
28
29
# File 'lib/http2/response.rb', line 26

def header(key)
  return false unless @args.fetch(:headers).key?(key)
  @args.fetch(: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)


34
35
36
37
# File 'lib/http2/response.rb', line 34

def header?(key)
  return true if @args[:headers].key?(key) && @args[:headers][key].first.to_s.length > 0
  false
end

#headersObject

Returns headers given from the host for the result.

Examples

headers_hash = res.headers



19
20
21
# File 'lib/http2/response.rb', line 19

def headers
  @args.fetch(:headers)
end

#hostObject



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

def host
  @request.http2.host
end

#inspectObject



100
101
102
# File 'lib/http2/response.rb', line 100

def inspect
  to_s
end

#jsonObject



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

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

#json?Boolean

Returns true if the result is JSON.

Returns:

  • (Boolean)


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

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

#pathObject



92
93
94
# File 'lib/http2/response.rb', line 92

def path
  @request.path
end

#portObject



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

def port
  @request.http2.port
end

#requested_urlObject

Returns the requested URL as a string.

Examples

res.requested_url #=> “?show=status&action=getstatus”



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

def requested_url
  raise "URL could not be detected." unless @args[:request_args][:url]
  @args[:request_args][:url]
end

#ssl?Boolean

Returns:

  • (Boolean)


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

def ssl?
  @request.http2.ssl?
end

#to_sObject



96
97
98
# File 'lib/http2/response.rb', line 96

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.



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

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