json_refs 0.1.3 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +10 -3
- data/json_refs.gemspec +1 -1
- data/lib/json_refs/loader.rb +11 -2
- data/lib/json_refs/version.rb +1 -1
- data/lib/json_refs.rb +28 -11
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1189625a84d998e3d9a6f249368c187bf0144393271aec06398830ba0ddc66a1
|
4
|
+
data.tar.gz: c079bf4dbf63782819d5d1081396c7f9e8ad9f155e5ef701ca337a6a21f3a176
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b18e096f64d639506cc39728e426af5ff276bee765c69331ba9a32d401299439367a851142717ab51214755ef2934eb34793a582e3cda73782fd8f5394eef3ce
|
7
|
+
data.tar.gz: afab208bf473c6f575761b0e51a83a99e8abefe625e185f13ebb93caed688e9e882eba9bdec49925a53e646eb22d61f59b6d4d8aec3db219ef2f28e09964c1fa
|
data/.travis.yml
CHANGED
data/json_refs.gemspec
CHANGED
@@ -24,6 +24,6 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_dependency "hana"
|
25
25
|
|
26
26
|
spec.add_development_dependency "bundler", "~> 2.0"
|
27
|
-
spec.add_development_dependency "rake", "
|
27
|
+
spec.add_development_dependency "rake", ">= 10.0"
|
28
28
|
spec.add_development_dependency "rspec", "~> 3.0"
|
29
29
|
end
|
data/lib/json_refs/loader.rb
CHANGED
@@ -27,8 +27,7 @@ module JsonRefs
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def handle(filename)
|
30
|
-
|
31
|
-
@body = f.read
|
30
|
+
@body = read_reference_file(filename)
|
32
31
|
ext = File.extname(filename)[1..-1]
|
33
32
|
ext ||= 'json' if json?(@body)
|
34
33
|
ext && EXTENSIONS.include?(ext) ? EXTENSIONS[ext].new.call(@body) : @body
|
@@ -40,5 +39,15 @@ module JsonRefs
|
|
40
39
|
rescue JSON::ParserError => e
|
41
40
|
false
|
42
41
|
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def read_reference_file(filename)
|
46
|
+
if RUBY_VERSION >= '2.7.0'
|
47
|
+
URI.open(filename, 'r', &:read)
|
48
|
+
else
|
49
|
+
open(filename, 'r', &:read)
|
50
|
+
end
|
51
|
+
end
|
43
52
|
end
|
44
53
|
end
|
data/lib/json_refs/version.rb
CHANGED
data/lib/json_refs.rb
CHANGED
@@ -2,8 +2,12 @@ require 'json_refs/version'
|
|
2
2
|
require 'json_refs/dereference_handler'
|
3
3
|
|
4
4
|
module JsonRefs
|
5
|
-
|
6
|
-
|
5
|
+
class << self
|
6
|
+
def call(doc, options = {})
|
7
|
+
Dereferencer.new(doc, options).call
|
8
|
+
end
|
9
|
+
|
10
|
+
alias_method :dereference, :call
|
7
11
|
end
|
8
12
|
|
9
13
|
class Dereferencer
|
@@ -36,7 +40,14 @@ module JsonRefs
|
|
36
40
|
target = paths.inject(@doc) do |obj, key|
|
37
41
|
obj[key]
|
38
42
|
end
|
39
|
-
|
43
|
+
value = follow_referenced_value(referenced_path)
|
44
|
+
target[key] = value
|
45
|
+
end
|
46
|
+
|
47
|
+
def follow_referenced_value(referenced_path)
|
48
|
+
value = referenced_value(referenced_path)
|
49
|
+
return referenced_value(value['$ref']) if value.is_a?(Hash) && value.has_key?('$ref')
|
50
|
+
value
|
40
51
|
end
|
41
52
|
|
42
53
|
def referenced_value(referenced_path)
|
@@ -44,16 +55,22 @@ module JsonRefs
|
|
44
55
|
puts "De-referencing #{referenced_path}"
|
45
56
|
end
|
46
57
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
58
|
+
filepath, pointer = referenced_path.split('#')
|
59
|
+
pointer.prepend('#') if pointer
|
60
|
+
return dereference_local(pointer) if filepath.empty?
|
61
|
+
|
62
|
+
dereferenced_file = dereference_file(filepath)
|
63
|
+
return dereferenced_file if pointer.nil?
|
64
|
+
|
65
|
+
JsonRefs::DereferenceHandler::Local.new(
|
66
|
+
doc: dereferenced_file,
|
67
|
+
path: pointer
|
68
|
+
).call
|
52
69
|
end
|
53
70
|
|
54
71
|
def dereference_local(referenced_path)
|
55
72
|
if @options[:resolve_local_ref] === false
|
56
|
-
return { '$ref'
|
73
|
+
return { '$ref' => referenced_path }
|
57
74
|
end
|
58
75
|
|
59
76
|
klass = JsonRefs::DereferenceHandler::Local
|
@@ -62,7 +79,7 @@ module JsonRefs
|
|
62
79
|
|
63
80
|
def dereference_file(referenced_path)
|
64
81
|
if @options[:resolve_file_ref] === false
|
65
|
-
return { '$ref'
|
82
|
+
return { '$ref' => referenced_path }
|
66
83
|
end
|
67
84
|
|
68
85
|
klass = JsonRefs::DereferenceHandler::File
|
@@ -80,7 +97,7 @@ module JsonRefs
|
|
80
97
|
def recursive_dereference(referenced_path, klass)
|
81
98
|
directory = File.dirname(referenced_path)
|
82
99
|
filename = File.basename(referenced_path)
|
83
|
-
|
100
|
+
|
84
101
|
dereferenced_doc = {}
|
85
102
|
Dir.chdir(directory) do
|
86
103
|
referenced_doc = klass.new(path: filename, doc: @doc).call
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_refs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Makoto Tajitsu
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hana
|
@@ -42,14 +42,14 @@ dependencies:
|
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '10.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '10.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
@@ -91,7 +91,7 @@ homepage: https://github.com/tzmfreedom/json_refs
|
|
91
91
|
licenses:
|
92
92
|
- MIT
|
93
93
|
metadata: {}
|
94
|
-
post_install_message:
|
94
|
+
post_install_message:
|
95
95
|
rdoc_options: []
|
96
96
|
require_paths:
|
97
97
|
- lib
|
@@ -106,8 +106,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
- !ruby/object:Gem::Version
|
107
107
|
version: '0'
|
108
108
|
requirements: []
|
109
|
-
rubygems_version: 3.
|
110
|
-
signing_key:
|
109
|
+
rubygems_version: 3.2.22
|
110
|
+
signing_key:
|
111
111
|
specification_version: 4
|
112
112
|
summary: Dereference JSON Pointer
|
113
113
|
test_files: []
|