lib/rest/api/client/json_parser.rb in rest-api-client-0.1.9 vs lib/rest/api/client/json_parser.rb in rest-api-client-0.1.10

- old
+ new

@@ -12,15 +12,15 @@ if json_response.kind_of?(Hash) && json_response.has_key?('data') json_data = json_response['data'] end if json_data.kind_of?(Array) - return json_data.map { |data| data_type.new data } if data_type + return json_data.map { |data| self.build_instance data_type, data } if data_type return json_data unless json_data.empty? elsif json_data.kind_of?(Hash) - return data_type.new json_data if data_type + return self.build_instance data_type, json_data if data_type return json_data unless json_data.empty? else return json_data end @@ -29,6 +29,35 @@ rescue Exception => e return (opts[:default_return] || nil) end end + private + + # Set the instance attributes values. + # If the instance have associations, the value of these associations will be an empty object, or an object with his id + # when the instance data contains a "object_id" key + def self.build_instance(data_type, instance_data) + instance = data_type.new + instance_data.each do |key, value| + + begin + if key.end_with? '_id' + key_without_id = key.gsub('_id', '') + if instance.respond_to?(key_without_id) + key = key_without_id + value = Object::const_get("#{data_type.parent}::" + key_without_id.camelize).new(:id => value) + end + end + + if value.is_a?(Hash) + value = Object::const_get("#{data_type.parent}::" + key.camelize).new(value) + end + rescue Exception => e + # ignored + end + + instance.instance_variable_set("@#{key}", value) + end + instance + end end