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?



480
481
482
483
484
485
486
487
488
489
# File 'lib/http_request.rb', line 480

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.



444
445
446
# File 'lib/http_request.rb', line 444

def url
  @url
end

Instance Method Details

#bodyObject

for gzipped body



452
453
454
455
456
457
458
459
460
# File 'lib/http_request.rb', line 452

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



447
448
449
# File 'lib/http_request.rb', line 447

def cookies
  HttpRequest.cookies
end

#raw_bodyObject

body



463
464
465
# File 'lib/http_request.rb', line 463

def raw_body
  read_body()
end