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(json, 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(json, klass=nil, hierarchy=nil)
  @json       = json
  @hierarchy  = hierarchy
  @klass      = klass      
end

Instance Attribute Details

#jsonObject (readonly)

Returns the value of attribute json.



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

def json
  @json
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

#to_objectsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/voorhees/response.rb', line 13

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