Class: GovukSchemas::Schema

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

Class Method Summary (collapse)

Class Method Details

+ (Object) all(schema_type: '*')

Return all schemas in a hash, keyed by schema name

Parameters:

  • schema_type (String)

    The type: frontend, publisher, notification or links



16
17
18
19
20
21
22
# File 'lib/govuk_schemas/schema.rb', line 16

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

+ (Object) find(schema_name, schema_type:)

Find a schema by name

Parameters:

  • schema_name (String)

    Name of the schema/format

  • schema_type (String)

    The type: frontend, publisher, notification or links



7
8
9
10
11
# File 'lib/govuk_schemas/schema.rb', line 7

def self.find(schema_name, schema_type:)
  schema_type = "publisher_v2" if schema_type == "publisher"
  file_path = "#{GovukSchemas::CONTENT_SCHEMA_DIR}/dist/formats/#{schema_name}/#{schema_type}/schema.json"
  JSON.parse(File.read(file_path))
end

+ (Object) random_schema(schema_type:)

Return a random schema of a certain type

Parameters:

  • schema_type (String)

    The type: frontend, publisher, notification or links



27
28
29
# File 'lib/govuk_schemas/schema.rb', line 27

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