include/response.rb in http2-0.0.29 vs include/response.rb in http2-0.0.30

- old
+ new

@@ -34,13 +34,27 @@ return true if @args[:headers].key?(key) && @args[:headers][key].first.to_s.length > 0 return false end def content_length - header("content-length").to_i if header?("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 + def content_type + if header?("content-type") + return header("content-type") + else + raise "No content-type was given." + end + end + #Returns the requested URL as a string. #===Examples # res.requested_url #=> "?show=status&action=getstatus" def requested_url raise "URL could not be detected." unless @args[:request_args][:url] @@ -49,9 +63,18 @@ # Checks the data that has been sat on the object and raises various exceptions, if it does not validate somehow. def validate! puts "Http2: Validating response length." if @debug validate_body_versus_content_length! + end + + # Returns true if the result is JSON. + def json? + content_type == "application/json" + end + + def json + @json ||= JSON.parse(body) end private # Checks that the length of the body is the same as the given content-length if given.