Class: URL::Response
- Inherits:
-
String
- Object
- String
- URL::Response
- 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
-
#code ⇒ Object
readonly
The http code return.
-
#response ⇒ Object
readonly
The response object generated by the handler.
-
#time ⇒ Object
readonly
The time taken for the request.
-
#url ⇒ Object
readonly
The url which generated this response.
Instance Method Summary collapse
-
#initialize(str, args = {}) ⇒ Response
constructor
A new instance of Response.
- #json ⇒ Object
-
#success? ⇒ Boolean
Compares #code to 2xx.
Constructor Details
#initialize(str, args = {}) ⇒ Response
Returns a new instance of Response.
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
#code ⇒ Object (readonly)
The http code return
20 21 22 |
# File 'lib/url/response.rb', line 20 def code @code end |
#response ⇒ Object (readonly)
The response object generated by the handler
24 25 26 |
# File 'lib/url/response.rb', line 24 def response @response end |
#time ⇒ Object (readonly)
The time taken for the request
16 17 18 |
# File 'lib/url/response.rb', line 16 def time @time end |
#url ⇒ Object (readonly)
The url which generated this response
28 29 30 |
# File 'lib/url/response.rb', line 28 def url @url end |
Instance Method Details
#json ⇒ Object
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
47 48 49 50 51 |
# File 'lib/url/response.rb', line 47 def success? return @successful if @successful (200..299).include?(code) end |