diff --git a/lib/diaspora_federation/entity.rb b/lib/diaspora_federation/entity.rb index fa3793f..814d447 100644 --- a/lib/diaspora_federation/entity.rb +++ b/lib/diaspora_federation/entity.rb @@ -50,7 +50,7 @@ module DiasporaFederation end self.class.default_values.merge(data).each do |k, v| - instance_variable_set("@#{k}", v) if setable?(k, v) + instance_variable_set("@#{k}", nilify(v)) if setable?(k, v) end freeze end @@ -113,6 +113,11 @@ module DiasporaFederation val.all? {|v| v.instance_of?(t.first) }) end + def nilify(value) + return nil if value.respond_to?(:empty?) && value.empty? + value + end + # Serialize the Entity into XML elements # @return [Nokogiri::XML::Element] root node def entity_xml diff --git a/spec/lib/diaspora_federation/discovery/h_card_spec.rb b/spec/lib/diaspora_federation/discovery/h_card_spec.rb index f4cf1c4..507a265 100644 --- a/spec/lib/diaspora_federation/discovery/h_card_spec.rb +++ b/spec/lib/diaspora_federation/discovery/h_card_spec.rb @@ -152,6 +152,25 @@ HTML expect(hcard.searchable).to eq(false) end + it "name is nil if empty" do + changed_html = html.sub( + "class=\"fn\">#{person.full_name}<", + "class=\"fn\"><" + ).sub( + "class=\"given_name\">#{person.first_name}<", + "class=\"given_name\"><" + ).sub( + "class=\"family_name\">#{person.last_name}<", + "class=\"family_name\"><" + ) + + hcard = Discovery::HCard.from_html(changed_html) + + expect(hcard.full_name).to be_nil + expect(hcard.first_name).to be_nil + expect(hcard.last_name).to be_nil + end + it "reads old-style HTML" do historic_html = <<-HTML