json_refs 0.1.1 → 0.1.2

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
2
  SHA1:
3
- metadata.gz: e623d13bcd51a7a1e254203e15c508065467469b
4
- data.tar.gz: 3a3ad67d1afd4df069fa1f4810a0a8ed20a03232
3
+ metadata.gz: b4f17701df4b9c089a11c31f6bde308873931802
4
+ data.tar.gz: 6f581341bdf53bf52e6b27ce88a774a805ad8e66
5
5
  SHA512:
6
- metadata.gz: 582f64c455143f262e407f97aabf3e4223ff7d59dab9c0d5a0ff6fa2ff874b287a063a583845e3cf77749565412dc881dbfd57e0a24346dcdc6cef39023fa48d
7
- data.tar.gz: d23cc3d915cadca4a3b1b70c25cd2d63fc80ddd043e4e27d66eb73d676916805f57b86f9cc8eaeec707b9dfd47569ebc1992851f1c509c86b0921d9e2e333100
6
+ metadata.gz: 4f29bdc8161c0a125a6875e3171cbaa6519c57027eeb28ab4a6be3f22fe7374917b9e0ce2fb323c56b8e13bdcd0509f36e1373425d3e9a98233df638fb70ccbd
7
+ data.tar.gz: cfc0d80386cde5a632ca668011c94aefcebca2e62fe81c355533a0584199ef35e48111ec866e4e289a22a7ebbe08d4deec44850852548cb4ee9d319d8524d706
@@ -1,5 +1,7 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - jruby-head
4
+ - 2.1.10
5
+ - 2.2.8
6
+ - 2.3.5
5
7
  - 2.4.2
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.
@@ -1,7 +1,5 @@
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
5
  def self.call(doc)
@@ -41,15 +39,8 @@ module JsonRefs
41
39
  end
42
40
 
43
41
  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)
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
@@ -1,3 +1,3 @@
1
1
  module JsonRefs
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
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.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-18 00:00:00.000000000 Z
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: