Class: DataPackage::Schema

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, options = {}) ⇒ Schema



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/datapackage/schema.rb', line 6

def initialize(schema, options = {})
  @registry_url = options[:registry_url]
  if schema.class == Hash
    self.merge! schema
  elsif schema.class == Symbol
    self.merge! get_schema_from_registry schema
  elsif schema.class == String
    self.merge! load_schema(schema)
  else
    raise SchemaException.new "Schema must be a URL, path, Hash or registry-identifier"
  end
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



4
5
6
# File 'lib/datapackage/schema.rb', line 4

def schema
  @schema
end

Instance Method Details

#base_path(path_or_url) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/datapackage/schema.rb', line 68

def base_path path_or_url
  if path_or_url =~ /\A#{URI::regexp}\z/
    uri = URI.parse path_or_url
    return "#{uri.scheme}://#{uri.host}#{File.dirname uri.path}".chomp('/')
  else

    if File.directory?(path_or_url)
      return path_or_url
    else
      return File.expand_path File.dirname path_or_url
    end
  end
end

#dereference_schema(path_or_url, schema) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/datapackage/schema.rb', line 33

def dereference_schema path_or_url, schema
  paths = hash_to_slashed_path schema
  ref_keys = paths.keys.select { |p| p =~ /\$ref/ }
  if ref_keys
    ref_keys = [ref_keys] unless ref_keys.is_a? Array

    ref_keys.each do |key|
      path = key.split('/')[0..-2]

      replacement = resolve(schema.dig(*path, '$ref'), path_or_url, schema)

      s = "schema#{path.map { |k| "['#{k}']" }.join}.merge! replacement"
      eval s
      s = "schema#{path.map { |k| "['#{k}']" }.join}.delete '$ref'"
      eval s
    end
  end

  schema
end

#get_definitions(filename, base_path) ⇒ Object



64
65
66
# File 'lib/datapackage/schema.rb', line 64

def get_definitions filename, base_path
  JSON.parse open("#{base_path}/#{filename}").read
end

#get_schema_from_registry(schema) ⇒ Object



97
98
99
100
# File 'lib/datapackage/schema.rb', line 97

def get_schema_from_registry schema
  d = DataPackage::Registry.new(@registry_url)
  dereference_schema (@registry_url || d.base_path), d.get(schema.to_s)
end

#hash_to_slashed_path(hash, path = "") ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/datapackage/schema.rb', line 20

def hash_to_slashed_path(hash, path = "")
  return {} unless hash
  hash.each_with_object({}) do |(k, v), ret|
    key = path + k.to_s

    if v.is_a? Hash
      ret.merge! hash_to_slashed_path(v, key.to_s + "/")
    else
      ret[key] = v
    end
  end
end

#load_schema(path_or_url) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/datapackage/schema.rb', line 82

def load_schema(path_or_url)
  json = open(path_or_url).read
  schema = JSON.parse json
  s = dereference_schema path_or_url, schema

rescue OpenURI::HTTPError => e
  raise SchemaException.new "Schema URL returned #{e.message}"

rescue JSON::ParserError
  raise SchemaException.new 'Schema is not valid JSON'

rescue Errno::ENOENT
  raise SchemaException.new "Path '#{path_or_url}' does not exist"
end

#resolve(reference, path_or_url, schema) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/datapackage/schema.rb', line 54

def resolve reference, path_or_url, schema
  base_path = base_path path_or_url
  filename, reference = reference.split '#'
  if filename == ''
    schema['define'][reference.split('/').last]
  else
    dereference_schema("#{base_path}/#{filename}", get_definitions(filename, base_path)).dig(*reference.split('/').reject(&:empty?))
  end
end

#valid?(package) ⇒ Boolean



102
103
104
# File 'lib/datapackage/schema.rb', line 102

def valid?(package)
  JSON::Validator.validate(self, package)
end

#validation_errors(package) ⇒ Object



106
107
108
# File 'lib/datapackage/schema.rb', line 106

def validation_errors(package)
  JSON::Validator.fully_validate(self, package)
end