Class: ClearElection::Election

Inherits:
Object
  • Object
show all
Defined in:
lib/clear-election-sdk/election.rb

Defined Under Namespace

Classes: Agent, Candidate, Contest

Constant Summary collapse

SCHEMA_VERSION =
0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, signin:, booth:, contests:, pollsOpen:, pollsClose:, uri: nil, voters: nil, ballots: nil) ⇒ Election

Returns a new instance of Election.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/clear-election-sdk/election.rb', line 13

def initialize(name:, signin:, booth:, contests:, pollsOpen:, pollsClose:, uri:nil, voters:nil, ballots:nil)
  @name = name
  @signin = 
  @booth = booth
  @contests = contests
  @pollsOpen = pollsOpen
  @pollsClose = pollsClose
  @uri = uri
  @voters = voters || []
  @ballots = ballots || []
end

Instance Attribute Details

#ballotsObject (readonly)

Returns the value of attribute ballots.



7
8
9
# File 'lib/clear-election-sdk/election.rb', line 7

def ballots
  @ballots
end

#boothObject (readonly)

Returns the value of attribute booth.



7
8
9
# File 'lib/clear-election-sdk/election.rb', line 7

def booth
  @booth
end

#contestsObject (readonly)

Returns the value of attribute contests.



7
8
9
# File 'lib/clear-election-sdk/election.rb', line 7

def contests
  @contests
end

#pollsCloseObject (readonly)

Returns the value of attribute pollsClose.



7
8
9
# File 'lib/clear-election-sdk/election.rb', line 7

def pollsClose
  @pollsClose
end

#pollsOpenObject (readonly)

Returns the value of attribute pollsOpen.



7
8
9
# File 'lib/clear-election-sdk/election.rb', line 7

def pollsOpen
  @pollsOpen
end

#signinObject (readonly)

Returns the value of attribute signin.



7
8
9
# File 'lib/clear-election-sdk/election.rb', line 7

def 
  @signin
end

#uriObject (readonly)

Returns the value of attribute uri.



7
8
9
# File 'lib/clear-election-sdk/election.rb', line 7

def uri
  @uri
end

#votersObject (readonly)

Returns the value of attribute voters.



7
8
9
# File 'lib/clear-election-sdk/election.rb', line 7

def voters
  @voters
end

Class Method Details

.from_json(data, uri: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/clear-election-sdk/election.rb', line 25

def self.from_json(data, uri: nil)
  ClearElection::Schema.validate!(schema, data, insert_defaults: true)
  self.new(
    name: data["name"],
    signin: Agent.from_json(data["agents"]["signin"]),
    booth: Agent.from_json(data["agents"]["booth"]),
    contests: data["contests"].map {|data| Contest.from_json(data) },
    pollsOpen: DateTime.rfc3339(data["schedule"]["pollsOpen"]),
    pollsClose: DateTime.rfc3339(data["schedule"]["pollsClose"]),
    uri: uri,
    voters: data["returns"]["voters"],
    ballots: data["returns"]["ballots"].map { |data| Ballot.from_json(data) }
  )
end

.schemaObject



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

def self.schema
  ClearElection::Schema.election(version: SCHEMA_VERSION)
end

Instance Method Details

#as_jsonObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/clear-election-sdk/election.rb', line 40

def as_json()
  data = {
    "clearelection" => "v0.0",
    "name" => @name,
    "agents" => {
      "signin" => @signin.as_json,
      "booth" => @booth.as_json
    },
    "schedule" => {
      "pollsOpen" => @pollsOpen.rfc3339,
      "pollsClose" => @pollsClose.rfc3339,
    },
    "contests" => @contests.map(&:as_json)
  }
  data["returns"] = {
    "voters" => @voters,
    "ballots" => @ballots.map(&:as_json)
  }
  ClearElection::Schema.validate!(Election.schema, data)
  data
end

#get_contest(contestId) ⇒ Object



80
81
82
# File 'lib/clear-election-sdk/election.rb', line 80

def get_contest(contestId)
  contests.find{|contest| contest.contestId == contestId}
end

#polls_are_now_closed?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/clear-election-sdk/election.rb', line 76

def polls_are_now_closed?
  DateTime.now > pollsClose
end

#polls_are_now_open?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/clear-election-sdk/election.rb', line 72

def polls_are_now_open?
  DateTime.now.between?(pollsOpen, pollsClose)
end

#polls_have_not_opened?Boolean

utilities

Returns:

  • (Boolean)


68
69
70
# File 'lib/clear-election-sdk/election.rb', line 68

def polls_have_not_opened?
  DateTime.now < pollsOpen
end

#set_returns(voters:, ballots:) ⇒ Object



62
63
64
65
# File 'lib/clear-election-sdk/election.rb', line 62

def set_returns(voters:, ballots:)
  @voters = voters
  @ballots = ballots
end