datapackage 0.1.3 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,111 +0,0 @@
1
- module DataPackage
2
- class Schema < Hash
3
-
4
- attr_reader :schema
5
-
6
- def initialize(schema, options = {})
7
- @registry_url = options[:registry_url]
8
- if schema.class == Hash
9
- self.merge! schema
10
- elsif schema.class == Symbol
11
- self.merge! get_schema_from_registry schema
12
- elsif schema.class == String
13
- self.merge! load_schema(schema)
14
- else
15
- raise SchemaException.new "Schema must be a URL, path, Hash or registry-identifier"
16
- end
17
- end
18
-
19
- # https://gist.github.com/vdw/f3c832df8ce271a036f2
20
- def hash_to_slashed_path(hash, path = "")
21
- return {} unless hash
22
- hash.each_with_object({}) do |(k, v), ret|
23
- key = path + k.to_s
24
-
25
- if v.is_a? Hash
26
- ret.merge! hash_to_slashed_path(v, key.to_s + "/")
27
- else
28
- ret[key] = v
29
- end
30
- end
31
- end
32
-
33
- def dereference_schema path_or_url, schema
34
- paths = hash_to_slashed_path schema
35
- ref_keys = paths.keys.select { |p| p =~ /\$ref/ }
36
- if ref_keys
37
- ref_keys = [ref_keys] unless ref_keys.is_a? Array
38
-
39
- ref_keys.each do |key|
40
- path = key.split('/')[0..-2]
41
-
42
- replacement = resolve(schema.dig(*path, '$ref'), path_or_url, schema)
43
-
44
- s = "schema#{path.map { |k| "['#{k}']" }.join}.merge! replacement"
45
- eval s
46
- s = "schema#{path.map { |k| "['#{k}']" }.join}.delete '$ref'"
47
- eval s
48
- end
49
- end
50
-
51
- schema
52
- end
53
-
54
- def resolve reference, path_or_url, schema
55
- base_path = base_path path_or_url
56
- filename, reference = reference.split '#'
57
- if filename == ''
58
- schema['define'][reference.split('/').last]
59
- else
60
- dereference_schema("#{base_path}/#{filename}", get_definitions(filename, base_path)).dig(*reference.split('/').reject(&:empty?))
61
- end
62
- end
63
-
64
- def get_definitions filename, base_path
65
- JSON.parse open("#{base_path}/#{filename}").read
66
- end
67
-
68
- def base_path path_or_url
69
- if path_or_url =~ /\A#{URI::regexp}\z/
70
- uri = URI.parse path_or_url
71
- return "#{uri.scheme}://#{uri.host}#{File.dirname uri.path}".chomp('/')
72
- else
73
-
74
- if File.directory?(path_or_url)
75
- return path_or_url
76
- else
77
- return File.expand_path File.dirname path_or_url
78
- end
79
- end
80
- end
81
-
82
- def load_schema(path_or_url)
83
- json = open(path_or_url).read
84
- schema = JSON.parse json
85
- s = dereference_schema path_or_url, schema
86
-
87
- rescue OpenURI::HTTPError => e
88
- raise SchemaException.new "Schema URL returned #{e.message}"
89
-
90
- rescue JSON::ParserError
91
- raise SchemaException.new 'Schema is not valid JSON'
92
-
93
- rescue Errno::ENOENT
94
- raise SchemaException.new "Path '#{path_or_url}' does not exist"
95
- end
96
-
97
- def get_schema_from_registry schema
98
- d = DataPackage::Registry.new(@registry_url)
99
- dereference_schema (@registry_url || d.base_path), d.get(schema.to_s)
100
- end
101
-
102
- def valid?(package)
103
- JSON::Validator.validate(self, package)
104
- end
105
-
106
- def validation_errors(package)
107
- JSON::Validator.fully_validate(self, package)
108
- end
109
-
110
- end
111
- end
@@ -1,3 +0,0 @@
1
- module DataPackage
2
- VERSION = "0.1.3"
3
- end
data/lib/datapackage.rb DELETED
@@ -1,17 +0,0 @@
1
- require 'date'
2
- require 'uri'
3
- require 'net/http'
4
- require 'csv'
5
- require 'json'
6
- require 'json-schema'
7
- require 'rest-client'
8
- require 'zip'
9
- require 'ruby_dig'
10
- require 'jsontableschema'
11
-
12
- require 'datapackage/version'
13
- require 'datapackage/exceptions'
14
- require 'datapackage/schema'
15
- require 'datapackage/resource'
16
- require 'datapackage/package'
17
- require 'datapackage/registry'