Class: Voorhees::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/voorhees/response.rb', line 5

def body
  @body
end

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/voorhees/response.rb', line 5

def klass
  @klass
end

Instance Method Details

#jsonObject



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_objectsObject



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