From 4bde03e2de6c9348b6033b18edacf98ea1d7c55a Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sun, 5 Jun 2016 04:13:55 +0200 Subject: [PATCH] use RelatedEntity as normal nested entity, but don't add it to xml --- .../entities/related_entity.rb | 5 +++++ lib/diaspora_federation/entities/relayable.rb | 19 +++++++++---------- .../entities/relayable_retraction.rb | 16 ++++------------ .../entities/retraction.rb | 16 ++++------------ .../entities/signed_retraction.rb | 16 ++++------------ lib/diaspora_federation/entity.rb | 3 ++- spec/entities.rb | 5 +++++ .../entities/comment_spec.rb | 2 +- .../diaspora_federation/entities/like_spec.rb | 2 +- .../entities/message_spec.rb | 2 +- .../entities/participation_spec.rb | 2 +- .../entities/poll_participation_spec.rb | 2 +- .../entities/related_entity_spec.rb | 6 ++++++ .../entities/relayable_retraction_spec.rb | 4 ++-- .../entities/retraction_spec.rb | 2 +- .../entities/signed_retraction_spec.rb | 2 +- spec/lib/diaspora_federation/entity_spec.rb | 7 +++++++ spec/support/shared_entity_specs.rb | 8 ++++---- 18 files changed, 59 insertions(+), 60 deletions(-) diff --git a/lib/diaspora_federation/entities/related_entity.rb b/lib/diaspora_federation/entities/related_entity.rb index eb69012..9acf788 100644 --- a/lib/diaspora_federation/entities/related_entity.rb +++ b/lib/diaspora_federation/entities/related_entity.rb @@ -23,6 +23,11 @@ module DiasporaFederation # if the entity also have a parent (Comment or Like), +nil+ if it has no parent # @return [RelatedEntity] parent entity entity :parent, Entities::RelatedEntity, default: nil + + # never add {RelatedEntity} to xml + def to_xml + nil + end end end end diff --git a/lib/diaspora_federation/entities/relayable.rb b/lib/diaspora_federation/entities/relayable.rb index d3a686d..8acb890 100644 --- a/lib/diaspora_federation/entities/relayable.rb +++ b/lib/diaspora_federation/entities/relayable.rb @@ -10,10 +10,6 @@ module DiasporaFederation # digest instance used for signing DIGEST = OpenSSL::Digest::SHA256.new - # parent entity - # @return [RelatedEntity] parent entity - attr_reader :parent - # order from the parsed xml for signature # @return [Array] order from xml attr_reader :xml_order @@ -49,20 +45,24 @@ module DiasporaFederation # This signature is required only when federation from upstream (parent) post author to # downstream subscribers. This is the case when the parent author has to resend a relayable # received from one of his subscribers to all others. - # # @return [String] parent author signature # - # @param [Entity] entity the entity in which it is included - def self.included(entity) - entity.class_eval do + # @!attribute [r] parent + # meta information about the parent object + # @return [RelatedEntity] parent entity + # + # @param [Entity] klass the entity in which it is included + def self.included(klass) + klass.class_eval do property :author, xml_name: :diaspora_handle property :guid property :parent_guid property :author_signature, default: nil property :parent_author_signature, default: nil + entity :parent, Entities::RelatedEntity end - entity.extend ParseXML + klass.extend ParseXML end # Initializes a new relayable Entity with order and additional xml elements @@ -72,7 +72,6 @@ module DiasporaFederation # @param [Hash] additional_xml_elements additional xml elements # @see DiasporaFederation::Entity#initialize def initialize(data, xml_order=nil, additional_xml_elements={}) - @parent = data[:parent] if data @xml_order = xml_order @additional_xml_elements = additional_xml_elements diff --git a/lib/diaspora_federation/entities/relayable_retraction.rb b/lib/diaspora_federation/entities/relayable_retraction.rb index f270b4f..edef6ef 100644 --- a/lib/diaspora_federation/entities/relayable_retraction.rb +++ b/lib/diaspora_federation/entities/relayable_retraction.rb @@ -53,18 +53,10 @@ module DiasporaFederation # @return [String] target author signature property :target_author_signature, default: nil - # target entity - # @return [RelatedEntity] target entity - attr_reader :target - - # Initializes a new relayable retraction entity - # - # @param [Hash] data entity data - # @see DiasporaFederation::Entity#initialize - def initialize(data) - @target = data[:target] if data - super(data) - end + # @!attribute [r] target + # target entity + # @return [RelatedEntity] target entity + entity :target, Entities::RelatedEntity # use only {Retraction} for receive # @return [Retraction] instance as normal retraction diff --git a/lib/diaspora_federation/entities/retraction.rb b/lib/diaspora_federation/entities/retraction.rb index dcd1f81..437a3d1 100644 --- a/lib/diaspora_federation/entities/retraction.rb +++ b/lib/diaspora_federation/entities/retraction.rb @@ -20,18 +20,10 @@ module DiasporaFederation # @return [String] target type property :target_type, xml_name: :type - # target entity - # @return [RelatedEntity] target entity - attr_reader :target - - # Initializes a new retraction entity - # - # @param [Hash] data entity data - # @see DiasporaFederation::Entity#initialize - def initialize(data) - @target = data[:target] if data - super(data) - end + # @!attribute [r] target + # target entity + # @return [RelatedEntity] target entity + entity :target, Entities::RelatedEntity def sender_valid?(sender) case target_type diff --git a/lib/diaspora_federation/entities/signed_retraction.rb b/lib/diaspora_federation/entities/signed_retraction.rb index e304595..584875f 100644 --- a/lib/diaspora_federation/entities/signed_retraction.rb +++ b/lib/diaspora_federation/entities/signed_retraction.rb @@ -30,18 +30,10 @@ module DiasporaFederation # @return [String] author signature property :target_author_signature, default: nil - # target entity - # @return [RelatedEntity] target entity - attr_reader :target - - # Initializes a new signed retraction entity - # - # @param [Hash] data entity data - # @see DiasporaFederation::Entity#initialize - def initialize(data) - @target = data[:target] if data - super(data) - end + # @!attribute [r] target + # target entity + # @return [RelatedEntity] target entity + entity :target, Entities::RelatedEntity # use only {Retraction} for receive # @return [Retraction] instance as normal retraction diff --git a/lib/diaspora_federation/entity.rb b/lib/diaspora_federation/entity.rb index e433837..c1f8ffd 100644 --- a/lib/diaspora_federation/entity.rb +++ b/lib/diaspora_federation/entity.rb @@ -225,7 +225,8 @@ module DiasporaFederation else # call #to_xml for each item and append to root [*value].compact.each do |item| - root_element << item.to_xml + child = item.to_xml + root_element << child if child end end end diff --git a/spec/entities.rb b/spec/entities.rb index 4b487bd..5d0aef0 100644 --- a/spec/entities.rb +++ b/spec/entities.rb @@ -26,6 +26,11 @@ module DiasporaFederation property :qwer, xml_name: :asdf end + class TestEntityWithRelatedEntity < DiasporaFederation::Entity + property :test + entity :parent, RelatedEntity + end + class Entity < DiasporaFederation::Entity property :test end diff --git a/spec/lib/diaspora_federation/entities/comment_spec.rb b/spec/lib/diaspora_federation/entities/comment_spec.rb index e02856d..8581424 100644 --- a/spec/lib/diaspora_federation/entities/comment_spec.rb +++ b/spec/lib/diaspora_federation/entities/comment_spec.rb @@ -21,7 +21,7 @@ XML } let(:string) { "Comment:#{data[:guid]}:#{parent.guid}" } - it_behaves_like "an Entity subclass", [:parent] + it_behaves_like "an Entity subclass" it_behaves_like "an XML Entity", [:created_at] diff --git a/spec/lib/diaspora_federation/entities/like_spec.rb b/spec/lib/diaspora_federation/entities/like_spec.rb index 9bcfcbc..75f7470 100644 --- a/spec/lib/diaspora_federation/entities/like_spec.rb +++ b/spec/lib/diaspora_federation/entities/like_spec.rb @@ -27,7 +27,7 @@ XML } let(:string) { "Like:#{data[:guid]}:Post:#{parent.guid}" } - it_behaves_like "an Entity subclass", [:parent] + it_behaves_like "an Entity subclass" it_behaves_like "an XML Entity" diff --git a/spec/lib/diaspora_federation/entities/message_spec.rb b/spec/lib/diaspora_federation/entities/message_spec.rb index 6f0e851..e0497b8 100644 --- a/spec/lib/diaspora_federation/entities/message_spec.rb +++ b/spec/lib/diaspora_federation/entities/message_spec.rb @@ -23,7 +23,7 @@ XML } let(:string) { "Message:#{data[:guid]}:#{parent.guid}" } - it_behaves_like "an Entity subclass", [:parent] + it_behaves_like "an Entity subclass" it_behaves_like "an XML Entity" diff --git a/spec/lib/diaspora_federation/entities/participation_spec.rb b/spec/lib/diaspora_federation/entities/participation_spec.rb index cefed84..2b348d8 100644 --- a/spec/lib/diaspora_federation/entities/participation_spec.rb +++ b/spec/lib/diaspora_federation/entities/participation_spec.rb @@ -26,7 +26,7 @@ XML } let(:string) { "Participation:#{data[:guid]}:Post:#{parent.guid}" } - it_behaves_like "an Entity subclass", [:parent] + it_behaves_like "an Entity subclass" it_behaves_like "an XML Entity" diff --git a/spec/lib/diaspora_federation/entities/poll_participation_spec.rb b/spec/lib/diaspora_federation/entities/poll_participation_spec.rb index 4687ea1..a1ecb53 100644 --- a/spec/lib/diaspora_federation/entities/poll_participation_spec.rb +++ b/spec/lib/diaspora_federation/entities/poll_participation_spec.rb @@ -25,7 +25,7 @@ XML } let(:string) { "PollParticipation:#{data[:guid]}:#{parent.guid}" } - it_behaves_like "an Entity subclass", [:parent] + it_behaves_like "an Entity subclass" it_behaves_like "an XML Entity" diff --git a/spec/lib/diaspora_federation/entities/related_entity_spec.rb b/spec/lib/diaspora_federation/entities/related_entity_spec.rb index 76d53c6..33301a6 100644 --- a/spec/lib/diaspora_federation/entities/related_entity_spec.rb +++ b/spec/lib/diaspora_federation/entities/related_entity_spec.rb @@ -4,5 +4,11 @@ module DiasporaFederation let(:string) { "RelatedEntity" } it_behaves_like "an Entity subclass" + + describe "#to_xml" do + it "returns nil" do + expect(described_class.new(data).to_xml).to be_nil + end + end end end diff --git a/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb b/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb index e8b967d..9fc9d43 100644 --- a/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb +++ b/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb @@ -34,9 +34,9 @@ XML } let(:string) { "RelayableRetraction:#{data[:target_type]}:#{data[:target_guid]}" } - it_behaves_like "an Entity subclass", [:target] + it_behaves_like "an Entity subclass" - it_behaves_like "an XML Entity", %i(parent_author_signature target_author_signature) + it_behaves_like "an XML Entity", %i(parent_author_signature target_author_signature target) it_behaves_like "a retraction" diff --git a/spec/lib/diaspora_federation/entities/retraction_spec.rb b/spec/lib/diaspora_federation/entities/retraction_spec.rb index d147e8a..5ced79b 100644 --- a/spec/lib/diaspora_federation/entities/retraction_spec.rb +++ b/spec/lib/diaspora_federation/entities/retraction_spec.rb @@ -22,7 +22,7 @@ XML } let(:string) { "Retraction:#{data[:target_type]}:#{data[:target_guid]}" } - it_behaves_like "an Entity subclass", [:target] + it_behaves_like "an Entity subclass" it_behaves_like "an XML Entity" diff --git a/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb b/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb index 41fe52c..960c720 100644 --- a/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb +++ b/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb @@ -24,7 +24,7 @@ XML } let(:string) { "SignedRetraction:#{data[:target_type]}:#{data[:target_guid]}" } - it_behaves_like "an Entity subclass", [:target] + it_behaves_like "an Entity subclass" it_behaves_like "an XML Entity", [:target_author_signature] diff --git a/spec/lib/diaspora_federation/entity_spec.rb b/spec/lib/diaspora_federation/entity_spec.rb index a9a9a4b..899f99d 100644 --- a/spec/lib/diaspora_federation/entity_spec.rb +++ b/spec/lib/diaspora_federation/entity_spec.rb @@ -282,6 +282,13 @@ XML expect(xml.xpath("other_entity")).to have_exactly(2).items end + it "is not added to xml if #to_xml returns nil" do + entity = Entities::TestEntityWithRelatedEntity.new(test: "test", parent: FactoryGirl.build(:related_entity)) + xml = entity.to_xml + expect(xml.children).to have_exactly(1).items + xml.children.first.name = "test" + end + it "instantiates nested entities if provided as hash" do entity = Entities::TestNestedEntity.new(nested_hash) diff --git a/spec/support/shared_entity_specs.rb b/spec/support/shared_entity_specs.rb index 2e474ea..83e5598 100644 --- a/spec/support/shared_entity_specs.rb +++ b/spec/support/shared_entity_specs.rb @@ -1,10 +1,10 @@ -shared_examples "an Entity subclass" do |ignored_props=[]| +shared_examples "an Entity subclass" do it "should be an Entity" do expect(described_class).to be < DiasporaFederation::Entity end it "has its properties set" do - expect(described_class.class_props.keys).to include(*(data.keys - ignored_props)) + expect(described_class.class_props.keys).to include(*data.keys) end context "behaviour" do @@ -26,7 +26,7 @@ shared_examples "an Entity subclass" do |ignored_props=[]| describe "#to_h" do it "should return a hash with nested data" do - expected_data = Hash[data.reject {|key, _| ignored_props.include?(key) }.map {|key, value| + expected_data = Hash[data.map {|key, value| if [String, TrueClass, FalseClass, Fixnum, Time, NilClass].include?(value.class) [key, value] elsif value.instance_of?(Array) @@ -76,7 +76,7 @@ shared_examples "an XML Entity" do |ignored_props=[]| if value.nil? expect(parsed_value).to be_nil elsif type == String - expect(parsed_value).to eq(value.to_s) + expect(parsed_value.to_s).to eq(value.to_s) elsif type.instance_of?(Array) value.each_with_index {|entity, index| check_entity(entity, parsed_value[index]) } elsif type.ancestors.include?(DiasporaFederation::Entity)