class URL::Response
The Response class is a deleegate to string which also contains metadata about the request. These methods are also available
-
body
-
code - http code
-
response - the original response object from whatever handler you chose
-
time - time taken to make call
-
success? - whether the http code is 2xx
-
url - the URL the object was gotten from
Attributes
The http code return @returns [Integer] eg. 200, 404, 500, 503
The response object generated by the handler @returns [Net::HTTPResponse,Typhoeus::Response]
The time taken for the request @returns [Integer]
The url which generated this response @returns [String]
The url object used to create the response @returns [URL]
Public Class Methods
@param [String] body The body of the response object, main string @param [Hash] args Additional arguments: :time,:code,:response,:url
# File lib/url/response.rb, line 40 def initialize(str,args={}) if str.is_a?(Hash) args = str str = args[:body] end raise ArgumentError, "No string provided" unless str super(str) args.each do |key, value| instance_variable_set "@#{key}", value end end
Public Instance Methods
This is set to true if the target server was not reachable @returns [Boolean]
# File lib/url/response.rb, line 34 def connection_refused @connection_refused || code == 0 end
Attempt to parse the response as json @returns [Hash]
# File lib/url/response.rb, line 64 def json raise StandardError, 'No JSON library initialized' unless URL.json_handler @json ||= URL.json_handler.new(self).parse end
Compares {Response#code} to 2xx @returns [true,false]
# File lib/url/response.rb, line 55 def success? return @successful if @successful (200..299).include?(code) end