Module: Nidyx::Reader

Extended by:
Reader
Included in:
Reader
Defined in:
lib/nidyx/reader.rb

Defined Under Namespace

Classes: EmptySchemaError

Instance Method Summary collapse

Instance Method Details

#empty_schema?(schema) ⇒ Boolean

Returns true if the schema is empty.

Parameters:

  • schema (Hash)

    an object containing JSON schema

Returns:

  • (Boolean)

    true if the schema is empty



39
40
41
42
43
# File 'lib/nidyx/reader.rb', line 39

def empty_schema?(schema)
  props = schema[PROPERTIES_KEY]
  items = schema[ITEMS_KEY]
  (!props || props.empty?) && (!items || items.empty?)
end

#read(path) ⇒ Hash

Reads JSON from a file

Parameters:

  • path (String)

    path of the file to read

Returns:

  • (Hash)

    the parsed JSON



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nidyx/reader.rb', line 15

def read(path)
  schema = nil

  begin
    # TODO: validate this is legitimate JSON Schema
    schema = JSON.parse(IO.read(path))
    raise EmptySchemaError if empty_schema?(schema)
  rescue JSON::JSONError => e
    puts "Encountered an error reading JSON from #{path}"
    puts e.message
    exit 1
  rescue EmptySchemaError
    puts "Schema read from #{path} is empty"
    exit 1
  rescue StandardError => e
    puts e.message
    exit 1
  end

  schema
end