babl-json 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/lib/babl.rb +2 -2
  3. data/lib/babl/builder.rb +2 -0
  4. data/lib/babl/builder/chain_builder.rb +1 -2
  5. data/lib/babl/builder/template_base.rb +3 -4
  6. data/lib/babl/nodes.rb +15 -0
  7. data/lib/babl/nodes/create_pin.rb +1 -2
  8. data/lib/babl/nodes/dep.rb +1 -2
  9. data/lib/babl/nodes/each.rb +2 -2
  10. data/lib/babl/nodes/fixed_array.rb +2 -3
  11. data/lib/babl/nodes/goto_pin.rb +1 -2
  12. data/lib/babl/nodes/internal_value.rb +1 -1
  13. data/lib/babl/nodes/merge.rb +14 -35
  14. data/lib/babl/nodes/nav.rb +1 -1
  15. data/lib/babl/nodes/object.rb +5 -4
  16. data/lib/babl/nodes/parent.rb +1 -2
  17. data/lib/babl/nodes/static.rb +2 -3
  18. data/lib/babl/nodes/switch.rb +2 -3
  19. data/lib/babl/nodes/terminal_value.rb +17 -12
  20. data/lib/babl/nodes/typed.rb +2 -2
  21. data/lib/babl/nodes/with.rb +1 -2
  22. data/lib/babl/operators.rb +21 -0
  23. data/lib/babl/operators/array.rb +1 -2
  24. data/lib/babl/operators/dep.rb +1 -1
  25. data/lib/babl/operators/each.rb +1 -1
  26. data/lib/babl/operators/enter.rb +1 -0
  27. data/lib/babl/operators/merge.rb +1 -2
  28. data/lib/babl/operators/nav.rb +1 -1
  29. data/lib/babl/operators/object.rb +1 -2
  30. data/lib/babl/operators/parent.rb +1 -1
  31. data/lib/babl/operators/partial.rb +1 -1
  32. data/lib/babl/operators/pin.rb +2 -3
  33. data/lib/babl/operators/static.rb +3 -3
  34. data/lib/babl/operators/switch.rb +1 -3
  35. data/lib/babl/operators/typed.rb +1 -1
  36. data/lib/babl/operators/with.rb +1 -2
  37. data/lib/babl/rendering.rb +3 -0
  38. data/lib/babl/rendering/compiled_template.rb +2 -2
  39. data/lib/babl/schema.rb +7 -0
  40. data/lib/babl/schema/any_of.rb +43 -38
  41. data/lib/babl/schema/dyn_array.rb +1 -1
  42. data/lib/babl/schema/fixed_array.rb +1 -1
  43. data/lib/babl/schema/object.rb +1 -1
  44. data/lib/babl/schema/static.rb +1 -1
  45. data/lib/babl/schema/typed.rb +1 -1
  46. data/lib/babl/template.rb +2 -23
  47. data/lib/babl/utils.rb +3 -0
  48. data/lib/babl/version.rb +1 -1
  49. metadata +9 -63
  50. data/.gitignore +0 -3
  51. data/.rubocop.yml +0 -201
  52. data/.ruby-version +0 -1
  53. data/.travis.yml +0 -4
  54. data/CHANGELOG.md +0 -36
  55. data/Gemfile +0 -5
  56. data/LICENSE +0 -7
  57. data/README.md +0 -89
  58. data/babl.gemspec +0 -24
  59. data/logo-babl.png +0 -0
  60. data/spec/operators/array_spec.rb +0 -37
  61. data/spec/operators/call_spec.rb +0 -64
  62. data/spec/operators/continue_spec.rb +0 -25
  63. data/spec/operators/default_spec.rb +0 -15
  64. data/spec/operators/dep_spec.rb +0 -15
  65. data/spec/operators/each_spec.rb +0 -42
  66. data/spec/operators/enter_spec.rb +0 -28
  67. data/spec/operators/extends_spec.rb +0 -30
  68. data/spec/operators/merge_spec.rb +0 -124
  69. data/spec/operators/nav_spec.rb +0 -71
  70. data/spec/operators/null_spec.rb +0 -15
  71. data/spec/operators/nullable_spec.rb +0 -29
  72. data/spec/operators/object_spec.rb +0 -30
  73. data/spec/operators/parent_spec.rb +0 -50
  74. data/spec/operators/partial_spec.rb +0 -36
  75. data/spec/operators/pin_spec.rb +0 -172
  76. data/spec/operators/source_spec.rb +0 -33
  77. data/spec/operators/static_spec.rb +0 -27
  78. data/spec/operators/switch_spec.rb +0 -151
  79. data/spec/operators/typed_spec.rb +0 -84
  80. data/spec/operators/with_spec.rb +0 -31
  81. data/spec/spec_helper.rb +0 -2
  82. data/spec/spec_helper/operator_testing.rb +0 -46
  83. data/spec/spec_helper/schema_utils.rb +0 -53
  84. data/spec/utils/value_spec.rb +0 -54
