From 7b4753148b84c85d929603dbedea591fe322d42c Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Fri, 25 Sep 2015 22:42:22 +0200 Subject: [PATCH] split populate_entity in xml_payload --- lib/diaspora_federation/salmon/xml_payload.rb | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/diaspora_federation/salmon/xml_payload.rb b/lib/diaspora_federation/salmon/xml_payload.rb index 664ac73..8d2b54d 100644 --- a/lib/diaspora_federation/salmon/xml_payload.rb +++ b/lib/diaspora_federation/salmon/xml_payload.rb @@ -91,17 +91,11 @@ module DiasporaFederation type = prop_def[:type] if type == String - # create simple entry in data hash - n = node.xpath(name.to_s) - data[name] = n.first.text if n.any? + data[name] = parse_string_from_node(name, node) elsif type.instance_of?(Array) - # collect all nested children of that type and create an array in the data hash - n = node.xpath(type.first.entity_name) - data[name] = n.map {|child| populate_entity(type.first, child) } + data[name] = parse_array_from_node(type, node) elsif type.ancestors.include?(Entity) - # create an entry in the data hash for the nested entity - n = node.xpath(type.entity_name) - data[name] = populate_entity(type, n.first) if n.any? + data[name] = parse_entity_from_node(type, node) end end @@ -109,6 +103,30 @@ module DiasporaFederation end private_class_method :populate_entity + # create simple entry in data hash + # @return [String] data + def self.parse_string_from_node(name, node) + n = node.xpath(name.to_s) + n.first.text if n.any? + end + private_class_method :parse_string_from_node + + # create an entry in the data hash for the nested entity + # @return [Entity] parsed child entity + def self.parse_entity_from_node(type, node) + n = node.xpath(type.entity_name) + populate_entity(type, n.first) if n.any? + end + private_class_method :parse_entity_from_node + + # collect all nested children of that type and create an array in the data hash + # @return [Array] array with parsed child entities + def self.parse_array_from_node(type, node) + n = node.xpath(type.first.entity_name) + n.map {|child| populate_entity(type.first, child) } + end + private_class_method :parse_array_from_node + # Raised, if the XML structure of the parsed document doesn't resemble the # expected structure. class InvalidStructure < RuntimeError