clear-election-sdk 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +7 -0
- data/clear-election-sdk.gemspec +31 -0
- data/lib/clear-election-sdk.rb +17 -0
- data/lib/clear-election-sdk/ballot.rb +156 -0
- data/lib/clear-election-sdk/election.rb +150 -0
- data/lib/clear-election-sdk/factory.rb +80 -0
- data/lib/clear-election-sdk/rspec.rb +129 -0
- data/lib/clear-election-sdk/schema.rb +43 -0
- data/lib/clear-election-sdk/version.rb +3 -0
- data/schemas/api/booth-agent-0.0.schema.json +91 -0
- data/schemas/ballot-0.0.schema.json +33 -0
- data/schemas/election-0.0.schema.json +60 -0
- data/spec/ballot_spec.rb +94 -0
- data/spec/clear_election_spec.rb +11 -0
- data/spec/election_spec.rb +49 -0
- data/spec/rspec_spec.rb +152 -0
- data/spec/schema_spec.rb +9 -0
- data/spec/spec_helper.rb +42 -0
- metadata +202 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 35f1b77def83bb18cbc429b636a9bdcd1683ea30
|
4
|
+
data.tar.gz: 9f6136cec7c9af255eb48b92a36af5d16050957c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0200ddc2529f35c99c1b47e165fa3653ee70904208f3f2e3da9416c8fdf0e4473d268e4d798411582c36e965de0dba497c220851a50dee3b7bf7182a43d53d43
|
7
|
+
data.tar.gz: c49ca30e6358a306db28619e82d4cf4b50061ae1785fb7b8b2434fafd0ea7d697a3077773560ea40bbef8f2bc9ab439defc9b722ff2673efbaa0b0c7701056dc
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.5
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in clear_election.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
gem 'coveralls', require: false
|
7
|
+
|
8
|
+
# for now use my forks in which warnings have been eliminated
|
9
|
+
gem "timecop", git: 'https://github.com/ronen/timecop.git' # PR is in https://github.com/travisjeffery/timecop/pull/131
|
10
|
+
gem "addressable", git: 'https://github.com/ronen/addressable.git', branch: "patch-1"
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 ronen barzel
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# ClearElection SDK (ruby)
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/clear-election-sdk.svg)](http://badge.fury.io/rb/clear-election-sdk)
|
3
|
+
[![Build Status](https://secure.travis-ci.org/ClearElection/clear-election-sdk-ruby.svg)](http://travis-ci.org/ClearElection/clear-election-sdk-ruby)
|
4
|
+
[![Coverage Status](https://img.shields.io/coveralls/ClearElection/clear-election-sdk-ruby.svg)](https://coveralls.io/r/ClearElection/clear-election-sdk-ruby)
|
5
|
+
|
6
|
+
Ruby SDK to work with ClearElection data.
|
7
|
+
|
8
|
+
|
9
|
+
## Overview
|
10
|
+
|
11
|
+
Defines several classes:
|
12
|
+
|
13
|
+
* `ClearElection::Election`
|
14
|
+
* `ClearElection::Ballot`
|
15
|
+
|
16
|
+
Defines a top-level function to retrieve an Election from a URI:
|
17
|
+
|
18
|
+
election = ClearElection.read(uri)
|
19
|
+
|
20
|
+
Also includes utilities for testing application code that works with elections:
|
21
|
+
|
22
|
+
require 'clear-election-sdk/rspec'
|
23
|
+
require 'clear-election-sdk/factory'
|
24
|
+
|
25
|
+
For now, for more details UTSL.
|
26
|
+
|
27
|
+
|
28
|
+
## Schemas
|
29
|
+
|
30
|
+
For now, this repository contains the masters for the schemas. Currently:
|
31
|
+
|
32
|
+
* `schemas/election-0.0.schema.json`
|
33
|
+
* `schemas/ballot-0.0.schema.json`
|
34
|
+
* `schemas/api/booth-agent-0.0.schema.json`
|
35
|
+
|
36
|
+
|
37
|
+
## Installation
|
38
|
+
|
39
|
+
Add this line to your application's Gemfile:
|
40
|
+
|
41
|
+
gem 'clear-election-sdk'
|
42
|
+
|
43
|
+
And then execute:
|
44
|
+
|
45
|
+
$ bundle
|
46
|
+
|
47
|
+
Or install it yourself as:
|
48
|
+
|
49
|
+
$ gem install clear_election
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it ( https://github.com/[my-github-username]/clear_election/fork )
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'clear-election-sdk/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "clear-election-sdk"
|
8
|
+
spec.version = ClearElection::VERSION
|
9
|
+
spec.authors = ["ronen barzel"]
|
10
|
+
spec.email = ["ronen@barzel.org"]
|
11
|
+
spec.summary = %q{Ruby SDK for working with ClearElection data}
|
12
|
+
spec.description = %q{Ruby SDK for working with ClearElection data. Also includes factory and rspec helpers for testing apps}
|
13
|
+
spec.homepage = "https://github.com/ClearElection/clear-election-sdk-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "faraday"
|
22
|
+
spec.add_dependency "json-schema"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "simplecov"
|
28
|
+
spec.add_development_dependency "simplecov-gem-profile"
|
29
|
+
spec.add_development_dependency "webmock"
|
30
|
+
spec.add_development_dependency "timecop"
|
31
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "json-schema"
|
3
|
+
|
4
|
+
require "clear-election-sdk/ballot"
|
5
|
+
require "clear-election-sdk/election"
|
6
|
+
require "clear-election-sdk/schema"
|
7
|
+
require "clear-election-sdk/version"
|
8
|
+
|
9
|
+
module ClearElection
|
10
|
+
|
11
|
+
def self.read(uri)
|
12
|
+
response = Faraday.get(uri)
|
13
|
+
return nil unless response.success?
|
14
|
+
Election.from_json(JSON.parse(response.body), uri: uri)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
module ClearElection
|
2
|
+
|
3
|
+
class Ballot
|
4
|
+
|
5
|
+
SCHEMA_VERSION = 0.0
|
6
|
+
|
7
|
+
attr_reader :ballotId, :uniquifier, :contests, :errors, :demographic
|
8
|
+
|
9
|
+
def self.schema
|
10
|
+
ClearElection::Schema.ballot(version: SCHEMA_VERSION)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.from_json(data)
|
14
|
+
errors = JSON::Validator.fully_validate(schema, data, insert_defaults: true, errors_as_objects: true)
|
15
|
+
return self.new(ballotId: nil, uniquifier: nil, contests: [], errors: errors) unless errors.empty?
|
16
|
+
|
17
|
+
self.new(
|
18
|
+
ballotId: data["ballotId"],
|
19
|
+
uniquifier: data["uniquifier"],
|
20
|
+
contests: data["contests"].map { |data| Contest.from_json(data) },
|
21
|
+
demographic: data["demographic"]
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(ballotId:, uniquifier:, contests:, errors: [], demographic: nil)
|
26
|
+
@ballotId = ballotId
|
27
|
+
@uniquifier = uniquifier
|
28
|
+
@contests = contests
|
29
|
+
@demographic = demographic
|
30
|
+
@errors = errors
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate(election, complete: true)
|
34
|
+
@contests.each do |contest|
|
35
|
+
@errors += contest.validate(election)
|
36
|
+
end
|
37
|
+
if complete and not (missing = election.contests.map(&:contestId) - @contests.map(&:contestId)).empty?
|
38
|
+
@errors += missing.map { |contestId| { contestId: contestId, message: "missing contest" } }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def valid?
|
43
|
+
@errors.empty?
|
44
|
+
end
|
45
|
+
|
46
|
+
def as_json()
|
47
|
+
data = {
|
48
|
+
"version" => "0.0",
|
49
|
+
"ballotId" => @ballotId,
|
50
|
+
"uniquifier" => @uniquifier,
|
51
|
+
"contests" => @contests.map(&:as_json),
|
52
|
+
}
|
53
|
+
data["demographic"] = @demographic if @demographic
|
54
|
+
JSON::Validator.validate!(Ballot.schema, data)
|
55
|
+
data
|
56
|
+
end
|
57
|
+
|
58
|
+
def <=>(other)
|
59
|
+
[self.ballotId, self.contests] <=> [other.ballotId, other.contests]
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
class Contest
|
64
|
+
|
65
|
+
attr_reader :contestId, :choices
|
66
|
+
|
67
|
+
def initialize(contestId:, choices:)
|
68
|
+
@contestId = contestId
|
69
|
+
@choices = choices
|
70
|
+
end
|
71
|
+
|
72
|
+
def validate(election)
|
73
|
+
errors = []
|
74
|
+
err = { contestId: contestId }
|
75
|
+
contest = election.get_contest(contestId)
|
76
|
+
if contest.nil?
|
77
|
+
errors.push err.merge(message: "Invalid contest id")
|
78
|
+
else
|
79
|
+
if choices.length != contest.multiplicity
|
80
|
+
errors.push err.merge(message: "Invalid multiplicity", multiplicity: choices.length)
|
81
|
+
end
|
82
|
+
if contest.ranked and choices.map(&:rank).sort != (0...contest.multiplicity).to_a
|
83
|
+
errors.push err.merge(message: "Invalid ranking")
|
84
|
+
end
|
85
|
+
if choices.map(&:candidateId).uniq.length != choices.length
|
86
|
+
errors.push err.merge(message: "Duplicate choices")
|
87
|
+
end
|
88
|
+
choices.each do |choice|
|
89
|
+
case
|
90
|
+
when choice.writeIn?
|
91
|
+
if !contest.writeIn
|
92
|
+
errors.push err.merge(message: "Write-in not allowed")
|
93
|
+
end
|
94
|
+
when choice.candidate?
|
95
|
+
if !contest.candidates.map(&:candidateId).include?(choice.candidateId)
|
96
|
+
errors.push err.merge(message: "Invalid candidate id", candidateId: choice.candidateId)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
errors
|
102
|
+
end
|
103
|
+
|
104
|
+
def <=>(other)
|
105
|
+
self.contestId <=> other.contestId
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.from_json(data)
|
109
|
+
self.new(
|
110
|
+
contestId: data["contestId"],
|
111
|
+
choices: data["choices"].each_with_index.map{|data, i| Choice.from_json(data, rank: i)}
|
112
|
+
)
|
113
|
+
end
|
114
|
+
|
115
|
+
def as_json
|
116
|
+
{
|
117
|
+
"contestId" => @contestId,
|
118
|
+
"choices" => @choices.sort_by(&:rank).map(&:as_json)
|
119
|
+
}
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
class Choice
|
124
|
+
attr_reader :candidateId, :rank
|
125
|
+
def initialize(candidateId:, rank: nil)
|
126
|
+
@candidateId = candidateId
|
127
|
+
@rank = rank
|
128
|
+
end
|
129
|
+
|
130
|
+
def writeIn?
|
131
|
+
@candidateId.start_with?("WRITEIN:")
|
132
|
+
end
|
133
|
+
|
134
|
+
def abstain?
|
135
|
+
@candidateId == "ABSTAIN"
|
136
|
+
end
|
137
|
+
|
138
|
+
def candidate?
|
139
|
+
not (writeIn? or abstain?)
|
140
|
+
end
|
141
|
+
|
142
|
+
def self.from_json(data, rank: nil)
|
143
|
+
self.new(candidateId: data["candidateId"],
|
144
|
+
rank: rank)
|
145
|
+
end
|
146
|
+
|
147
|
+
def as_json
|
148
|
+
{
|
149
|
+
"candidateId" => @candidateId
|
150
|
+
}
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
|
155
|
+
end
|
156
|
+
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
module ClearElection
|
2
|
+
|
3
|
+
class Election
|
4
|
+
|
5
|
+
SCHEMA_VERSION = 0.0
|
6
|
+
|
7
|
+
attr_reader :signin, :booth, :pollsOpen, :pollsClose, :contests, :uri
|
8
|
+
|
9
|
+
def self.schema
|
10
|
+
ClearElection::Schema.election(version: SCHEMA_VERSION)
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(name:, signin:, booth:, contests:, pollsOpen:, pollsClose:, uri:nil)
|
14
|
+
@name = name
|
15
|
+
@signin = signin
|
16
|
+
@booth = booth
|
17
|
+
@contests = contests
|
18
|
+
@pollsOpen = pollsOpen
|
19
|
+
@pollsClose = pollsClose
|
20
|
+
@uri = uri
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.from_json(data, uri: nil)
|
24
|
+
JSON::Validator.validate!(schema, data, insert_defaults: true)
|
25
|
+
self.new(
|
26
|
+
name: data["name"],
|
27
|
+
signin: Agent.from_json(data["agents"]["signin"]),
|
28
|
+
booth: Agent.from_json(data["agents"]["booth"]),
|
29
|
+
contests: data["contests"].map {|data| Contest.from_json(data) },
|
30
|
+
pollsOpen: DateTime.rfc3339(data["schedule"]["pollsOpen"]),
|
31
|
+
pollsClose: DateTime.rfc3339(data["schedule"]["pollsClose"]),
|
32
|
+
uri: uri
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
def as_json()
|
37
|
+
data = {
|
38
|
+
"version" => "0.0",
|
39
|
+
"name" => @name,
|
40
|
+
"agents" => {
|
41
|
+
"signin" => @signin.as_json,
|
42
|
+
"booth" => @booth.as_json
|
43
|
+
},
|
44
|
+
"schedule" => {
|
45
|
+
"pollsOpen" => @pollsOpen.rfc3339,
|
46
|
+
"pollsClose" => @pollsClose.rfc3339,
|
47
|
+
},
|
48
|
+
"contests" => @contests.map(&:as_json)
|
49
|
+
}
|
50
|
+
JSON::Validator.validate!(Election.schema, data)
|
51
|
+
data
|
52
|
+
end
|
53
|
+
|
54
|
+
# utilities
|
55
|
+
def polls_have_not_opened?
|
56
|
+
DateTime.now < pollsOpen
|
57
|
+
end
|
58
|
+
|
59
|
+
def polls_are_now_open?
|
60
|
+
DateTime.now.between?(pollsOpen, pollsClose)
|
61
|
+
end
|
62
|
+
|
63
|
+
def polls_are_now_closed?
|
64
|
+
DateTime.now > pollsClose
|
65
|
+
end
|
66
|
+
|
67
|
+
def get_contest(contestId)
|
68
|
+
contests.find{|contest| contest.contestId == contestId}
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
class Agent
|
73
|
+
attr_reader :uri
|
74
|
+
|
75
|
+
def initialize(uri:)
|
76
|
+
@uri = URI(uri)
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.from_json(data)
|
80
|
+
self.new(uri: data["uri"].sub(%r{/?$}, '/'))
|
81
|
+
end
|
82
|
+
|
83
|
+
def as_json()
|
84
|
+
{
|
85
|
+
"uri" => @uri.to_s
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
class Contest
|
92
|
+
attr_reader :contestId, :ranked, :multiplicity, :writeIn, :candidates
|
93
|
+
|
94
|
+
def initialize(contestId:, name:, ranked: nil, multiplicity: nil, writeIn: nil, candidates:)
|
95
|
+
@contestId = contestId
|
96
|
+
@name = name
|
97
|
+
@ranked = ranked || false
|
98
|
+
@multiplicity = multiplicity || 1
|
99
|
+
@writeIn = writeIn || false
|
100
|
+
@candidates = candidates
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.from_json(data)
|
104
|
+
self.new(
|
105
|
+
contestId: data["contestId"],
|
106
|
+
name: data["name"],
|
107
|
+
ranked: data["ranked"],
|
108
|
+
multiplicity: data["multiplicity"],
|
109
|
+
writeIn: data["writeIn"],
|
110
|
+
candidates: data["candidates"].map {|data| Candidate.from_json(data)}
|
111
|
+
)
|
112
|
+
end
|
113
|
+
|
114
|
+
def as_json
|
115
|
+
{
|
116
|
+
"contestId" => @contestId,
|
117
|
+
"name" => @name,
|
118
|
+
"ranked" => @ranked,
|
119
|
+
"multiplicity" => @multiplicity,
|
120
|
+
"writeIn" => @writeIn,
|
121
|
+
"candidates" => @candidates.map(&:as_json)
|
122
|
+
}
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
class Candidate
|
128
|
+
attr_reader :candidateId
|
129
|
+
|
130
|
+
def initialize(candidateId:, name:)
|
131
|
+
@candidateId = candidateId
|
132
|
+
@name = name
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.from_json(data)
|
136
|
+
self.new(
|
137
|
+
candidateId: data["candidateId"],
|
138
|
+
name: data["name"]
|
139
|
+
)
|
140
|
+
end
|
141
|
+
|
142
|
+
def as_json
|
143
|
+
{
|
144
|
+
"candidateId" => @candidateId,
|
145
|
+
"name" => @name
|
146
|
+
}
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|