@@ -1 +0,0 @@
1
- 2.4.1
@@ -1,4 +0,0 @@
1
- language: ruby
2
- install:
3
- - bundle install
4
- script: bundle exec rspec spec && bundle exec rubocop
@@ -1,36 +0,0 @@
1
- # BABL Changelog
2
-
3
- ## 0.2.3 (July 21, 2017)
4
- - Try to simplify JSON-Schema when types are specified.
5
- - Cut out dependency on [Values](https://github.com/tcrayford/Values).
6
-
7
- ## 0.2.2 (July 13, 2017)
8
-
9
- - Added four typing operators: `integer`, `number`, `string` and `boolean`.
10
-
11
- ## 0.2.1 (July 11, 2017)
12
-
13
- - Fix: array serialization is broken.
14
-
15
- ## 0.2.0 (July 11, 2017)
16
-
17
- - Added support for producing documentation using [JSON-Schema](http://json-schema.org/).
18
- - Improved some error messages occurring during rendering phase.
19
- - Stricter validation of templates passed to the `merge` operator. Compilation will fail if at least one of them is not producing object.
20
-
21
- ## 0.1.4 (July 6, 2017)
22
-
23
- - Added operator `extends`, to simplify the common usage pattern `merge(partial(...), ...)`.
24
-
25
- ## 0.1.3 (June 19, 2017)
26
-
27
- - Added operator `null`, in order to make any JSON file a valid BABL template.
28
-
29
- ## 0.1.2 (May 22, 2017)
30
-
31
- - Fixed a bug in `call(primitive)`.
32
- - Improved test suite.
33
-
34
- ## 0.1.1 (May 16, 2017)
35
-
36
- - First released version.
data/Gemfile DELETED
@@ -1,5 +0,0 @@
1
- ruby File.open('./.ruby-version').readline.split("\n").first
2
-
3
- source 'https://rubygems.org' do
4
- gemspec
5
- end
data/LICENSE DELETED
@@ -1,7 +0,0 @@
1
- Copyright 2017 Frederic Terrazzoni
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
-
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
-
7
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md DELETED
@@ -1,89 +0,0 @@
1
- ![BABL Logo](https://github.com/getbannerman/babl/raw/master/logo-babl.png)
2
-
3
- [![Build Status](https://travis-ci.org/getbannerman/babl.svg?branch=master)](https://travis-ci.org/getbannerman/babl)
4
- [![Gem](https://img.shields.io/gem/v/babl-json.svg)](https://rubygems.org/gems/babl-json)
5
- [![Downloads](https://img.shields.io/gem/dt/babl-json.svg)](https://rubygems.org/gems/babl-json)
6
-
7
- BABL (Bannerman API Builder Language) is a templating langage for generating JSON in APIs.
8
-
9
- It plays a role similar to [RABL](https://github.com/nesquena/rabl), [JBuilder](https://github.com/rails/jbuilder), [Grape Entity](https://github.com/ruby-grape/grape-entity), [AMS](https://github.com/rails-api/active_model_serializers), and many others. However, unlike existing tools, BABL has several advantages.
10
-
11
-
12
- ### Static compilation
13
-
14
- BABL is a simple Ruby DSL. Unlike RABL, the template code is fully parsed and executed before any data is available. This approach makes it possible to detect errors earlier and document the output schema automatically, without data. Experimentally, it also makes partials evaluation much faster, because partials are loaded only once.
15
-
16
- ### Automatic preloading
17
-
18
- BABL is also able to infer the "input schema". It can loosely be seen as the list of properties we need to read from the models to construct the JSON output. These *dependencies* can be used to retrieve data more efficiently. For instance, all ActiveRecord associations can be preloaded automatically using this mechanism.
19
-
20
- ### Simple syntax
21
-
22
- JSON is simple, and generating JSON should be as simple as possible.
23
-
24
- BABL template:
25
-
26
- ```ruby
27
- object(
28
- document: object(
29
- :id, :title,
30
-
31
- owner: _.nullable.object(:id, :name),
32
- authors: _.each.object(:id, :name),
33
- category: 'Not implemented'
34
- )
35
- )
36
- ```
37
-
38
- Output:
39
-
40
- ```json
41
- {
42
- "document": {
43
- "id": 1,
44
- "title": "Hello BABL",
45
- "owner": null,
46
- "authors": [
47
- { "id": 4, "name": "Fred" },
48
- { "id": 5, "name": "Vivien" }
49
- ],
50
- "category": "not implemented"
51
- }
52
- }
53
- ```
54
-
55
- Interestingly, this JSON output is also a valid BABL template. This property makes it very easy to mix static JSON and dynamic content during developpement.
56
-
57
- ## Documentation
58
-
59
- Not yet available.
60
-
61
- ## Current limitations
62
-
63
- ### Automatic preloading
64
-
65
- This feature only works if BABL is configured to use an external preloader.
66
-
67
- As of today, the only compatible preloader implementation *has not been released* yet, because it has severe limitations. Hopefully, it will be available soon :-)
68
-
69
- ### Automatic documentation
70
-
71
- The structure of the JSON produced by a BABL template can be documented using [JSON-Schema](http://json-schema.org/).
72
-
73
- ### Rails integration
74
-
75
- This gem implements support of `*.babl` views in [Rails](https://github.com/rails/rails/).
76
-
77
- In theory, the template could be compliled once for all and re-used for subsequent requests. In practice, today's implementation will re-compile the template at every request, because Rails templating mechanism doesn't make our life easy.
78
-
79
- If it turns out to be a performance bottleneck, we will try to work around this issue.
80
-
81
- ### Recursion
82
-
83
- BABL does not support recursive templates. The first reason is that it makes dependency tracking more complicated (especially on preloader side). The other reason is that it is not as useful as it might seem.
84
-
85
- ## License
86
-
87
- Copyright (c) 2017 [Bannerman](https://www.bannerman.com/), [Frederic Terrazzoni](https://github.com/fterrazzoni)
88
-
89
- Licensed under the [MIT license](https://opensource.org/licenses/MIT).
@@ -1,24 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'lib/babl/version')
2
-
3
- Gem::Specification.new do |gem|
4
- gem.name = "babl-json"
5
- gem.version = Babl::VERSION
6
- gem.licenses = ['MIT']
7
- gem.authors = ['Frederic Terrazzoni']
8
- gem.email = ['frederic.terrazzoni@gmail.com']
9
- gem.description = "JSON templating on steroids"
10
- gem.summary = gem.description
11
- gem.homepage = 'https://github.com/getbannerman/babl'
12
-
13
- gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
14
- gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
15
- gem.test_files = gem.files.grep(%r{^spec/})
16
- gem.require_paths = ['lib']
17
-
18
- gem.add_development_dependency 'pry', '~> 0'
19
- gem.add_development_dependency 'rspec', '~> 3'
20
- gem.add_development_dependency 'rubocop', '~> 0.48'
21
- gem.add_development_dependency 'json-schema', '~> 2.8'
22
-
23
- gem.add_dependency 'oj', '~> 3.0'
24
- end
Binary file
@@ -1,37 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Babl::Operators::Array do
4
- extend SpecHelper::OperatorTesting
5
-
6
- describe '#array' do
7
- context 'statically defined array' do
8
- template { array('coucou', 45, a: 1, b: [_]) }
9
-
10
- let(:object) { { b: 12 } }
11
-
12
- it { expect(json).to eq ['coucou', 45, { 'a' => 1, 'b' => [12] }] }
13
- it { expect(dependencies).to eq(b: {}) }
14
- it {
15
- expect(schema).to eq(
16
- s_fixed_array(
17
- s_static('coucou'),
18
- s_static(45),
19
- s_object(
20
- s_property(:a, s_static(1)),
21
- s_property(:b, s_fixed_array(s_anything))
22
- )
23
- )
24
- )
25
- }
26
- end
27
-
28
- context 'nullable array' do
29
- template { nullable.array(itself) }
30
-
31
- let(:object) { 1 }
32
-
33
- it { expect(json).to eq([1]) }
34
- it { expect(schema).to eq s_any_of(s_null, s_fixed_array(s_anything)) }
35
- end
36
- end
37
- end
@@ -1,64 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Babl::Operators::Call do
4
- extend SpecHelper::OperatorTesting
5
-
6
- describe '#call' do
7
- context 'primitive' do
8
- template { call(false) }
9
-
10
- it { expect(json).to eq false }
11
- it { expect(dependencies).to eq({}) }
12
- it { expect(schema).to eq s_static(false) }
13
-
14
- context 'call primitive after a conditional' do
15
- template { nullable.call(34) }
16
-
17
- it { expect(json).to eq nil }
18
- end
19
- end
20
-
21
- context 'block' do
22
- template { call { self * 2 } }
23
-
24
- let(:object) { 2 }
25
-
26
- it { expect(json).to eq 4 }
27
- end
28
-
29
- context 'hash' do
30
- template { call('a' => 1, b: 2) }
31
-
32
- it { expect(json).to eq('a' => 1, 'b' => 2) }
33
- it { expect(dependencies).to eq({}) }
34
- end
35
-
36
- context 'array' do
37
- template { call(['a', 2, :b]) }
38
-
39
- let(:object) { { b: 42 } }
40
-
41
- it { expect(json).to eq(['a', 2, 42]) }
42
- it { expect(dependencies).to eq(b: {}) }
43
- end
44
-
45
- context 'block' do
46
- template { call(:lol) }
47
-
48
- let(:object) { OpenStruct.new(lol: 'tam') }
49
-
50
- it { expect(json).to eq 'tam' }
51
- it { expect(dependencies).to eq(lol: {}) }
52
- end
53
-
54
- context 'template' do
55
- template { object(coucou: nav(:a).call(nav(:b))) }
56
-
57
- let(:object) { OpenStruct.new(a: OpenStruct.new(b: 1)) }
58
-
59
- it { expect(dependencies).to eq(a: { b: {} }) }
60
- it { expect(json).to eq('coucou' => 1) }
61
- it { expect(schema).to eq(s_object(s_property(:coucou, s_anything))) }
62
- end
63
- end
64
- end
@@ -1,25 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Babl::Operators::Continue do
4
- extend SpecHelper::OperatorTesting
5
-
6
- describe '#switch' do
7
- context 'navigation before continue' do
8
- template { nav(:abc).switch(false => 1, default => nav(:lol).continue).object(val: nav(:ok)) }
9
-
10
- it { expect { compiled }.to raise_error Babl::Errors::InvalidTemplate }
11
- end
12
-
13
- context 'continue without switch' do
14
- template { continue }
15
-
16
- it { expect { compiled }.to raise_error Babl::Errors::InvalidTemplate }
17
- end
18
-
19
- context 'continue in sub-object' do
20
- template { object(a: switch(default => object(x: continue))) }
21
-
22
- it { expect { compiled }.to raise_error Babl::Errors::InvalidTemplate }
23
- end
24
- end
25
- end
@@ -1,15 +0,0 @@
1
- require 'spec_helper'
2
-
3
- require 'spec_helper'
4
-
5
- describe Babl::Operators::Default do
6
- extend SpecHelper::OperatorTesting
7
-
8
- describe '#default' do
9
- template { default }
10
-
11
- it { expect(json).to eq true }
12
- it { expect(schema).to eq s_static(true) }
13
- it { expect(dependencies).to eq({}) }
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Babl::Operators::Dep do
4
- extend SpecHelper::OperatorTesting
5
-
6
- describe '#dep' do
7
- template { dep(a: [:b, :c]).nav(:b).dep(x: :y).nav(:z) }
8
-
9
- let(:object) { { b: { z: 42 } } }
10
-
11
- it { expect(schema).to eq s_anything }
12
- it { expect(dependencies).to eq(a: { b: {}, c: {} }, b: { x: { y: {} }, z: {} }) }
13
- it { expect(json).to eq(42) }
14
- end
15
- end
@@ -1,42 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Babl::Operators::Each do
4
- extend SpecHelper::OperatorTesting
5
-
6
- describe '#each' do
7
- context 'when everything is fine' do
8
- template { each.nav(:a) }
9
-
10
- let(:object) { [{ a: 3 }, { a: 2 }, { a: 1 }] }
11
-
12
- it { expect(json).to eq [3, 2, 1] }
13
- it { expect(dependencies).to eq(__each__: { a: {} }) }
14
- it { expect(schema).to eq s_dyn_array(s_anything) }
15
- end
16
-
17
- context 'error while navigating' do
18
- template { nav(:box).each.nav(:trololol) }
19
-
20
- let(:object) { { box: [{ trololol: 2 }, 42] } }
21
-
22
- it { expect { json }.to raise_error(/\__root__\.box\.1\.trololol/) }
23
- end
24
-
25
- context 'not enumerable' do
26
- template { nav(:lol).each }
27
-
28
- let(:object) { { lol: 'not enumerable' } }
29
-
30
- it { expect { json }.to raise_error(Babl::Errors::RenderingError, /\__root__\.lol/) }
31
- end
32
-
33
- context 'nullable array' do
34
- template { nullable.each.nullable.object }
35
-
36
- let(:object) { [1, nil] }
37
-
38
- it { expect(json).to eq [{}, nil] }
39
- it { expect(schema).to eq s_any_of(s_dyn_array(s_any_of(s_object, s_null)), s_null) }
40
- end
41
- end
42
- end
@@ -1,28 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Babl::Operators::Enter do
4
- extend SpecHelper::OperatorTesting
5
-
6
- describe '#enter' do
7
- context 'invalid usage' do
8
- template { enter }
9
-
10
- it { expect { compiled }.to raise_error Babl::Errors::InvalidTemplate }
11
- end
12
-
13
- context 'valid usage' do
14
- template { object(a: enter) }
15
-
16
- let(:object) { { a: 42 } }
17
-
18
- it { expect(schema).to eq(s_object(s_property(:a, s_anything))) }
19
- it { expect(dependencies).to eq(a: {}) }
20
- it { expect(json).to eq('a' => 42) }
21
-
22
- context 'using alias' do
23
- template { object(a: _) }
24
- it { expect(json).to eq('a' => 42) }
25
- end
26
- end
27
- end
28
- end
@@ -1,30 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Babl::Operators::Extends do
4
- extend SpecHelper::OperatorTesting
5
-
6
- let(:custom_lookup_context) {
7
- TestLookupContext.new(
8
- object_partial: TestLookupContext.new('object(a: 1, b: 3)'),
9
- string_partial: TestLookupContext.new('"lol"')
10
- )
11
- }
12
- let(:dsl) { Babl::Template.new.with_lookup_context(custom_lookup_context) }
13
- let(:object) { [nil] }
14
-
15
- context 'simple use case' do
16
- template { each.extends('object_partial', object(x: 1), b: 34) }
17
- it { expect(json).to eq(['a' => 1, 'x' => 1, 'b' => 34]) }
18
- end
19
-
20
- context 'extend a non-object' do
21
- template { extends('string_partial') }
22
- it { expect(json).to eq('lol') }
23
- it { expect(schema).to eq s_static('lol') }
24
- end
25
-
26
- context 'extend a non-object and try to add properties' do
27
- template { each.extends('string_partial', test: 1) }
28
- it { expect { schema }.to raise_error Babl::Errors::InvalidTemplate }
29
- end
30
- end