clear-election-sdk 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/Gemfile +1 -4
- data/README.md +7 -0
- data/lib/clear-election-sdk/ballot.rb +2 -2
- data/lib/clear-election-sdk/election.rb +3 -3
- data/lib/clear-election-sdk/schema.rb +28 -9
- data/lib/clear-election-sdk/version.rb +1 -1
- data/schemas/election-0.0.schema.json +1 -1
- data/spec/election_spec.rb +10 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e304320e6664ae5f0b56944eda5209ae7eda87d4
|
4
|
+
data.tar.gz: 344e5b381317c69805c682453f4d11d84a553ca3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b078c52a8326387f4212edcd2f2de3fc36ff55e9cf2ec0fad132b9e6c90be3d8a633562f1cb2e5b90f1f28d83bdf7a08857950fc1d03752a32834c16dafb09a
|
7
|
+
data.tar.gz: 971f9d2b3cebcfeda1a420b6d089569c4bc3ea4776c87008da8db42a6e780c55b624110b8539553e8ffcf1d7c55e101dd3d357159673640c72e16ffd994b0afb
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.2.2
|
data/Gemfile
CHANGED
@@ -4,7 +4,4 @@ source 'https://rubygems.org'
|
|
4
4
|
gemspec
|
5
5
|
|
6
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"
|
7
|
+
gem 'byebug'
|
data/README.md
CHANGED
@@ -48,6 +48,13 @@ Or install it yourself as:
|
|
48
48
|
|
49
49
|
$ gem install clear_election
|
50
50
|
|
51
|
+
## History
|
52
|
+
|
53
|
+
* 0.1.2 Bug fix: schema expansion
|
54
|
+
* 0.1.1 Dependency fix
|
55
|
+
* 0.1.0 Results, json roundtrip
|
56
|
+
* 0.0.1 Initial release
|
57
|
+
|
51
58
|
## Contributing
|
52
59
|
|
53
60
|
1. Fork it ( https://github.com/[my-github-username]/clear_election/fork )
|
@@ -11,7 +11,7 @@ module ClearElection
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.from_json(data)
|
14
|
-
errors =
|
14
|
+
errors = ClearElection::Schema.validate(schema, data, insert_defaults: true)
|
15
15
|
return self.new(ballotId: nil, uniquifier: nil, contests: [], errors: errors) unless errors.empty?
|
16
16
|
|
17
17
|
self.new(
|
@@ -51,7 +51,7 @@ module ClearElection
|
|
51
51
|
"contests" => @contests.map(&:as_json),
|
52
52
|
}
|
53
53
|
data["demographic"] = @demographic if @demographic
|
54
|
-
|
54
|
+
ClearElection::Schema.validate!(Ballot.schema, data)
|
55
55
|
data
|
56
56
|
end
|
57
57
|
|
@@ -23,7 +23,7 @@ module ClearElection
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def self.from_json(data, uri: nil)
|
26
|
-
|
26
|
+
ClearElection::Schema.validate!(schema, data, insert_defaults: true)
|
27
27
|
self.new(
|
28
28
|
name: data["name"],
|
29
29
|
signin: Agent.from_json(data["agents"]["signin"]),
|
@@ -55,7 +55,7 @@ module ClearElection
|
|
55
55
|
"voters" => @voters,
|
56
56
|
"ballots" => @ballots.map(&:as_json)
|
57
57
|
}
|
58
|
-
|
58
|
+
ClearElection::Schema.validate!(Election.schema, data)
|
59
59
|
data
|
60
60
|
end
|
61
61
|
|
@@ -86,7 +86,7 @@ module ClearElection
|
|
86
86
|
attr_reader :uri
|
87
87
|
|
88
88
|
def initialize(uri:)
|
89
|
-
@uri = URI(uri)
|
89
|
+
@uri = Addressable::URI.parse(uri)
|
90
90
|
end
|
91
91
|
|
92
92
|
def self.from_json(data)
|
@@ -6,12 +6,6 @@ module ClearElection
|
|
6
6
|
@root ||= Pathname.new(__FILE__).dirname.parent.parent + "schemas"
|
7
7
|
end
|
8
8
|
|
9
|
-
def _get(group:nil, item:, version:, expand: false)
|
10
|
-
JSON.parse(File.read(root + (group||"") + "#{item}-#{version}.schema.json")).tap { |json|
|
11
|
-
expand_refs!(json) if expand
|
12
|
-
}
|
13
|
-
end
|
14
|
-
|
15
9
|
def election(version: ELECTION_SCHEMA_VERSION)
|
16
10
|
_get(item: "election", version: version)
|
17
11
|
end
|
@@ -20,20 +14,45 @@ module ClearElection
|
|
20
14
|
_get(item: "ballot", version: version)
|
21
15
|
end
|
22
16
|
|
17
|
+
def validate!(*args, **kwd)
|
18
|
+
validate(*args, **kwd.merge(raise_on_error: true))
|
19
|
+
end
|
20
|
+
|
21
|
+
def validate(schema, data, insert_defaults: nil, raise_on_error: false)
|
22
|
+
errors = []
|
23
|
+
Dir.chdir root do
|
24
|
+
args = { insert_defaults: insert_defaults }
|
25
|
+
if raise_on_error
|
26
|
+
method = :validate!
|
27
|
+
else
|
28
|
+
method = :fully_validate
|
29
|
+
args[:errors_as_objects] = true
|
30
|
+
end
|
31
|
+
errors = JSON::Validator.send method, schema, data, **args
|
32
|
+
end
|
33
|
+
errors
|
34
|
+
end
|
35
|
+
|
36
|
+
def _get(group:nil, item:, version:, expand: false)
|
37
|
+
JSON.parse(File.read(root + (group||"") + "#{item}-#{version}.schema.json")).tap { |json|
|
38
|
+
_expand_refs!(json) if expand
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
23
42
|
def api(agent, version:)
|
24
43
|
_get(group: "api", item: agent, version: version, expand: true)
|
25
44
|
end
|
26
45
|
|
27
|
-
def
|
46
|
+
def _expand_refs!(json)
|
28
47
|
json.tap {
|
29
48
|
JSON.recurse_proc json do |item|
|
30
49
|
if Hash === item and uri = item['$ref']
|
31
|
-
uri = URI.parse(uri)
|
50
|
+
uri = Addressable::URI.parse(uri)
|
32
51
|
if uri.scheme
|
33
52
|
source = uri
|
34
53
|
source = ClearElection::Schema.root.join uri.path.sub(%r{^/}, '') if uri.scheme == 'file'
|
35
54
|
item.delete '$ref'
|
36
|
-
item.merge!
|
55
|
+
item.merge! _expand_refs! JSON.parse source.read
|
37
56
|
end
|
38
57
|
end
|
39
58
|
end
|
data/spec/election_spec.rb
CHANGED
@@ -57,12 +57,22 @@ describe ClearElection::Election do
|
|
57
57
|
roundtrip ClearElection::Factory.election(returns: true)
|
58
58
|
end
|
59
59
|
|
60
|
+
it "works in arbitrary directory" do
|
61
|
+
election = ClearElection::Factory.election
|
62
|
+
Dir.chdir Dir.mktmpdir do |dir|
|
63
|
+
expect{ election.as_json }.to_not raise_error
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
60
69
|
def roundtrip(election)
|
61
70
|
original_json = JSON.generate election.as_json
|
62
71
|
final_json = JSON.generate ClearElection::Election.from_json(JSON.parse original_json).as_json
|
63
72
|
expect(final_json).to eq original_json
|
64
73
|
end
|
65
74
|
|
75
|
+
|
66
76
|
end
|
67
77
|
|
68
78
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clear-election-sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ronen barzel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -189,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
189
189
|
version: '0'
|
190
190
|
requirements: []
|
191
191
|
rubyforge_project:
|
192
|
-
rubygems_version: 2.
|
192
|
+
rubygems_version: 2.4.5
|
193
193
|
signing_key:
|
194
194
|
specification_version: 4
|
195
195
|
summary: Ruby SDK for working with ClearElection data
|