json-ref 0.1.0
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 +7 -0
- data/.autotest +8 -0
- data/.gitignore +3 -0
- data/.travis.yml +8 -0
- data/README.md +62 -0
- data/Rakefile +9 -0
- data/json-ref.gemspec +22 -0
- data/lib/json-ref.rb +31 -0
- data/test/helper.rb +3 -0
- data/test/test_json-ref.rb +24 -0
- metadata +69 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ec764bcb2049b661551c8b11b02739eed7cbccbb
|
4
|
+
data.tar.gz: cba8f213b0bc0b23203d57a9d8d59c83ef9547e4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d8c0507b4098b9fa1872750b0451cd52c7309f33bf38372a7cacd3ce197898c506f40587d6af4fad524132dcb226e3445b5bb9a8357d39153100446be22b323c
|
7
|
+
data.tar.gz: 74b96eb1e2a910141a03f168af12af1939552899d1ec140b446124ebee85acdc1a01f69f32a87a10daf53a95ea85f81fcd22c598d17539964604be94ad838aa3
|
data/.autotest
ADDED
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# JSON Ref
|
2
|
+
|
3
|
+
[](https://travis-ci.org/nmerouze/json-ref)
|
4
|
+
[](https://codeclimate.com/github/nmerouze/json-ref)
|
5
|
+
|
6
|
+
Partial implementation of [JSON Ref](http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-00) draft. The URI can only contain a fragment pointing to a value inside the document at this moment.
|
7
|
+
|
8
|
+
## Install
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'json-ref'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install json-ref
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
origin_doc = { 'title' => 'foobar', 'ref_title' => { '$ref' => '#/title' } }
|
26
|
+
JSONRef.new(origin_doc).expand
|
27
|
+
# =>
|
28
|
+
{ 'title' => 'foobar', 'ref_title' => 'foobar' }
|
29
|
+
```
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
Copyright (c) 2013 Nicolas Mérouze
|
42
|
+
|
43
|
+
MIT License
|
44
|
+
|
45
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
46
|
+
a copy of this software and associated documentation files (the
|
47
|
+
"Software"), to deal in the Software without restriction, including
|
48
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
49
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
50
|
+
permit persons to whom the Software is furnished to do so, subject to
|
51
|
+
the following conditions:
|
52
|
+
|
53
|
+
The above copyright notice and this permission notice shall be
|
54
|
+
included in all copies or substantial portions of the Software.
|
55
|
+
|
56
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
57
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
58
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
59
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
60
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
61
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
62
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/json-ref.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'json-ref'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "json-ref"
|
8
|
+
spec.version = JSONRef::VERSION
|
9
|
+
spec.authors = ["Nicolas Mérouze"]
|
10
|
+
spec.email = ["nicolas@merouze.me"]
|
11
|
+
spec.description = %q{An implementation of JSON Ref.}
|
12
|
+
spec.summary = %q{An implementation of JSON Ref.}
|
13
|
+
spec.homepage = "https://github.com/nmerouze/json-ref"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
end
|
data/lib/json-ref.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
class JSONRef
|
3
|
+
VERSION = '0.1.0'
|
4
|
+
|
5
|
+
def initialize(document)
|
6
|
+
@document = document
|
7
|
+
end
|
8
|
+
|
9
|
+
def expand
|
10
|
+
@document.each do |k,v|
|
11
|
+
if v.is_a?(Hash) && v.has_key?('$ref')
|
12
|
+
@document[k] = split_path(v['$ref']).inject(@document) { |doc, item| doc[item] }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.expand(document)
|
18
|
+
self.new(document).expand
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def split_path(path)
|
24
|
+
escape_characters = {'^/' => '/', '^^' => '^', '~0' => '~', '~1' => '/'}
|
25
|
+
return [''] if path == '#/'
|
26
|
+
|
27
|
+
path.sub(/^#\//, '').split(/(?<!\^)\//).map! { |part|
|
28
|
+
part.gsub!(/\^[\/^]|~[01]/) { |m| escape_characters[m] }; part
|
29
|
+
}
|
30
|
+
end
|
31
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestJSONRef < MiniTest::Unit::TestCase
|
4
|
+
def test_expand_string
|
5
|
+
origin_doc = { 'title' => 'foobar', 'ref_title' => { '$ref' => '#/title' } }
|
6
|
+
result_doc = { 'title' => 'foobar', 'ref_title' => 'foobar' }
|
7
|
+
|
8
|
+
assert_equal result_doc, JSONRef.new(origin_doc).expand
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_expand_hash
|
12
|
+
origin_doc = { 'title' => { 'value' => 'foobar' }, 'ref_title' => { '$ref' => '#/title' } }
|
13
|
+
result_doc = { 'title' => { 'value' => 'foobar' }, 'ref_title' => { 'value' => 'foobar' } }
|
14
|
+
|
15
|
+
assert_equal result_doc, JSONRef.new(origin_doc).expand
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_expand_complex_path
|
19
|
+
origin_doc = { 'nested' => { 'title' => 'foobar' }, 'ref_title' => { '$ref' => '#/nested/title' } }
|
20
|
+
result_doc = { 'nested' => { 'title' => 'foobar' }, 'ref_title' => 'foobar' }
|
21
|
+
|
22
|
+
assert_equal result_doc, JSONRef.new(origin_doc).expand
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json-ref
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicolas Mérouze
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: An implementation of JSON Ref.
|
28
|
+
email:
|
29
|
+
- nicolas@merouze.me
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- .autotest
|
35
|
+
- .gitignore
|
36
|
+
- .travis.yml
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- json-ref.gemspec
|
40
|
+
- lib/json-ref.rb
|
41
|
+
- test/helper.rb
|
42
|
+
- test/test_json-ref.rb
|
43
|
+
homepage: https://github.com/nmerouze/json-ref
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 2.0.2
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: An implementation of JSON Ref.
|
67
|
+
test_files:
|
68
|
+
- test/helper.rb
|
69
|
+
- test/test_json-ref.rb
|