Class: URL::Response

Inherits:
String
  • Object
show all
Defined in:
lib/url/response.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, args = {}) ⇒ Response

Returns a new instance of Response.

Parameters:

  • body (String)

    The body of the response object, main string

  • args (Hash) (defaults to: {})

    Additional arguments: :time,:code,:response,:url



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/url/response.rb', line 32

def initialize(str,args={})
  if str.is_a?(Hash)
    args = str
    str = args[:body]
  end
  
  raise unless str
  super(str)
  args.each do |key, value|
    instance_variable_set "@#{key}", value
  end
end

Instance Attribute Details

#codeObject (readonly)

The http code return



20
21
22
# File 'lib/url/response.rb', line 20

def code
  @code
end

#responseObject (readonly)

The response object generated by the handler



24
25
26
# File 'lib/url/response.rb', line 24

def response
  @response
end

#timeObject (readonly)

The time taken for the request



16
17
18
# File 'lib/url/response.rb', line 16

def time
  @time
end

#urlObject (readonly)

The url which generated this response



28
29
30
# File 'lib/url/response.rb', line 28

def url
  @url
end

Instance Method Details

#jsonObject

Raises:

  • (StandardError)


53
54
55
56
# File 'lib/url/response.rb', line 53

def json
  raise StandardError, 'No JSON library initialized' unless URL.json_handler
  URL.json_handler.new(self).parse
end

#success?Boolean

Compares #code to 2xx

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/url/response.rb', line 47

def success?
  return @successful if @successful
  
  (200..299).include?(code)
end