use default values after parse xml

This commit is contained in:
Benjamin Neff 2016-04-29 02:59:09 +02:00
parent 8aa353a438
commit 28ab54abef
7 changed files with 116 additions and 5 deletions

View file

@ -20,7 +20,7 @@ module DiasporaFederation
# @!attribute [r] messages # @!attribute [r] messages
# @return [[Entities::Message]] Messages of this conversation # @return [[Entities::Message]] Messages of this conversation
entity :messages, [Entities::Message] entity :messages, [Entities::Message], default: []
# @!attribute [r] author # @!attribute [r] author
# The diaspora ID of the person initiated the conversation. # The diaspora ID of the person initiated the conversation.

View file

@ -153,7 +153,7 @@ module DiasporaFederation
end end
def nilify(value) def nilify(value)
return nil if value.respond_to?(:empty?) && value.empty? return nil if value.respond_to?(:empty?) && value.empty? && !value.instance_of?(Array)
value value
end end
@ -207,8 +207,9 @@ module DiasporaFederation
# @return [Hash] entity data # @return [Hash] entity data
def self.entity_data(root_node) def self.entity_data(root_node)
Hash[class_props.map {|name, type| Hash[class_props.map {|name, type|
[name, parse_element_from_node(name, type, root_node)] value = parse_element_from_node(name, type, root_node)
}] [name, value] if value
}.compact]
end end
private_class_method :entity_data private_class_method :entity_data
@ -257,7 +258,7 @@ module DiasporaFederation
# @return [Array<Entity>] array with parsed child entities # @return [Array<Entity>] array with parsed child entities
def self.parse_array_from_node(type, root_node) def self.parse_array_from_node(type, root_node)
node = root_node.xpath(type.entity_name) node = root_node.xpath(type.entity_name)
node.map {|child| type.from_xml(child) } node.map {|child| type.from_xml(child) } unless node.empty?
end end
private_class_method :parse_array_from_node private_class_method :parse_array_from_node

View file

@ -35,5 +35,24 @@ XML
it_behaves_like "an Entity subclass" it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity" it_behaves_like "an XML Entity"
context "default values" do
let(:minimal_xml) {
<<-XML
<conversation>
<guid>#{parent.guid}</guid>
<subject>#{data[:subject]}</subject>
<created_at>#{data[:created_at]}</created_at>
<author>#{data[:author]}</author>
<participant_handles>#{data[:participants]}</participant_handles>
</conversation>
XML
}
it "allows no nested messages" do
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(minimal_xml).root)
expect(parsed_instance.messages).to eq([])
end
end
end end
end end

View file

@ -22,5 +22,28 @@ XML
it_behaves_like "an Entity subclass" it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity" it_behaves_like "an XML Entity"
context "default values" do
let(:minimal_xml) {
<<-XML
<photo>
<guid>#{data[:guid]}</guid>
<author>#{data[:author]}</author>
<created_at>#{data[:created_at]}</created_at>
<remote_photo_path>#{data[:remote_photo_path]}</remote_photo_path>
<remote_photo_name>#{data[:remote_photo_name]}</remote_photo_name>
<status_message_guid>#{data[:status_message_guid]}</status_message_guid>
<height>#{data[:height]}</height>
<width>#{data[:width]}</width>
</photo>
XML
}
it "uses default values" do
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(minimal_xml).root)
expect(parsed_instance.public).to be_falsey
expect(parsed_instance.text).to be_nil
end
end
end end
end end

View file

@ -25,5 +25,31 @@ XML
it_behaves_like "an Entity subclass" it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity" it_behaves_like "an XML Entity"
context "default values" do
let(:minimal_xml) {
<<-XML
<profile>
<author>#{data[:author]}</author>
</profile>
XML
}
it "uses default values" do
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(minimal_xml).root)
expect(parsed_instance.first_name).to be_nil
expect(parsed_instance.last_name).to be_nil
expect(parsed_instance.image_url).to be_nil
expect(parsed_instance.image_url_medium).to be_nil
expect(parsed_instance.image_url_small).to be_nil
expect(parsed_instance.birthday).to be_nil
expect(parsed_instance.gender).to be_nil
expect(parsed_instance.bio).to be_nil
expect(parsed_instance.location).to be_nil
expect(parsed_instance.searchable).to be_truthy
expect(parsed_instance.nsfw).to be_falsey
expect(parsed_instance.tag_string).to be_nil
end
end
end end
end end

View file

@ -19,5 +19,25 @@ XML
it_behaves_like "an Entity subclass" it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity" it_behaves_like "an XML Entity"
context "default values" do
let(:minimal_xml) {
<<-XML
<reshare>
<author>#{data[:author]}</author>
<guid>#{data[:guid]}</guid>
<created_at>#{data[:created_at]}</created_at>
<root_diaspora_id>#{data[:root_author]}</root_diaspora_id>
<root_guid>#{data[:root_guid]}</root_guid>
</reshare>
XML
}
it "uses default values" do
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(minimal_xml).root)
expect(parsed_instance.public).to be_truthy
expect(parsed_instance.provider_display_name).to be_nil
end
end
end end
end end

View file

@ -57,5 +57,27 @@ module DiasporaFederation
it_behaves_like "an Entity subclass" it_behaves_like "an Entity subclass"
it_behaves_like "an XML Entity" it_behaves_like "an XML Entity"
context "default values" do
let(:minimal_xml) {
<<-XML
<status_message>
<author>#{data[:author]}</author>
<guid>#{data[:guid]}</guid>
<created_at>#{data[:created_at]}</created_at>
<raw_message>#{data[:raw_message]}</raw_message>
</status_message>
XML
}
it "uses default values" do
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(minimal_xml).root)
expect(parsed_instance.photos).to eq([])
expect(parsed_instance.location).to be_nil
expect(parsed_instance.poll).to be_nil
expect(parsed_instance.public).to be_falsey
expect(parsed_instance.provider_display_name).to be_nil
end
end
end end
end end