Module: ClearElection::Schema

Extended by:
Schema
Included in:
Schema
Defined in:
lib/clear-election-sdk/schema.rb

Instance Method Summary collapse

Instance Method Details

#_get(group: nil, item:, version:, expand: false) ⇒ Object



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

def _get(group:nil, item:, version:, expand: false)
  JSON.parse(File.read(root + (group||"") + "#{item}-#{version}.schema.json")).tap { |json|
    expand_refs!(json) if expand
  }
end

#api(agent, version:) ⇒ Object



23
24
25
# File 'lib/clear-election-sdk/schema.rb', line 23

def api(agent, version:)
  _get(group: "api", item: agent, version:version, expand: true)
end

#ballot(version: BALLOT_SCHEMA_VERSION) ⇒ Object



19
20
21
# File 'lib/clear-election-sdk/schema.rb', line 19

def ballot(version: BALLOT_SCHEMA_VERSION)
  _get(item: "ballot", version: version)
end

#election(version: ELECTION_SCHEMA_VERSION) ⇒ Object



15
16
17
# File 'lib/clear-election-sdk/schema.rb', line 15

def election(version: ELECTION_SCHEMA_VERSION)
  _get(item: "election", version: version)
end

#expand_refs!(json) ⇒ Object



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

def expand_refs!(json)
  json.tap {
    JSON.recurse_proc json do |item|
      if Hash === item and uri = item['$ref']
        uri = URI.parse(uri)
        if uri.scheme
          source = uri
          source = ClearElection::Schema.root.join uri.path.sub(%r{^/}, '') if uri.scheme == 'file'
          item.delete '$ref'
          item.merge! expand_refs! JSON.parse source.read
        end
      end
    end
  }
end

#rootObject



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

def root
  @root ||= Pathname.new(__FILE__).dirname.parent.parent + "schemas"
end