json_refs 0.1.0 → 0.1.5

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: f0bcdbaa895884700d53a32dcc4422c0e6618f17
4
- data.tar.gz: 9f8f74e7311da94f83fc899bcff9fc92c173d4a2
2
+ SHA256:
3
+ metadata.gz: afda1a9f8de85cb20c9baadba689f01b21a664a16d5c47e405c3c2dffc7f8203
4
+ data.tar.gz: db450eb89016442f61af6325fc7f66dc1e642bb70420a087277d5172025469ac
5
5
  SHA512:
6
- metadata.gz: 6c3efed79a7b6a767270220a648fb98f36f112453f0d6c1c134ceb2d0c40a1b5ade83c5bd1cf0a8063e8f87dd879eef181bdfa662131791e7bcc78b7cf59cca8
7
- data.tar.gz: 4001d016043bd9dee2c4c299ac5c7578a1efa84b101d91ff86e578aed7cabc6722f192803b13e29fab0cf3fe12ecbda3ed81a3200620fde8a1b3d34b2646af2c
6
+ metadata.gz: c90414cb7617ebf433c1a05079600032f692136bcca25ae3388b21ff27c8764750cdcf82d69f300d159fef97f629b1147de36c81d84334735c8af20df8643deb
7
+ data.tar.gz: 02c43b3dc1aa1737050b863b591b7c03d4acf9f8ef54885643cfe2d19c4aa37d11e77422a7baf047bda8db83dc46d9e77101417a90b6ab32eff12c95256fd8ac
@@ -1,5 +1,13 @@
1
1
  sudo: false
2
+
2
3
  language: ruby
4
+
3
5
  rvm:
4
- - 2.4.1
5
- before_install: gem install bundler -v 1.15.4
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
@@ -1,8 +1,10 @@
1
+ [![Build Status](https://travis-ci.org/tzmfreedom/json_refs.svg?branch=master)](https://travis-ci.org/tzmfreedom/json_refs)
2
+
1
3
  # JsonRefs
2
4
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/json_refs`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ Dereference JSON reference with JSON Pointer.
4
6
 
5
- TODO: Delete this and the text above, and describe your gem
7
+ '$refs' value is replaced with referenced value.
6
8
 
7
9
  ## Installation
8
10
 
@@ -22,7 +24,44 @@ Or install it yourself as:
22
24
 
23
25
  ## Usage
24
26
 
25
- TODO: Write usage instructions here
27
+ ```ruby
28
+ json = { 'a' => 'foo', 'b' => { '$ref' => '#/a' } }
29
+ JsonRefs.(json) # {"a"=>"foo", "b"=>"foo"}
30
+ ```
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.
26
65
 
27
66
  ## Development
28
67
 
@@ -32,7 +71,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
71
 
33
72
  ## Contributing
34
73
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/json_refs.
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tzmfreedom/json_refs.
36
75
 
37
76
  ## License
38
77
 
@@ -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,14 +1,19 @@
1
- require "json_refs/version"
2
- require 'hana'
1
+ require 'json_refs/version'
2
+ require 'json_refs/dereference_handler'
3
3
 
4
4
  module JsonRefs
5
- def self.call(doc)
6
- 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
7
11
  end
8
12
 
9
13
  class Dereferencer
10
- def initialize(doc)
14
+ def initialize(doc, options = {})
11
15
  @doc = doc
16
+ @options = options
12
17
  end
13
18
 
14
19
  def call(doc = @doc, keys = [])
@@ -35,7 +40,57 @@ module JsonRefs
35
40
  target = paths.inject(@doc) do |obj, key|
36
41
  obj[key]
37
42
  end
38
- target[key] = Hana::Pointer.new(referenced_path[1..-1]).eval(@doc)
43
+ target[key] = referenced_value(referenced_path)
44
+ end
45
+
46
+ def referenced_value(referenced_path)
47
+ if @options[:logging] == true
48
+ puts "De-referencing #{referenced_path}"
49
+ end
50
+
51
+ if referenced_path =~ /^#/
52
+ dereference_local(referenced_path)
53
+ else
54
+ dereference_file(referenced_path)
55
+ end
56
+ end
57
+
58
+ def dereference_local(referenced_path)
59
+ if @options[:resolve_local_ref] === false
60
+ return { '$ref' => referenced_path }
61
+ end
62
+
63
+ klass = JsonRefs::DereferenceHandler::Local
64
+ klass.new(path: referenced_path, doc: @doc).call
65
+ end
66
+
67
+ def dereference_file(referenced_path)
68
+ if @options[:resolve_file_ref] === false
69
+ return { '$ref' => referenced_path }
70
+ end
71
+
72
+ klass = JsonRefs::DereferenceHandler::File
73
+
74
+ # Checking for "://" in a URL like http://something.com so as to determine if it's a remote URL
75
+ remote_uri = referenced_path =~ /:\/\//
76
+
77
+ if remote_uri
78
+ klass.new(path: referenced_path, doc: @doc).call
79
+ else
80
+ recursive_dereference(referenced_path, klass)
81
+ end
82
+ end
83
+
84
+ def recursive_dereference(referenced_path, klass)
85
+ directory = File.dirname(referenced_path)
86
+ filename = File.basename(referenced_path)
87
+
88
+ dereferenced_doc = {}
89
+ Dir.chdir(directory) do
90
+ referenced_doc = klass.new(path: filename, doc: @doc).call
91
+ dereferenced_doc = Dereferencer.new(referenced_doc, @options).call
92
+ end
93
+ dereferenced_doc
39
94
  end
40
95
  end
41
96
  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.0"
2
+ VERSION = "0.1.5"
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.0
4
+ version: 0.1.5
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-15 00:00:00.000000000 Z
11
+ date: 2020-10-04 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