jsonschema_serializer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 395b1b884c0ae4cb6a7482c829874658d73b4263
4
+ data.tar.gz: d336c0f04228e6e9d9eed216587186f15c26b677
5
+ SHA512:
6
+ metadata.gz: a3c34d1b59ba6ff2b94f266627bb78273a41fcab30f4f7e3f391f0ef51abcda8c8a74087de5fce94dc1830ccd396feabc8c0786b2fd2ed067d0e2c1e8e4fd775
7
+ data.tar.gz: 7b05406b0c4ff3ee5d73c1dcb64935c750e80b3bd8563fface0a5ce3d762cf542d39f66c31f302a2de97e9eb966b7dfd5448c7ba05e28e24ff84d49a888d328d
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,23 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+ Exclude:
4
+ - bin/*
5
+ - "*.gemspec"
6
+ - Gemfile
7
+ - Rakefile
8
+
9
+ Metrics/BlockLength:
10
+ Exclude:
11
+ - spec/**/*
12
+
13
+ Metrics/LineLength:
14
+ Max: 90
15
+
16
+ Style/Documentation:
17
+ Enabled: true
18
+
19
+ Style/FrozenStringLiteralComment:
20
+ Enabled: false
21
+
22
+ Style/SymbolArray:
23
+ EnforcedStyle: brackets
@@ -0,0 +1,40 @@
1
+ sudo: false
2
+
3
+ language: ruby
4
+
5
+ jobs:
6
+ allow_failures:
7
+ - rvm: 1.8.7
8
+ gemfile: Gemfile-pre-2
9
+ - rvm: 1.9.3
10
+ gemfile: Gemfile-pre-2
11
+ - rvm: 2.0.0
12
+ gemfile: Gemfile-pre-2
13
+ - rvm: jruby-1.7.21
14
+ gemfile: Gemfile-pre-2
15
+
16
+ include:
17
+ - rvm: 1.8.7
18
+ gemfile: Gemfile-pre-2
19
+ - rvm: 1.9.3
20
+ gemfile: Gemfile-pre-2
21
+ - rvm: 2.0.0
22
+ gemfile: Gemfile-pre-2
23
+ - rvm: jruby-1.7.21
24
+ gemfile: Gemfile-pre-2
25
+ - rvm: 2.1.5
26
+ gemfile: Gemfile
27
+ - rvm: 2.2.7
28
+ gemfile: Gemfile
29
+ - rvm: 2.3.4
30
+ gemfile: Gemfile
31
+ - rvm: 2.4.1
32
+ gemfile: Gemfile
33
+ - rvm: 2.5.0
34
+ gemfile: Gemfile
35
+ - rvm: ruby-head
36
+ gemfile: Gemfile
37
+ - rvm: jruby-9.0.5.0
38
+ gemfile: Gemfile
39
+
40
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in jsonschema_serializer.gemspec
6
+ gemspec
7
+
8
+ gem 'rubocop', '~> 0.55.0'
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # The purpose of this Gemfile is to test old ruby versions
4
+ gemspec
@@ -0,0 +1,87 @@
1
+ # JsonschemaSerializer
2
+
3
+ [![Build Status](https://travis-ci.org/mberlanda/jsonschema_serializer.svg?branch=master)](https://travis-ci.org/mberlanda/jsonschema_serializer)
4
+
5
+ This purpose of this gem is to generate [JsonSchema](http://json-schema.org/).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'jsonschema_serializer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install jsonschema_serializer
22
+
23
+ ## Usage
24
+
25
+ You can generate a schema as follows:
26
+
27
+ ```ruby
28
+ schema = JsonSchema::Builder.build do |b|
29
+
30
+ subscriber = b._object title: :subscriber, required: [:age] do |prop|
31
+ prop.merge! b.string :first_name, title: 'First Name'
32
+ prop.merge! b.string :last_name, title: 'Last Name'
33
+ prop.merge! b.integer :age, title: 'Age'
34
+ end
35
+
36
+ b.title "a title"
37
+ b.description "a description"
38
+ b.required :a, :b, :c
39
+ b.properties.tap do |p|
40
+ p.merge! b.string :a, description: "abc"
41
+ p.merge! b.array :subscribers, description: "subscribers", items: subscriber
42
+ end
43
+ end
44
+
45
+ schema.to_json
46
+ ```
47
+
48
+ Allowed parameters for data types:
49
+
50
+ - `array` : `:default, :description, items: {}||[{}], :minItems, :maxItems, :title`
51
+ - `boolean`: `:default, :description, :title`
52
+ - `integer`: `:default, :description, enum: [], :minimum, :maximum, :multipleOf, :title`
53
+ - `number` : `:default, :description, enum: [], :minimum, :maximum, :multipleOf, :title`
54
+ - `string` : `:default, :description, :format, :minLength, :title`
55
+
56
+ You can alternatively use an experimental builder for `ActiveRecord`
57
+
58
+
59
+ ```ruby
60
+ class Serializer
61
+ include JsonschemaSerializer::ActiveRecord
62
+ end
63
+
64
+ schema = Serializer.from_active_record(MyActiveRecordClass)
65
+ schema = Serializer.from_active_record(MyActiveRecordClass, only: %[desired1 desired2])
66
+ schema = Serializer.from_active_record(MyActiveRecordClass, except: %[ignored1 ignored2])
67
+
68
+ # You can manipulate the resulting schema
69
+
70
+ schema.tap do |s|
71
+ s.title "a title"
72
+ s.description "a description"
73
+ end
74
+
75
+ schema.to_json
76
+ ```
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/mberlanda/jsonschema_serializer.
87
+ ygy
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jsonschema_serializer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -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,48 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "jsonschema_serializer/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jsonschema_serializer"
8
+ spec.version = JsonschemaSerializer::VERSION
9
+ spec.authors = ["Mauro Berlanda"]
10
+ spec.email = ["mauro.berlanda@gmail.com"]
11
+
12
+ spec.summary = %q{The purpose of this gem is to generate JsonSchema}
13
+ spec.description = <<-EOT
14
+ The goal behind this gem is to generate JsonSchema.
15
+ This can be done using thanks to a Builder class or
16
+ an ActiveRecord serialization module.
17
+ EOT
18
+ spec.homepage = "https://github.com/mberlanda/jsonschema_serializer"
19
+ spec.licenses = ['MIT']
20
+
21
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
22
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
23
+ spec.metadata = { "source_code_uri" => "https://github.com/mberlanda/jsonschema_serialize" }
24
+ # if spec.respond_to?(:metadata)
25
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
26
+ # else
27
+ # raise "RubyGems 2.0 or newer is required to protect against " \
28
+ # "public gem pushes."
29
+ # end
30
+
31
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
32
+ f.match(%r{^(test|spec|features)/})
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "bundler", "~> 1.16"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+
42
+ spec.extra_rdoc_files = ['README.md']
43
+ spec.rdoc_options << '--title' << 'Jsonschema Serializer' <<
44
+ '--main' << 'README.md' <<
45
+ '--line-numbers'
46
+
47
+ spec.required_ruby_version = '>= 2.1.0'
48
+ end
@@ -0,0 +1,7 @@
1
+ require 'jsonschema_serializer/version'
2
+ require 'jsonschema_serializer/builder'
3
+ require 'jsonschema_serializer/active_record'
4
+
5
+ # :no-rdoc:
6
+ module JsonschemaSerializer
7
+ end
@@ -0,0 +1,74 @@
1
+ require_relative 'builder'
2
+
3
+ # The +JsonschemaSerializer::Activerecord+ module provides
4
+ # a +from_activerecord+ class method to serialize some
5
+ # ActiveRecord classes with the minimum effort
6
+
7
+ module JsonschemaSerializer
8
+ # :no-rdoc:
9
+ module ActiveRecord
10
+ # :no-rdoc:
11
+ def self.included(klass)
12
+ klass.extend(ClassMethods)
13
+ end
14
+
15
+ # :no-rdoc:
16
+ module ClassMethods
17
+ # Serialize an ActiveRecord class into a
18
+ # JsonschemaSerializer::Builder object
19
+ #
20
+ # params:
21
+ # +klass+ [ActiveRecord::Base]
22
+ # +only+ [Array[String]]
23
+ # +except+ [Array[String]]
24
+ def from_active_record(klass, only: nil, except: nil)
25
+ validate_arguments(only, except)
26
+ JsonschemaSerializer::Builder.build do |b|
27
+ selected_columns(klass, only, except).each do |col|
28
+ b.properties.tap do |prop|
29
+ el = format_column_element(col)
30
+ # Handle basic case of attribute type and attribute name
31
+ prop.merge! b.send(el[:type], el[:name])
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ # Raise if +only+ and +except+ are both provided
40
+ def validate_arguments(only, except)
41
+ raise ArgumentError, 'only and except options both provided' if only && except
42
+ end
43
+
44
+ # Retrieves the columns and keep/discard some elements if needed
45
+ def selected_columns(klass, only, except)
46
+ klass.columns.tap do |cols|
47
+ cols.select! { |col| only.include?(col.name) } if only
48
+ cols.reject! { |col| except.include?(col.name) } if except
49
+ end
50
+ end
51
+
52
+ # Mapping Ruby types on Jsonschema types.
53
+ # This could be moved to a separate module later
54
+
55
+ TYPE_CONVERSIONS = {
56
+ boolean: :boolean,
57
+ datetime: :string,
58
+ decimal: :number,
59
+ float: :number,
60
+ integer: :integer,
61
+ text: :string
62
+ }.freeze
63
+
64
+ # Format a ActiveRecord::ConnectionAdapters::<Adapter>::Column as an Hash
65
+ def format_column_element(col)
66
+ {}.tap do |h|
67
+ h[:name] = col.name
68
+ h[:type] = TYPE_CONVERSIONS[col.type] || :string
69
+ # col.default.tap { |d| h[:default] = d if d}
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,67 @@
1
+ require 'json'
2
+
3
+ module JsonschemaSerializer
4
+ # :no-rdoc:
5
+ class Builder
6
+ class << self
7
+ def build
8
+ new.tap do |builder|
9
+ yield(builder) if block_given?
10
+ end
11
+ end
12
+ end
13
+
14
+ attr_reader :schema
15
+
16
+ def initialize
17
+ @schema ||= {
18
+ type: :object,
19
+ properties: {}
20
+ }
21
+ end
22
+
23
+ def to_json
24
+ @schema.to_json
25
+ end
26
+
27
+ def title(title)
28
+ @schema[:title] = title
29
+ end
30
+
31
+ def description(description)
32
+ @schema[:description] = description
33
+ end
34
+
35
+ def required(*required)
36
+ @schema[:required] = required
37
+ end
38
+
39
+ def properties
40
+ @schema[:properties] ||= {}
41
+ end
42
+
43
+ def _object(**opts)
44
+ { type: :object, properties: {} }.merge(opts).tap do |h|
45
+ yield(h[:properties]) if block_given?
46
+ end
47
+ end
48
+
49
+ [:boolean, :integer, :number, :string].each do |attr_type|
50
+ define_method("_#{attr_type}") do |**opts|
51
+ { type: attr_type }.merge(opts)
52
+ end
53
+
54
+ define_method(attr_type) do |name, **opts|
55
+ {
56
+ name => send("_#{attr_type}", **opts)
57
+ }
58
+ end
59
+ end
60
+
61
+ def array(name, items:, **opts)
62
+ {
63
+ name => { type: :array, items: items }.merge(opts)
64
+ }
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,4 @@
1
+ # :no-rdoc:
2
+ module JsonschemaSerializer
3
+ VERSION = '0.0.1'.freeze
4
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonschema_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mauro Berlanda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: |2
56
+ The goal behind this gem is to generate JsonSchema.
57
+ This can be done using thanks to a Builder class or
58
+ an ActiveRecord serialization module.
59
+ email:
60
+ - mauro.berlanda@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files:
64
+ - README.md
65
+ files:
66
+ - ".gitignore"
67
+ - ".rspec"
68
+ - ".rubocop.yml"
69
+ - ".travis.yml"
70
+ - Gemfile
71
+ - Gemfile-pre-2
72
+ - README.md
73
+ - Rakefile
74
+ - bin/console
75
+ - bin/setup
76
+ - jsonschema_serializer.gemspec
77
+ - lib/jsonschema_serializer.rb
78
+ - lib/jsonschema_serializer/active_record.rb
79
+ - lib/jsonschema_serializer/builder.rb
80
+ - lib/jsonschema_serializer/version.rb
81
+ homepage: https://github.com/mberlanda/jsonschema_serializer
82
+ licenses:
83
+ - MIT
84
+ metadata:
85
+ source_code_uri: https://github.com/mberlanda/jsonschema_serialize
86
+ post_install_message:
87
+ rdoc_options:
88
+ - "--title"
89
+ - Jsonschema Serializer
90
+ - "--main"
91
+ - README.md
92
+ - "--line-numbers"
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 2.1.0
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.6.8
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: The purpose of this gem is to generate JsonSchema
111
+ test_files: []