json_refs 0.1.1 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: e623d13bcd51a7a1e254203e15c508065467469b
4
- data.tar.gz: 3a3ad67d1afd4df069fa1f4810a0a8ed20a03232
2
+ SHA256:
3
+ metadata.gz: 873527fb07ff698ff5cae1f2954cf403e876c2e6353fb5a395e7eab5517dd08a
4
+ data.tar.gz: d9d3c3a6b2493f1150c29a1834b72a474089690e0aab0ad4d6e32670dfa55e74
5
5
  SHA512:
6
- metadata.gz: 582f64c455143f262e407f97aabf3e4223ff7d59dab9c0d5a0ff6fa2ff874b287a063a583845e3cf77749565412dc881dbfd57e0a24346dcdc6cef39023fa48d
7
- data.tar.gz: d23cc3d915cadca4a3b1b70c25cd2d63fc80ddd043e4e27d66eb73d676916805f57b86f9cc8eaeec707b9dfd47569ebc1992851f1c509c86b0921d9e2e333100
6
+ metadata.gz: f1ef5cb22467df59eb10e0a0f4bdc5fbdcae6e6260f7d8f59a26b34447b325a8d2bb4bec2c31715c997c99a2e071c6669692fc3f8620c9495915a740cd191a4e
7
+ data.tar.gz: 719c2b6dd54023a0f788d30ddc3caa0526bff1c3f80bfa6c1f395b399cd11f8d77563283c03c2da42802abf49e8ca50eec602709498ee2fd95b9d3da9c718764
@@ -1,5 +1,13 @@
1
1
  sudo: false
2
+
2
3
  language: ruby
4
+
3
5
  rvm:
4
- - jruby-head
5
- - 2.4.2
6
+ - 2.3.8
7
+ - 2.4.10
8
+ - 2.5.8
9
+ - 2.6.6
10
+ - 2.7.1
11
+
12
+ before_install:
13
+ - gem install -N bundler
data/README.md CHANGED
@@ -29,6 +29,40 @@ 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
+ JsonRefs.(json)
43
+ ```
44
+
45
+ YAML is also available for file format.
46
+ ```ruby
47
+ json = {
48
+ 'a' => {
49
+ '$ref' => '/path/to/yaml.yml'
50
+ },
51
+ 'b' => {
52
+ '$ref' => '/path/to/yaml.yaml'
53
+ }
54
+ }
55
+ JsonRefs.(json)
56
+ ```
57
+
58
+ In case you don't want to resolve local references or file references, you have the following options
59
+ to pass down when you initialize the JSONRefs. If you don't pass the following, both references will be resolved.
60
+ ```ruby
61
+ JsonRefs.(json, { resolve_local_ref: false, resolve_file_ref: true })
62
+ ```
63
+
64
+ JSONRef supports recursive reference resolving. If one the files that you reference have another reference in it, JSONRef will try to automatically resolve that.
65
+
32
66
  ## Development
33
67
 
34
68
  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.
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_dependency "hana"
25
25
 
26
- spec.add_development_dependency "bundler", "~> 1.15"
27
- spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "bundler", "~> 2.0"
27
+ spec.add_development_dependency "rake", ">= 10.0"
28
28
  spec.add_development_dependency "rspec", "~> 3.0"
29
29
  end
@@ -1,16 +1,19 @@
1
- require "json_refs/version"
2
- require 'hana'
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
- def self.call(doc)
8
- Dereferencer.new(doc).call
5
+ class << self
6
+ def call(doc, options = {})
7
+ Dereferencer.new(doc, options).call
8
+ end
9
+
10
+ alias_method :dereference, :call
9
11
  end
10
12
 
11
13
  class Dereferencer
12
- def initialize(doc)
14
+ def initialize(doc, options = {})
13
15
  @doc = doc
16
+ @options = options
14
17
  end
15
18
 
16
19
  def call(doc = @doc, keys = [])
@@ -41,15 +44,59 @@ module JsonRefs
41
44
  end
42
45
 
43
46
  def referenced_value(referenced_path)
44
- if referenced_path =~ /^#/
45
- Hana::Pointer.new(referenced_path[1..-1]).eval(@doc)
46
- elsif referenced_path =~ /^(http:\/\/|https:\/\/)/
47
- json = Net::HTTP.get(URI.parse(referenced_path))
48
- JSON.load(json)
47
+ if @options[:logging] == true
48
+ puts "De-referencing #{referenced_path}"
49
+ end
50
+
51
+ filepath, pointer = referenced_path.split('#')
52
+ pointer.prepend('#') if pointer
53
+ return dereference_local(pointer) if filepath.empty?
54
+
55
+ dereferenced_file = dereference_file(filepath)
56
+ return dereferenced_file if pointer.nil?
57
+
58
+ JsonRefs::DereferenceHandler::Local.new(
59
+ doc: dereferenced_file,
60
+ path: pointer
61
+ ).call
62
+ end
63
+
64
+ def dereference_local(referenced_path)
65
+ if @options[:resolve_local_ref] === false
66
+ return { '$ref' => referenced_path }
67
+ end
68
+
69
+ klass = JsonRefs::DereferenceHandler::Local
70
+ klass.new(path: referenced_path, doc: @doc).call
71
+ end
72
+
73
+ def dereference_file(referenced_path)
74
+ if @options[:resolve_file_ref] === false
75
+ return { '$ref' => referenced_path }
76
+ end
77
+
78
+ klass = JsonRefs::DereferenceHandler::File
79
+
80
+ # Checking for "://" in a URL like http://something.com so as to determine if it's a remote URL
81
+ remote_uri = referenced_path =~ /:\/\//
82
+
83
+ if remote_uri
84
+ klass.new(path: referenced_path, doc: @doc).call
49
85
  else
50
- f = File.open(referenced_path)
51
- JSON.load(f)
86
+ recursive_dereference(referenced_path, klass)
87
+ end
88
+ end
89
+
90
+ def recursive_dereference(referenced_path, klass)
91
+ directory = File.dirname(referenced_path)
92
+ filename = File.basename(referenced_path)
93
+
94
+ dereferenced_doc = {}
95
+ Dir.chdir(directory) do
96
+ referenced_doc = klass.new(path: filename, doc: @doc).call
97
+ dereferenced_doc = Dereferencer.new(referenced_doc, @options).call
52
98
  end
99
+ dereferenced_doc
53
100
  end
54
101
  end
55
102
  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,53 @@
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
+ @body = read_reference_file(filename)
31
+ ext = File.extname(filename)[1..-1]
32
+ ext ||= 'json' if json?(@body)
33
+ ext && EXTENSIONS.include?(ext) ? EXTENSIONS[ext].new.call(@body) : @body
34
+ end
35
+
36
+ def json?(file_body)
37
+ JSON.load(file_body)
38
+ true
39
+ rescue JSON::ParserError => e
40
+ false
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
52
+ end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module JsonRefs
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.6"
3
3
  end
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.1
4
+ version: 0.1.6
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-18 00:00:00.000000000 Z
11
+ date: 2020-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hana
@@ -30,26 +30,26 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.15'
33
+ version: '2.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.15'
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
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
@@ -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:
@@ -104,8 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
106
  - !ruby/object:Gem::Version
105
107
  version: '0'
106
108
  requirements: []
107
- rubyforge_project:
108
- rubygems_version: 2.6.11
109
+ rubygems_version: 3.0.3
109
110
  signing_key:
110
111
  specification_version: 4
111
112
  summary: Dereference JSON Pointer