class URL::Response

The Response class is a deleegate to string which also contains metadata about the request. These methods are also available

Attributes

code[R]

The http code return @returns [Integer] eg. 200, 404, 500, 503

response[R]

The response object generated by the handler @returns [Net::HTTPResponse,Typhoeus::Response]

time[R]

The time taken for the request @returns [Integer]

url[R]

The url which generated this response @returns [String]

url_obj[R]

The url object used to create the response @returns [URL]

Public Class Methods

new(str,args={}) click to toggle source

@param [String] body The body of the response object, main string @param [Hash] args Additional arguments: :time,:code,:response,:url

Calls superclass method
# 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

connection_refused() click to toggle source

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
json() click to toggle source

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
success?() click to toggle source

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
Also aliased as: successful?
successful?()
Alias for: success?