Class: Net::HTTPResponse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

detect the response code

Example: puts HttpRequest.get(‘www.example.com’).code_200? puts HttpRequest.get(‘www.example.com’).code_2xx? HttpRequest.get(‘www.example.com/404.html’) {|http|

puts "IS 4xx" if http.code_4xx?
puts "IS 404" if http.code_404?

}

supported methods code_1xx? code_2xx? code_3xx? code_4xx? code_5xx? code_100? code_101? code_200? code_201? … code_505?



458
459
460
461
462
463
464
465
466
467
# File 'lib/http_request.rb', line 458

def method_missing(method_name)
	case method_name.to_s
	when /^(code|status)_([0-9])xx\?$/
		not CODE_CLASS_TO_OBJ[$2].nil? and is_a? CODE_CLASS_TO_OBJ[$2]
	when /^(code|status)_([0-9]+)\?$/
		not CODE_TO_OBJ[$2].nil? and is_a? CODE_TO_OBJ[$2]
	else
		raise NoHttpMethodException, 'Unknown method of response code!'
	end
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



422
423
424
# File 'lib/http_request.rb', line 422

def url
  @url
end

Instance Method Details

#bodyObject

for gzipped body



430
431
432
433
434
435
436
437
438
# File 'lib/http_request.rb', line 430

def body
	bd = read_body()
	return bd unless bd
	if (self['content-encoding'] == 'gzip') and defined?(::Zlib)
		::Zlib::GzipReader.new(StringIO.new(bd)).read
	else
		bd
	end
end

#cookiesObject

get cookies as a hash



425
426
427
# File 'lib/http_request.rb', line 425

def cookies
	HttpRequest.cookies
end

#raw_bodyObject

body



441
442
443
# File 'lib/http_request.rb', line 441

def raw_body
	read_body()
end