json_structure 0.0.2

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: 7743f55d019b53d39543aede96c77ab327a2fe4a
4
+ data.tar.gz: 91f6d05f464028a7f8b3183db2b236b15033968b
5
+ SHA512:
6
+ metadata.gz: 622248b21a9759681fae67b8d6000bd28c77140e93a0ac2d83a1c9b717dc8b1fd2c93b465d74bb51fbd138002b8a1c5ebaae6c037185430b70966695db6aa515
7
+ data.tar.gz: 570fbf46a0c804daf3603ad4b322b231764020157e86a8d12fadeecd9e4ad758e63015966d87fd6ae0456bbfe27f77ababafe160d17a170afeab734aa5bc15eb
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json_structure.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kaido Iwamoto
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,60 @@
1
+ # JsonStructure
2
+
3
+ ```ruby
4
+ user = JsonStructure.build do
5
+ object({
6
+ id: integer,
7
+ username: string(max_length: 30),
8
+ address: string | null,
9
+ age: integer(min: 0),
10
+ favorite_things: array(string),
11
+ })
12
+ end
13
+
14
+ user === {
15
+ 'id' => 13,
16
+ 'username' => 'Foo Bar',
17
+ 'address' => nil,
18
+ 'age' => 10,
19
+ 'favorite_things' => ['music', 'succer'],
20
+ } # => true
21
+
22
+ user === {
23
+ 'id' => '9e0d23a',
24
+ 'username' => nil,
25
+ 'address' => ''
26
+ 'age': -1,
27
+ } # => false
28
+ ```
29
+
30
+ ```ruby
31
+ products_array = JsonStructure.build do
32
+ array(
33
+ object({
34
+ id: integer,
35
+ name: string,
36
+ price: integer(min: 0),
37
+ maker: object({
38
+ id: integer,
39
+ name: string,
40
+ }) | null,
41
+ })
42
+ )
43
+ end
44
+
45
+ products_array === [
46
+ { 'id' => 22,
47
+ 'name' => 'pencil',
48
+ 'price' => 98,
49
+ 'maker' => {
50
+ 'id' => 32,
51
+ 'name' => 'Maker A',
52
+ },
53
+ },
54
+ {
55
+ 'id' => 43,
56
+ 'name' => 'rular',
57
+ 'price' => 128,
58
+ },
59
+ ] # => true
60
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "json_structure"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json_structure/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json_structure"
8
+ spec.version = JsonStructure::VERSION
9
+ spec.authors = ["Kaido Iwamoto"]
10
+ spec.email = ["h6824.kaido@gmail.com"]
11
+
12
+ if spec.respond_to?(:metadata)
13
+ end
14
+
15
+ spec.summary = %q{JSON structure checker}
16
+ spec.description = %q{}
17
+ spec.homepage = "http://github.com/odiak/json_structure"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.8"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,15 @@
1
+ module JsonStructure
2
+ class AnyOf < Type
3
+ def initialize(*types)
4
+ @types = types
5
+ end
6
+
7
+ def ===(value)
8
+ @types.any? { |type| type === value }
9
+ end
10
+
11
+ def |(type)
12
+ AnyOf.new(*(@types + [type]))
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module JsonStructure
2
+ class Array < Type
3
+ def initialize(elem_type = nil)
4
+ @elem_type = elem_type
5
+ end
6
+
7
+ def ===(value)
8
+ return false unless value.is_a?(::Array)
9
+
10
+ if @elem_type
11
+ return value.all? { |v| @elem_type === v }
12
+ end
13
+
14
+ true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ module JsonStructure
2
+ class Null < Type
3
+ def ===(value)
4
+ value.nil?
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,30 @@
1
+ module JsonStructure
2
+ class Number < Type
3
+ def initialize(min: nil, max: nil)
4
+ @min = min
5
+ @max = max
6
+ end
7
+
8
+ def ===(value)
9
+ return false unless value.is_a?(::Integer) || value.is_a?(::Float)
10
+
11
+ return false if @min && value < @min
12
+ return false if @max && value > @max
13
+ true
14
+ end
15
+ end
16
+
17
+ class Integer < Number
18
+ def ===(value)
19
+ return false unless value.is_a?(::Integer)
20
+ super
21
+ end
22
+ end
23
+
24
+ class Float < Number
25
+ def ===(value)
26
+ return false unless value.is_a?(::Float)
27
+ super
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ module JsonStructure
2
+ class Object_ < Type
3
+ def initialize(hash = nil)
4
+ @hash = hash.each_with_object({}) do |(key, value), new_hash|
5
+ new_hash[key.to_s] = value
6
+ end
7
+ end
8
+
9
+ def ===(value)
10
+ return false unless value.is_a?(Hash)
11
+
12
+ if @hash
13
+ return @hash.all? do |key, type|
14
+ type === value[key]
15
+ end
16
+ end
17
+
18
+ true
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ module JsonStructure
2
+ class String < Type
3
+ def initialize(min_length: nil, max_length: nil, pattern: nil)
4
+ @min_length = min_length
5
+ @max_length = max_length
6
+ @pattern = pattern
7
+ end
8
+
9
+ def ===(value)
10
+ return false unless value.is_a?(::String)
11
+
12
+ return false unless @min_length && value.size < @min_length
13
+ return false unless @max_length && value.size > @max_length
14
+ return false unless @pattern && pattern =~ value
15
+ true
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module JsonStructure
2
+ class Type
3
+ def initialize(cond)
4
+ @cond = cond
5
+ end
6
+
7
+ def ===(value)
8
+ !!(@cond && @cond.call(value))
9
+ end
10
+
11
+ def |(type)
12
+ AnyOf.new(self, type)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module JsonStructure
2
+ class Value
3
+ def initialize(*values)
4
+ @values = values
5
+ end
6
+
7
+ def ===(value)
8
+ @values.include?(value)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module JsonStructure
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,37 @@
1
+ require 'json_structure/type'
2
+ require 'json_structure/object'
3
+ require 'json_structure/array'
4
+ require 'json_structure/number'
5
+ require 'json_structure/string'
6
+ require 'json_structure/null'
7
+ require 'json_structure/any_of'
8
+ require 'json_structure/value'
9
+
10
+ module JsonStructure
11
+ def self.build(&block)
12
+ Builder.new.instance_eval(&block)
13
+ end
14
+
15
+ class Builder
16
+ [
17
+ Type,
18
+ Object_,
19
+ Array,
20
+ Number,
21
+ Integer,
22
+ Float,
23
+ String,
24
+ Null,
25
+ AnyOf,
26
+ ].each do |klass|
27
+ method = klass.name
28
+ .gsub(/([a-z])([A-Z])/, '\1_\2')
29
+ .gsub(/_+$/, '')
30
+ .downcase
31
+
32
+ define_method(method) do |*args|
33
+ klass.new(*args)
34
+ end
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_structure
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Kaido Iwamoto
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-26 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.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
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
+ description: ''
42
+ email:
43
+ - h6824.kaido@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - json_structure.gemspec
57
+ - lib/json_structure.rb
58
+ - lib/json_structure/any_of.rb
59
+ - lib/json_structure/array.rb
60
+ - lib/json_structure/null.rb
61
+ - lib/json_structure/number.rb
62
+ - lib/json_structure/object.rb
63
+ - lib/json_structure/string.rb
64
+ - lib/json_structure/type.rb
65
+ - lib/json_structure/value.rb
66
+ - lib/json_structure/version.rb
67
+ homepage: http://github.com/odiak/json_structure
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.4.5
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: JSON structure checker
91
+ test_files: []
92
+ has_rdoc: