json_schema-artesano 0.0.1.beta.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f64ae69feb786b7aebba6d914cfabe466ab8951d
4
+ data.tar.gz: 6664232be5861daf3e8884420c3b3afdcf1631a8
5
+ SHA512:
6
+ metadata.gz: 5edbf1326b78a52761bbdac3e61500c6f37faa605f08b21188fa93dd4f850b9aa4d6653c04cb3d2d58066dbe037879214b2659639dfa51fc08e768b0c0988e7c
7
+ data.tar.gz: fd2b26a4e91e2ef3f3f5b11139d9d6dc25438ca7e00509eaa441d2ae1e785d635efb12790e46f552bc1dab61142eecb8b330f55e99c0b8354e7aa44accd1e197
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.15.4
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## [Unreleased]
5
+ ## [0.0.1.beta.1] - 2017-10-09 (Alfonso Mancilla)
6
+ ### Changed
7
+ - Process oneOf, anyOf and allOf first, then primivites, object and array.
8
+ ### Fixed
9
+ - Minor speel / grammar improvements to README.
10
+
11
+ ## [0.0.1] - 2017-09-17 (Alfonso Mancilla)
12
+ ### Added
13
+ - Proof of concept strategies: Null, DataType and Static.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Alfonso Mancilla
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # JsonSchema::Artesano
2
+
3
+ If you give a **sketch, materials and tools** to an artesano (artisan), they will use the tools to transform the materials into a product that (hopefully) matches the sketch.
4
+
5
+ Similary, if you give a **JSON Schema (sketch) and a strategy (tool)** to `JsonSchema::Artesano`, it will give you data (or objects, or whatever else you ask for) conformed to the schema.
6
+
7
+ **Mmm I don't believe you. You didn't give it materials!?**
8
+ > Because it has it owns materials! (thanks to the `json_schema` gem).
9
+
10
+ ### **AND HOW COULD THIS BE USEFUL?**
11
+ I came up with this after looking for a ruby 'JSON Schema faker' (give it a JSON Schema, get fake data according to the schema) and not finding any that fullfilled my needs. I know this gem is not what I was looking for, but now I can just create a simple faker strategy, pass it to `JsonSchema::Artesano` and get what I originally wanted. Someone could also create an strategy to generate ruby objects instead or similar.
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ # the sketch
17
+ schema = {
18
+ "title": "Person",
19
+ "type": "object",
20
+ "properties": {
21
+ "firstName": { "type": "string" },
22
+ "lastName": { "type": "string" },
23
+ "age": {
24
+ "description": "Age in years",
25
+ "type": "integer",
26
+ "minimum": 0
27
+ },
28
+ "genre": {
29
+ "type": "string",
30
+ "enum": ["male", "female"]
31
+ },
32
+ "married": { "type": "boolean" }
33
+ },
34
+ "required": ["firstName", "lastName"]
35
+ }
36
+
37
+ # the tool
38
+ require 'json_schema-artesano/json_schema/artesano/tools/static'
39
+
40
+ # the product
41
+ product = JsonSchema::Artesano.mold(sketch: sketch, tool: JsonSchema::Artesano::Tools::Static)
42
+
43
+ JSON.pretty_generate(product)
44
+
45
+ # =>
46
+ #
47
+ # {
48
+ # "firstName": "Lorem ipsum dolor sit amet",
49
+ # "lastName": "Lorem ipsum dolor sit amet",
50
+ # "age": 22,
51
+ # "genre": "male",
52
+ # "married": false
53
+ # }
54
+ #
55
+
56
+ ```
57
+
58
+ ## Artesano Tools (Strategies)
59
+
60
+ TODO: Describe how to create strategies and the provided ones.
61
+
62
+ ## Installation
63
+
64
+ Add this line to your application's Gemfile:
65
+
66
+ ```ruby
67
+ gem 'json_schema-artesano'
68
+ ```
69
+
70
+ And then execute:
71
+
72
+ $ bundle
73
+
74
+ Or install it yourself as:
75
+
76
+ $ gem install json_schema-artesano
77
+
78
+ ## Development
79
+
80
+ 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.
81
+
82
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ammancilla/json_schema-artesano.
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'json_schema/artesano'
5
+
6
+ require 'irb'
7
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'json_schema/artesano/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'json_schema-artesano'
9
+ spec.version = JsonSchema::Artesano::VERSION
10
+ spec.authors = ["Alfonso Mancilla"]
11
+ spec.email = ["almancill@gmail.com"]
12
+
13
+ spec.summary = %q{Json Schema data generator}
14
+ spec.description = %q{Generate data (objects, or anything else you like) from a given JSON Schema}
15
+ spec.homepage = ''
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_runtime_dependency 'json_schema', '~> 0.17'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.15'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ end
@@ -0,0 +1,88 @@
1
+ #
2
+ # Give it a sketch (JSON Schema), a Tool (strategy), and it
3
+ # will *mold* a product (hopefully) conformmatches the sketch.
4
+ #
5
+
6
+ require 'json'
7
+ require 'json_schema'
8
+
9
+ require_relative 'tools/static'
10
+
11
+ module JsonSchema
12
+ module Artesano
13
+ class Hand
14
+ PRIMITIVE_MATERIALS = %w{null boolean number string integer}.freeze
15
+
16
+ attr_reader :tool
17
+
18
+ def initialize(tool: Tools::Static)
19
+ self.use_tool(tool)
20
+ end
21
+
22
+ def use_tool(tool)
23
+ @tool = tool.new
24
+ end
25
+
26
+ def mold(sketch)
27
+ transform( material(sketch) )
28
+ end
29
+
30
+ private
31
+
32
+ def transform(material)
33
+ # Enum
34
+ if !material.enum.nil?
35
+ return tool.shape_enum(material)
36
+ end
37
+
38
+ # OneOf
39
+ if material.one_of.any?
40
+ item = tool.select_oneof( material.one_of )
41
+ return transform(item)
42
+ end
43
+
44
+ # AnyOf
45
+ if material.any_of.any?
46
+ items = tool.select_anyof( material.any_of )
47
+ items = [items] unless items.is_a?(Array)
48
+
49
+ return tool.shape_array( transform_array(items) )
50
+ end
51
+
52
+ # AllOf
53
+ if material.all_of.any?
54
+ array = material.all_of
55
+ return tool.shape_array( transform_array(array) )
56
+ end
57
+
58
+ case material.type[0]
59
+ when *PRIMITIVE_MATERIALS
60
+ tool.shape_primitive(material)
61
+ when 'object'
62
+ tool.shape_object( transform_object(material) )
63
+ when 'array'
64
+ tool.shape_array( transform_array(material) )
65
+ else
66
+ # Null
67
+ end
68
+ end
69
+
70
+ def transform_object(object)
71
+ object.properties.inject(Hash.new) do |result, property|
72
+ result.merge(
73
+ { property[0] => transform( property[1] ) }
74
+ )
75
+ end
76
+ end
77
+
78
+ def transform_array(array)
79
+ items = array.respond_to?(:items) ? [array.items] : array
80
+ items.flatten.map { |item| transform(item) }
81
+ end
82
+
83
+ def material(sketch)
84
+ JsonSchema.parse!(sketch).tap { |schema| schema.expand_references! }
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,42 @@
1
+ #
2
+ # The DataType strategy uses the data type of any primitive or enum property as the generated data.
3
+ #
4
+ # IMPORTANT:
5
+ # Generates valid data agains a given schema? NO!
6
+ #
7
+
8
+ module JsonSchema
9
+ module Artesano
10
+ module Tools
11
+ class DataType
12
+ def initialize
13
+ end
14
+
15
+ def shape_object(material)
16
+ material
17
+ end
18
+
19
+ def shape_array(material)
20
+ material
21
+ end
22
+
23
+ def shape_primitive(material)
24
+ material.type[0]
25
+ end
26
+
27
+ def shape_enum(material)
28
+ enum_type = material.type[0]
29
+ enum_type.nil? ? 'enum' : "enum[#{enum_type}]"
30
+ end
31
+
32
+ def select_oneof(materials)
33
+ materials.sample
34
+ end
35
+
36
+ def select_anyof(materials)
37
+ materials.sample
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # The Null strategy uses 'nil' as the generated data for any primitive or enum property.
3
+ #
4
+ # IMPORTANT:
5
+ # Generates valid data agains a given schema? NO!
6
+ #
7
+
8
+ module JsonSchema
9
+ module Artesano
10
+ module Tools
11
+ class Null
12
+ def initialize
13
+ end
14
+
15
+ def shape_object(material)
16
+ material
17
+ end
18
+
19
+ def shape_array(materials)
20
+ materials
21
+ end
22
+
23
+ def shape_primitive(material)
24
+ nil
25
+ end
26
+
27
+ def shape_enum(material)
28
+ nil
29
+ end
30
+
31
+ def select_oneof(materials)
32
+ materials.sample
33
+ end
34
+
35
+ def select_anyof(materials)
36
+ materials.sample
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,50 @@
1
+ #
2
+ # The Static strategy generates static values according to the data type of any primitive or enum property.
3
+ #
4
+ # IMPORTANT:
5
+ # Generates valid data agains a given schema? NO!
6
+ #
7
+
8
+ module JsonSchema
9
+ module Artesano
10
+ module Tools
11
+ class Static
12
+ def initialize
13
+ end
14
+
15
+ def shape_object(material)
16
+ material
17
+ end
18
+
19
+ def shape_array(material)
20
+ material
21
+ end
22
+
23
+ def shape_primitive(material)
24
+ case material.type[0]
25
+ when 'boolean'
26
+ false
27
+ when 'integer'
28
+ 22
29
+ when 'number'
30
+ 22.15
31
+ when 'string'
32
+ 'Lorem ipsum dolor sit amet'
33
+ end
34
+ end
35
+
36
+ def shape_enum(material)
37
+ material.enum[0]
38
+ end
39
+
40
+ def select_oneof(materials)
41
+ materials[0]
42
+ end
43
+
44
+ def select_anyof(materials)
45
+ materials[0]
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ module JsonSchema
2
+ module Artesano
3
+ VERSION = '0.0.1.beta.1'.freeze
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require_relative 'artesano/hand'
2
+ require_relative 'artesano/tools/static'
3
+
4
+ module JsonSchema
5
+ module Artesano
6
+ def self.mold(sketch:, tool: Tools::Static)
7
+ Hand.new(tool: tool).mold(sketch)
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_schema-artesano
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.beta.1
5
+ platform: ruby
6
+ authors:
7
+ - Alfonso Mancilla
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json_schema
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.17'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Generate data (objects, or anything else you like) from a given JSON
70
+ Schema
71
+ email:
72
+ - almancill@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CHANGELOG.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - json_schema-artesano.gemspec
88
+ - lib/json_schema/artesano.rb
89
+ - lib/json_schema/artesano/hand.rb
90
+ - lib/json_schema/artesano/tools/data_type.rb
91
+ - lib/json_schema/artesano/tools/null.rb
92
+ - lib/json_schema/artesano/tools/static.rb
93
+ - lib/json_schema/artesano/version.rb
94
+ homepage: ''
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">"
110
+ - !ruby/object:Gem::Version
111
+ version: 1.3.1
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.6.13
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Json Schema data generator
118
+ test_files: []