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) (defaults to: "*")

    The type: frontend, publisher, notification or links

Returns:

  • (Array<Hash>)

    List of JSON schemas as hashes



22
23
24
25
26
27
# 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").each_with_object({}) do |file_path, hash|
    hash[file_path] = JSON.parse(File.read(file_path))
  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



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

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

.schema_namesArray

Return all schema names

Returns:

  • (Array)

    all the schema names



40
41
42
43
44
# File 'lib/govuk_schemas/schema.rb', line 40

def self.schema_names
  Dir.glob("#{GovukSchemas.content_schema_dir}/dist/formats/*").map do |directory|
    File.basename(directory)
  end
end