jsss 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/jsss.rb +4 -0
- data/lib/jsss/matcher.rb +45 -0
- data/lib/jsss/parser.rb +30 -0
- data/spec/jsss_spec.rb +118 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +99 -0
data/.document
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Roman Kamyk jr
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= jsss
|
2
|
+
|
3
|
+
JSon Simple Specification.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Roman Kamyk jr. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'bundler'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "jsss"
|
9
|
+
gem.summary = %Q{JSon Simple Specification}
|
10
|
+
# gem.description = %Q{TODO: longer description of your gem}
|
11
|
+
gem.email = "roman.kamyk@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/rkj/jsss"
|
13
|
+
gem.authors = ["Roman Kamyk jr"]
|
14
|
+
gem.add_bundler_dependencies
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "jsss #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/jsss.rb
ADDED
data/lib/jsss/matcher.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'json'
|
2
|
+
module JSON::Spec
|
3
|
+
class Matcher
|
4
|
+
DataTypes = {
|
5
|
+
:String => String,
|
6
|
+
:Integer => Numeric
|
7
|
+
}
|
8
|
+
def initialize(spec)
|
9
|
+
@spec = if spec.is_a? String
|
10
|
+
Parser.new(spec).parse
|
11
|
+
else
|
12
|
+
spec
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def match(obj)
|
17
|
+
obj = JSON.parse(obj) if obj.is_a? String
|
18
|
+
do_match(obj, @spec)
|
19
|
+
end # match
|
20
|
+
|
21
|
+
private
|
22
|
+
def do_match(obj, m)
|
23
|
+
return false if obj.nil?
|
24
|
+
if m.is_a? Symbol
|
25
|
+
# todo user defined data types
|
26
|
+
return obj.is_a? DataTypes[m]
|
27
|
+
elsif m.is_a? String
|
28
|
+
return m == obj
|
29
|
+
end
|
30
|
+
return false unless (obj.class == m.class)
|
31
|
+
if obj.is_a? Array
|
32
|
+
obj.all? { |e| do_match(e, m.first) }
|
33
|
+
elsif obj.is_a? Hash
|
34
|
+
tmp = obj.clone
|
35
|
+
syms, others = m.to_a.partition { |k, v| k.is_a? Symbol }
|
36
|
+
raise 'There should be only one sym key' if syms.length > 1
|
37
|
+
sym = syms.first
|
38
|
+
others.all? { |k, v| do_match(tmp.delete(k), v) } &&
|
39
|
+
tmp.all? { |k, v| do_match(k, sym[0]) && do_match(v, sym[1])}
|
40
|
+
else
|
41
|
+
true
|
42
|
+
end
|
43
|
+
end # do_match(obj, m)
|
44
|
+
end # class Matcher
|
45
|
+
end # module
|
data/lib/jsss/parser.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'strscan'
|
2
|
+
require 'json/common'
|
3
|
+
require 'json/pure/parser'
|
4
|
+
|
5
|
+
module JSON
|
6
|
+
module Spec
|
7
|
+
class Parser < JSON::Pure::Parser
|
8
|
+
DATA_TYPE = /[A-Z][a-z]*/
|
9
|
+
ELIPSIS = /\.\.\./
|
10
|
+
def parse_string
|
11
|
+
if scan(DATA_TYPE)
|
12
|
+
self[0].intern
|
13
|
+
else
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse_value
|
19
|
+
case
|
20
|
+
when skip(ELIPSIS)
|
21
|
+
:more
|
22
|
+
else
|
23
|
+
super
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
spec = JSON::Spec::Parser.new(%{[{String: Integer}, ...]}).parse
|
data/spec/jsss_spec.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Jsss" do
|
4
|
+
it "should load sample specs" do
|
5
|
+
JSON::Spec::Parser.new(%{[{String: Integer}, ...]}).parse
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should match good data to spec" do
|
9
|
+
spec = JSON::Spec::Parser.new("[{String: Integer}, ...]").parse
|
10
|
+
data = JSON::Pure::Parser.new('[{"123": 12}, {"ala": 16}]').parse
|
11
|
+
# s.match(data).should == true
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'simple Array of Strings' do
|
15
|
+
before(:all) do
|
16
|
+
@spec = JSON::Spec::Matcher.new("[String, ...]")
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should be fine with proper data' do
|
20
|
+
[
|
21
|
+
'["Ala", "ma", "kota"]',
|
22
|
+
'["Ala"]',
|
23
|
+
'[]'
|
24
|
+
].each do |json|
|
25
|
+
@spec.match(json).should == true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should fail on other data" do
|
30
|
+
[
|
31
|
+
'["ala", 12]',
|
32
|
+
'[1]',
|
33
|
+
'{"x":"y"}'
|
34
|
+
].each do |json|
|
35
|
+
@spec.match(json).should == false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'Hash of keys to Strings' do
|
41
|
+
before(:all) do
|
42
|
+
@spec = JSON::Spec::Matcher.new('{"Thing1": String, "Thing2": String}')
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should be fine on proper data" do
|
46
|
+
@spec.match('{"Thing1": "kot", "Thing2": "foo"}').should == true
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should fail on other data" do
|
50
|
+
@spec.match('{}').should == false
|
51
|
+
@spec.match('{"Thing1": "kot"}').should == false
|
52
|
+
@spec.match('{"Thing2": "foo"}').should == false
|
53
|
+
@spec.match('{"Thingwe1": "kot", "Thing2": "foo"}').should == false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe 'Hash of Strings to Strings' do
|
58
|
+
before(:all) do
|
59
|
+
@spec = JSON::Spec::Matcher.new("{String: String}")
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should be fine on proper data" do
|
63
|
+
@spec.match('{}').should == true
|
64
|
+
@spec.match('{"Ala": "kot"}').should == true
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should fail on other data" do
|
68
|
+
@spec.match('{"Ala": 1}').should == false
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe 'Hash with values and symbols' do
|
73
|
+
before(:all) do
|
74
|
+
@spec = JSON::Spec::Matcher.new('{"X": "Y", "Thing1": String, String: String}')
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should be fine on proper data" do
|
78
|
+
[
|
79
|
+
'{"X": "Y", "Thing1": "String", "String": "String"}',
|
80
|
+
'{"X": "Y", "Thing1": "String"}',
|
81
|
+
'{"X": "Y", "Thing1": "String", "String": "String", "ala": "Kota"}'
|
82
|
+
].each do |json|
|
83
|
+
@spec.match(json).should == true
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should fail on other data" do
|
88
|
+
[
|
89
|
+
'{"X": "Y", "Thing1": 4, "String": "String"}',
|
90
|
+
'{"X": "Z", "Thing1": "String"}',
|
91
|
+
'{"X": "Y", "Thing1": "String", "String": "String", "ala": 10}'
|
92
|
+
].each do |json|
|
93
|
+
@spec.match(json).should == false
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe 'array of hashes' do
|
99
|
+
before(:all) do
|
100
|
+
@spec = JSON::Spec::Matcher.new('[{"X": String, "Y": Integer}, ...]')
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should be fine on proper data" do
|
104
|
+
@spec.match('[]').should == true
|
105
|
+
@spec.match('[{"X": "Y", "Y": 10},{"X": "Z", "Y": 20}]').should == true
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should fail on other data" do
|
109
|
+
[
|
110
|
+
'{"X": "Y", "Thing1": 4, "String": "String"}',
|
111
|
+
'{"X": "Z", "Thing1": "String"}',
|
112
|
+
'[{"X": "Y", "Thing1": "String", "String": "String", "ala": 10}]'
|
113
|
+
].each do |json|
|
114
|
+
@spec.match(json).should == false
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsss
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Roman Kamyk jr
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-18 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
type: :runtime
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
segments:
|
27
|
+
- 0
|
28
|
+
version: "0"
|
29
|
+
name: rspec
|
30
|
+
requirement: *id001
|
31
|
+
prerelease: false
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
segments:
|
39
|
+
- 0
|
40
|
+
version: "0"
|
41
|
+
name: json_pure
|
42
|
+
requirement: *id002
|
43
|
+
prerelease: false
|
44
|
+
description:
|
45
|
+
email: roman.kamyk@gmail.com
|
46
|
+
executables: []
|
47
|
+
|
48
|
+
extensions: []
|
49
|
+
|
50
|
+
extra_rdoc_files:
|
51
|
+
- LICENSE
|
52
|
+
- README.rdoc
|
53
|
+
files:
|
54
|
+
- .document
|
55
|
+
- .gitignore
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE
|
58
|
+
- README.rdoc
|
59
|
+
- Rakefile
|
60
|
+
- VERSION
|
61
|
+
- lib/jsss.rb
|
62
|
+
- lib/jsss/matcher.rb
|
63
|
+
- lib/jsss/parser.rb
|
64
|
+
- spec/jsss_spec.rb
|
65
|
+
- spec/spec.opts
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
has_rdoc: true
|
68
|
+
homepage: http://github.com/rkj/jsss
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options:
|
73
|
+
- --charset=UTF-8
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.3.6
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: JSon Simple Specification
|
97
|
+
test_files:
|
98
|
+
- spec/jsss_spec.rb
|
99
|
+
- spec/spec_helper.rb
|