Add the Poll entity
This commit is contained in:
parent
34a720cd14
commit
e8fa25c6a6
17 changed files with 219 additions and 0 deletions
|
|
@ -12,6 +12,8 @@ require "diaspora_federation/entities/profile"
|
|||
require "diaspora_federation/entities/person"
|
||||
require "diaspora_federation/entities/location"
|
||||
require "diaspora_federation/entities/photo"
|
||||
require "diaspora_federation/entities/poll_answer"
|
||||
require "diaspora_federation/entities/poll"
|
||||
require "diaspora_federation/entities/status_message"
|
||||
require "diaspora_federation/entities/request"
|
||||
require "diaspora_federation/entities/participation"
|
||||
|
|
@ -24,3 +26,4 @@ require "diaspora_federation/entities/relayable_retraction"
|
|||
require "diaspora_federation/entities/reshare"
|
||||
require "diaspora_federation/entities/retraction"
|
||||
require "diaspora_federation/entities/signed_retraction"
|
||||
require "diaspora_federation/entities/poll_participation"
|
||||
|
|
|
|||
9
lib/diaspora_federation/entities/poll.rb
Normal file
9
lib/diaspora_federation/entities/poll.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
module DiasporaFederation
|
||||
module Entities
|
||||
class Poll < Entity
|
||||
property :guid
|
||||
property :question
|
||||
entity :poll_answers, [Entities::PollAnswer]
|
||||
end
|
||||
end
|
||||
end
|
||||
8
lib/diaspora_federation/entities/poll_answer.rb
Normal file
8
lib/diaspora_federation/entities/poll_answer.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
module DiasporaFederation
|
||||
module Entities
|
||||
class PollAnswer < Entity
|
||||
property :guid
|
||||
property :answer
|
||||
end
|
||||
end
|
||||
end
|
||||
11
lib/diaspora_federation/entities/poll_participation.rb
Normal file
11
lib/diaspora_federation/entities/poll_participation.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
module DiasporaFederation
|
||||
module Entities
|
||||
class PollParticipation < Entity
|
||||
property :guid
|
||||
property :parent_guid
|
||||
property :parent_author_signature
|
||||
property :diaspora_id, xml_name: :diaspora_handle
|
||||
property :poll_answer_guid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -4,6 +4,7 @@ module DiasporaFederation
|
|||
property :raw_message
|
||||
entity :photos, [Entities::Photo], default: []
|
||||
entity :location, Entities::Location, default: nil
|
||||
entity :poll, Entities::Poll, default: nil
|
||||
property :guid
|
||||
property :diaspora_id, xml_name: :diaspora_handle
|
||||
property :public, default: false
|
||||
|
|
|
|||
|
|
@ -52,3 +52,6 @@ require "diaspora_federation/validators/relayable_retraction_validator"
|
|||
require "diaspora_federation/validators/reshare_validator"
|
||||
require "diaspora_federation/validators/retraction_validator"
|
||||
require "diaspora_federation/validators/signed_retraction_validator"
|
||||
require "diaspora_federation/validators/poll_answer_validator"
|
||||
require "diaspora_federation/validators/poll_validator"
|
||||
require "diaspora_federation/validators/poll_participation_validator"
|
||||
|
|
|
|||
10
lib/diaspora_federation/validators/poll_answer_validator.rb
Normal file
10
lib/diaspora_federation/validators/poll_answer_validator.rb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
module DiasporaFederation
|
||||
module Validators
|
||||
class PollAnswerValidator < Validation::Validator
|
||||
include Validation
|
||||
|
||||
rule :guid, :guid
|
||||
rule :answer, [:not_empty, length: {maximum: 255}]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
module DiasporaFederation
|
||||
module Validators
|
||||
class PollParticipationValidator < Validation::Validator
|
||||
include Validation
|
||||
|
||||
rule :guid, :guid
|
||||
|
||||
rule :parent_guid, :guid
|
||||
|
||||
rule :parent_author_signature, :not_empty
|
||||
|
||||
rule :diaspora_id, %i(not_empty diaspora_id)
|
||||
|
||||
rule :poll_answer_guid, :guid
|
||||
end
|
||||
end
|
||||
end
|
||||
10
lib/diaspora_federation/validators/poll_validator.rb
Normal file
10
lib/diaspora_federation/validators/poll_validator.rb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
module DiasporaFederation
|
||||
module Validators
|
||||
class PollValidator < Validation::Validator
|
||||
include Validation
|
||||
|
||||
rule :guid, :guid
|
||||
rule :question, [:not_empty, length: {maximum: 255}]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -5,6 +5,7 @@ def r_str
|
|||
end
|
||||
|
||||
FactoryGirl.define do
|
||||
initialize_with { new(attributes) }
|
||||
sequence(:guid) { UUID.generate :compact }
|
||||
sequence(:diaspora_id) {|n| "person-#{n}-#{r_str}@localhost:3000" }
|
||||
sequence(:public_key) { OpenSSL::PKey::RSA.generate(1024).public_key.export }
|
||||
|
|
@ -188,4 +189,23 @@ FactoryGirl.define do
|
|||
sender_id { generate(:diaspora_id) }
|
||||
target_author_signature { generate(:signature) }
|
||||
end
|
||||
|
||||
factory :poll_answer_entity, class: DiasporaFederation::Entities::PollAnswer do
|
||||
guid
|
||||
answer { "Obama is a bicycle" }
|
||||
end
|
||||
|
||||
factory :poll_entity, class: DiasporaFederation::Entities::Poll do
|
||||
guid
|
||||
question { "Select an answer" }
|
||||
poll_answers { 3.times.map { FactoryGirl.build(:poll_answer_entity) } }
|
||||
end
|
||||
|
||||
factory :poll_participation_entity, class: DiasporaFederation::Entities::PollParticipation do
|
||||
guid
|
||||
parent_guid { generate(:guid) }
|
||||
diaspora_id
|
||||
parent_author_signature { generate(:signature) }
|
||||
poll_answer_guid { generate(:guid) }
|
||||
end
|
||||
end
|
||||
|
|
|
|||
18
spec/lib/diaspora_federation/entities/poll_answer_spec.rb
Normal file
18
spec/lib/diaspora_federation/entities/poll_answer_spec.rb
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
module DiasporaFederation
|
||||
describe Entities::PollAnswer do
|
||||
let(:data) { FactoryGirl.attributes_for(:poll_answer_entity) }
|
||||
|
||||
let(:xml) {
|
||||
<<-XML
|
||||
<poll_answer>
|
||||
<guid>#{data[:guid]}</guid>
|
||||
<answer>#{data[:answer]}</answer>
|
||||
</poll_answer>
|
||||
XML
|
||||
}
|
||||
|
||||
it_behaves_like "an Entity subclass"
|
||||
|
||||
it_behaves_like "an XML Entity"
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
module DiasporaFederation
|
||||
describe Entities::PollParticipation do
|
||||
let(:data) { FactoryGirl.attributes_for(:poll_participation_entity) }
|
||||
|
||||
let(:xml) {
|
||||
<<-XML
|
||||
<poll_participation>
|
||||
<guid>#{data[:guid]}</guid>
|
||||
<parent_guid>#{data[:parent_guid]}</parent_guid>
|
||||
<parent_author_signature>#{data[:parent_author_signature]}</parent_author_signature>
|
||||
<diaspora_handle>#{data[:diaspora_id]}</diaspora_handle>
|
||||
<poll_answer_guid>#{data[:poll_answer_guid]}</poll_answer_guid>
|
||||
</poll_participation>
|
||||
XML
|
||||
}
|
||||
|
||||
it_behaves_like "an Entity subclass"
|
||||
|
||||
it_behaves_like "an XML Entity"
|
||||
end
|
||||
end
|
||||
19
spec/lib/diaspora_federation/entities/poll_spec.rb
Normal file
19
spec/lib/diaspora_federation/entities/poll_spec.rb
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
module DiasporaFederation
|
||||
describe Entities::Poll do
|
||||
let(:data) { FactoryGirl.attributes_for(:poll_entity) }
|
||||
|
||||
let(:xml) {
|
||||
<<-XML
|
||||
<poll>
|
||||
<guid>#{data[:guid]}</guid>
|
||||
<question>#{data[:question]}</question>
|
||||
#{data[:poll_answers].map {|a| a.to_xml.to_s.indent(2) }.join("\n")}
|
||||
</poll>
|
||||
XML
|
||||
}
|
||||
|
||||
it_behaves_like "an Entity subclass"
|
||||
|
||||
it_behaves_like "an XML Entity"
|
||||
end
|
||||
end
|
||||
|
|
@ -8,6 +8,7 @@ module DiasporaFederation
|
|||
raw_message: "this is such an interesting text",
|
||||
photos: [photo1, photo2],
|
||||
location: location,
|
||||
poll: nil,
|
||||
guid: FactoryGirl.generate(:guid),
|
||||
diaspora_id: FactoryGirl.generate(:diaspora_id),
|
||||
public: true,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
module DiasporaFederation
|
||||
describe Validators::PollAnswerValidator do
|
||||
let(:entity) { :poll_answer_entity }
|
||||
|
||||
it_behaves_like "a common validator"
|
||||
|
||||
describe "#guid" do
|
||||
it_behaves_like "a guid validator" do
|
||||
let(:property) { :guid }
|
||||
end
|
||||
end
|
||||
|
||||
describe "#answer" do
|
||||
it_behaves_like "a property with a value validation/restriction" do
|
||||
let(:property) { :answer }
|
||||
let(:wrong_values) { [nil, "", "a" * 256] }
|
||||
let(:correct_values) { ["a" * 255] }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
module DiasporaFederation
|
||||
describe Validators::PollParticipationValidator do
|
||||
let(:entity) { :poll_participation_entity }
|
||||
|
||||
it_behaves_like "a common validator"
|
||||
|
||||
it_behaves_like "a diaspora id validator" do
|
||||
let(:property) { :diaspora_id }
|
||||
let(:mandatory) { true }
|
||||
end
|
||||
|
||||
%i(guid parent_guid poll_answer_guid).each do |prop|
|
||||
describe "##{prop}" do
|
||||
it_behaves_like "a guid validator" do
|
||||
let(:property) { prop }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#parent_author_signature" do
|
||||
it_behaves_like "a property that mustn't be empty" do
|
||||
let(:property) { :parent_author_signature }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
module DiasporaFederation
|
||||
describe Validators::PollValidator do
|
||||
let(:entity) { :poll_entity }
|
||||
|
||||
it_behaves_like "a common validator"
|
||||
|
||||
describe "#guid" do
|
||||
it_behaves_like "a guid validator" do
|
||||
let(:property) { :guid }
|
||||
end
|
||||
end
|
||||
|
||||
describe "#question" do
|
||||
it_behaves_like "a property with a value validation/restriction" do
|
||||
let(:property) { :question }
|
||||
let(:wrong_values) { [nil, "", "a" * 256] }
|
||||
let(:correct_values) { ["a" * 255] }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue