Class: Voorhees::Response
- Inherits:
-
Object
- Object
- Voorhees::Response
- Defined in:
- lib/voorhees/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
-
#initialize(body, klass = nil, hierarchy = nil) ⇒ Response
constructor
A new instance of Response.
- #json ⇒ Object
- #to_objects ⇒ Object
Constructor Details
#initialize(body, klass = nil, hierarchy = nil) ⇒ Response
Returns a new instance of Response.
7 8 9 10 11 |
# File 'lib/voorhees/response.rb', line 7 def initialize(body, klass=nil, hierarchy=nil) @body = body @hierarchy = hierarchy @klass = klass end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
5 6 7 |
# File 'lib/voorhees/response.rb', line 5 def body @body end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/voorhees/response.rb', line 5 def klass @klass end |
Instance Method Details
#json ⇒ Object
13 14 15 16 17 18 |
# File 'lib/voorhees/response.rb', line 13 def json @json ||= JSON.parse(@body) rescue JSON::ParserError Voorhees.debug("Parsing JSON failed.\nFirst 500 chars of body:\n#{response.body[0...500]}") raise Voorhees::ParseError end |
#to_objects ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/voorhees/response.rb', line 20 def to_objects return unless @klass raise Voorhees::NotResourceError.new unless @klass.respond_to?(:new_from_json) if json.is_a?(Array) json.collect do |item| @klass.new_from_json(item, @hierarchy) end else @klass.new_from_json(json, @hierarchy) end end |