json_refs 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +3 -1
- data/README.md +12 -0
- data/lib/json_refs.rb +4 -13
- data/lib/json_refs/dereference_handler.rb +27 -0
- data/lib/json_refs/loader.rb +44 -0
- data/lib/json_refs/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4f17701df4b9c089a11c31f6bde308873931802
|
4
|
+
data.tar.gz: 6f581341bdf53bf52e6b27ce88a774a805ad8e66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f29bdc8161c0a125a6875e3171cbaa6519c57027eeb28ab4a6be3f22fe7374917b9e0ce2fb323c56b8e13bdcd0509f36e1373425d3e9a98233df638fb70ccbd
|
7
|
+
data.tar.gz: cfc0d80386cde5a632ca668011c94aefcebca2e62fe81c355533a0584199ef35e48111ec866e4e289a22a7ebbe08d4deec44850852548cb4ee9d319d8524d706
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -29,6 +29,18 @@ json = { 'a' => 'foo', 'b' => { '$ref' => '#/a' } }
|
|
29
29
|
JsonRefs.(json) # {"a"=>"foo", "b"=>"foo"}
|
30
30
|
```
|
31
31
|
|
32
|
+
local path or remote url is available
|
33
|
+
```ruby
|
34
|
+
json = {
|
35
|
+
'a' => {
|
36
|
+
'$ref' => '/path/to/file'
|
37
|
+
},
|
38
|
+
'b' => {
|
39
|
+
'$ref' => 'http://httpbin.org/user-agent'
|
40
|
+
}
|
41
|
+
}
|
42
|
+
```
|
43
|
+
|
32
44
|
## Development
|
33
45
|
|
34
46
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/json_refs.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require '
|
3
|
-
require 'net/http'
|
4
|
-
require 'json'
|
1
|
+
require 'json_refs/version'
|
2
|
+
require 'json_refs/dereference_handler'
|
5
3
|
|
6
4
|
module JsonRefs
|
7
5
|
def self.call(doc)
|
@@ -41,15 +39,8 @@ module JsonRefs
|
|
41
39
|
end
|
42
40
|
|
43
41
|
def referenced_value(referenced_path)
|
44
|
-
|
45
|
-
|
46
|
-
elsif referenced_path =~ /^(http:\/\/|https:\/\/)/
|
47
|
-
json = Net::HTTP.get(URI.parse(referenced_path))
|
48
|
-
JSON.load(json)
|
49
|
-
else
|
50
|
-
f = File.open(referenced_path)
|
51
|
-
JSON.load(f)
|
52
|
-
end
|
42
|
+
klass = referenced_path =~ /^#/ ? JsonRefs::DereferenceHandler::Local : JsonRefs::DereferenceHandler::File
|
43
|
+
klass.new(path: referenced_path, doc: @doc).call
|
53
44
|
end
|
54
45
|
end
|
55
46
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'hana'
|
2
|
+
require 'json_refs/loader'
|
3
|
+
|
4
|
+
module JsonRefs
|
5
|
+
module DereferenceHandler
|
6
|
+
class File
|
7
|
+
def initialize(options = {})
|
8
|
+
@path = options.fetch(:path)
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
JsonRefs::Loader.handle(@path)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Local
|
17
|
+
def initialize(options = {})
|
18
|
+
@path = options.fetch(:path)
|
19
|
+
@doc = options.fetch(:doc)
|
20
|
+
end
|
21
|
+
|
22
|
+
def call
|
23
|
+
Hana::Pointer.new(@path[1..-1]).eval(@doc)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'json'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module JsonRefs
|
6
|
+
class Loader
|
7
|
+
class Json
|
8
|
+
def call(file)
|
9
|
+
JSON.load(file)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Yaml
|
14
|
+
def call(file)
|
15
|
+
YAML.load(file)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
EXTENSIONS = {
|
20
|
+
'json' => JsonRefs::Loader::Json,
|
21
|
+
'yaml' => JsonRefs::Loader::Yaml,
|
22
|
+
'yml' => JsonRefs::Loader::Yaml,
|
23
|
+
}.freeze
|
24
|
+
|
25
|
+
def self.handle(filename)
|
26
|
+
new.handle(filename)
|
27
|
+
end
|
28
|
+
|
29
|
+
def handle(filename)
|
30
|
+
f = open(filename)
|
31
|
+
@body = f.read
|
32
|
+
ext = File.extname(filename)[1..-1]
|
33
|
+
ext ||= 'json' if json?(@body)
|
34
|
+
ext && EXTENSIONS.include?(ext) ? EXTENSIONS[ext].new.call(@body) : @body
|
35
|
+
end
|
36
|
+
|
37
|
+
def json?(file_body)
|
38
|
+
JSON.load(file_body)
|
39
|
+
true
|
40
|
+
rescue JSON::ParserError => e
|
41
|
+
false
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/json_refs/version.rb
CHANGED
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Makoto Tajitsu
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11-
|
11
|
+
date: 2017-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hana
|
@@ -84,6 +84,8 @@ files:
|
|
84
84
|
- bin/setup
|
85
85
|
- json_refs.gemspec
|
86
86
|
- lib/json_refs.rb
|
87
|
+
- lib/json_refs/dereference_handler.rb
|
88
|
+
- lib/json_refs/loader.rb
|
87
89
|
- lib/json_refs/version.rb
|
88
90
|
homepage: https://github.com/tzmfreedom/json_refs
|
89
91
|
licenses:
|