Class: Net::HTTPResponse
- Inherits:
-
Object
- Object
- Net::HTTPResponse
- Defined in:
- lib/http_request.rb
Instance Attribute Summary collapse
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#body ⇒ Object
for gzipped body.
-
#cookies ⇒ Object
get cookies as a hash.
-
#method_missing(method_name) ⇒ Object
detect the response code.
-
#raw_body ⇒ Object
body.
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
#url ⇒ Object
Returns the value of attribute url.
422 423 424 |
# File 'lib/http_request.rb', line 422 def url @url end |
Instance Method Details
#body ⇒ Object
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 |
#cookies ⇒ Object
get cookies as a hash
425 426 427 |
# File 'lib/http_request.rb', line 425 def HttpRequest. end |
#raw_body ⇒ Object
body
441 442 443 |
# File 'lib/http_request.rb', line 441 def raw_body read_body() end |