Module: ClearElection::Factory

Defined in:
lib/clear-election-sdk/factory.rb

Class Method Summary collapse

Class Method Details

.agent_uri(what = "agent", host: "agents.example.com") ⇒ Object



13
14
15
# File 'lib/clear-election-sdk/factory.rb', line 13

def self.agent_uri(what="agent", host:"agents.example.com")
  "http://#{host}/#{seq(what)}/"
end

.ballot(election = nil, ballotId: nil, uniquifier: nil, demographic: nil, invalid: nil, complete: true) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/clear-election-sdk/factory.rb', line 57

def self.ballot(election=nil, ballotId: nil, uniquifier: nil, demographic: nil, invalid: nil, complete: true)
  election ||= self.election
  contests = election.contests.dup
  contests = contests.drop(1) if not complete
  contests << Factory.contest if invalid == :contestId
  Ballot.new(
    ballotId: ballotId || seq(:ballotId),
    uniquifier: uniquifier || seq(:uniquifier),
    contests: contests.map { |contest|
      options = contest.candidates.map(&:candidateId)
      options << "ABSTAIN"
      options << "WRITEIN: TestWritein" if contest.writeIn
      options.shuffle!
      options.push "Test-Invalid-CandidateId" if invalid == :candidateId
      options.push "WRITEIN: Test-Unpermitted-Writein" if invalid == :writeIn and not contest.writeIn
      options.push options.last if invalid == :duplicateChoice
      nchoices = contest.multiplicity
      nchoices -= 1 if invalid == :multiplicityTooFew
      nchoices += 1 if invalid == :multiplicityTooMany
      Ballot::Contest.new(
        contestId: contest.contestId,
        choices: nchoices.times.map {|rank|
          rank += 1 if invalid == :ranking
          Ballot::Choice.new(candidateId: options.pop, rank: rank)
        }
      )
    },
    demographic: demographic
  )
end

.contest(ranked: nil, multiplicity: nil, writeIn: nil, ncandidates: 3) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/clear-election-sdk/factory.rb', line 46

def self.contest(ranked: nil, multiplicity: nil, writeIn: nil, ncandidates: 3)
  Election::Contest.new(
    contestId: seq(:contestId),
    name: seq("Contest Name"),
    ranked: ranked,
    multiplicity: multiplicity,
    writeIn: writeIn,
    candidates: ncandidates.times.map{ Election::Candidate.new(candidateId: seq(:candidateId), name: seq("Candidate Name")) }
  )
end

.election(signin: nil, booth: nil, pollsOpen: nil, pollsClose: nil, writeIn: true, returns: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/clear-election-sdk/factory.rb', line 17

def self.election(
  signin: nil,
  booth: nil,
  pollsOpen: nil,
  pollsClose: nil,
  writeIn: true,
  returns: false
)
  one_month = 60*60*24*30
  Election.new(
    name: seq("Election Name"),
    signin: Election::Agent.new(uri:  || self.agent_uri("signin")),
    booth: Election::Agent.new(uri: booth || self.agent_uri("booth")),
    pollsOpen: (pollsOpen || Time.now - one_month).to_datetime(),
    pollsClose: (pollsClose || Time.now + one_month).to_datetime(),
    contests: [
      Factory.contest(ranked: true, multiplicity: 3, writeIn: writeIn, ncandidates: 3),
      Factory.contest(ncandidates: 2)
    ]
  ).tap { |election|
    if returns
      election.set_returns(
        voters: 10.times.map{ { "name" => seq("Voter") } }.sort_by { |v| v[:name] },
        ballots: 10.times.map{ Factory.ballot(election) }.sort
      )
    end
  }
end

.election_uriObject



9
10
11
# File 'lib/clear-election-sdk/factory.rb', line 9

def self.election_uri
  "http://test.example.com/elections/#{seq(:id)}"
end

.seq(key) ⇒ Object



4
5
6
7
# File 'lib/clear-election-sdk/factory.rb', line 4

def self.seq(key)
  val = (@seq ||= Hash.new(-1))[key] += 1
  "#{key}-#{val}"
end