Class: GovukSchemas::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_schemas/schema.rb

Class Method Summary collapse

Class Method Details

.all(schema_type: '*') ⇒ Array<Hash>

Return all schemas in a hash, keyed by schema name

Parameters:

  • schema_type (String)

    The type: frontend, publisher, notification or links

Returns:

  • (Array<Hash>)

    List of JSON schemas as hashes


22
23
24
25
26
27
28
# File 'lib/govuk_schemas/schema.rb', line 22

def self.all(schema_type: '*')
  schema_type = "publisher_v2" if schema_type == "publisher"
  Dir.glob("#{GovukSchemas::CONTENT_SCHEMA_DIR}/dist/formats/*/#{schema_type}/*.json").reduce({}) do |hash, file_path|
    hash[file_path] = JSON.parse(File.read(file_path))
    hash
  end
end

.find(schema) ⇒ Hash

Find a schema by name

Examples:


GovukSchemas::Schema.find(links_schema: "detailed_guide")
GovukSchemas::Schema.find(frontend_schema: "detailed_guide")
GovukSchemas::Schema.find(publisher_schema: "detailed_guide")
GovukSchemas::Schema.find(notification_schema: "detailed_guide")

Parameters:

  • schema (Hash)

    Type => Name of the schema/format:

Returns:

  • (Hash)

    the JSON schema as a hash


13
14
15
16
# File 'lib/govuk_schemas/schema.rb', line 13

def self.find(schema)
  file_path = "#{GovukSchemas::CONTENT_SCHEMA_DIR}/dist/formats/#{location_for_schema_name(schema)}"
  JSON.parse(File.read(file_path))
end

.random_schema(schema_type:) ⇒ Hash

Return a random schema of a certain type

Parameters:

  • schema_type (String)

    The type: frontend, publisher, notification or links

Returns:

  • (Hash)

    a JSON schema as a hash


34
35
36
# File 'lib/govuk_schemas/schema.rb', line 34

def self.random_schema(schema_type:)
  all(schema_type: schema_type).values.sample
end