json_schema_tools 0.4.0 → 0.4.1
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.
- checksums.yaml +8 -8
- data/CHANGELOG.md +1 -0
- data/README.md +21 -0
- data/lib/schema_tools/modules/attributes.rb +35 -6
- data/lib/schema_tools/version.rb +1 -1
- data/spec/fixtures/data/contact_nested.json +9 -0
- data/spec/fixtures/data/contact_plain.json +10 -0
- data/spec/schema_tools/circuluar_ref_spec.rb +1 -1
- data/spec/schema_tools/hash_spec.rb +1 -1
- data/spec/schema_tools/modules/attributes_spec.rb +34 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/test_helpers.rb +10 -0
- metadata +20 -18
- /data/spec/fixtures/{address.json → schemata/address.json} +0 -0
- /data/spec/fixtures/{basic_definitions.json → schemata/basic_definitions.json} +0 -0
- /data/spec/fixtures/{circular_references_twice.json → schemata/circular_references_twice.json} +0 -0
- /data/spec/fixtures/{client.json → schemata/client.json} +0 -0
- /data/spec/fixtures/{contact.json → schemata/contact.json} +0 -0
- /data/spec/fixtures/{includes_basic_definitions.json → schemata/includes_basic_definitions.json} +0 -0
- /data/spec/fixtures/{includes_deep_nested_refs.json → schemata/includes_deep_nested_refs.json} +0 -0
- /data/spec/fixtures/{lead.json → schemata/lead.json} +0 -0
- /data/spec/fixtures/{nested_schemas → schemata/nested_schemas}/person.json +0 -0
- /data/spec/fixtures/{one_of_definition.json → schemata/one_of_definition.json} +0 -0
- /data/spec/fixtures/{page.json → schemata/page.json} +0 -0
- /data/spec/fixtures/{relative_paths → schemata/relative_paths}/includes_relative_path.json +0 -0
- /data/spec/{fixtures_broken → fixtures/schemata_broken}/circular_references.json +0 -0
- /data/spec/{fixtures_broken → fixtures/schemata_broken}/circular_references_multi.json +0 -0
- /data/spec/{fixtures_broken → fixtures/schemata_broken}/circular_references_multi_file_one.json +0 -0
- /data/spec/{fixtures_broken → fixtures/schemata_broken}/circular_references_multi_file_two.json +0 -0
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZWI2NjlkM2IyMjgxZGYyODJkNzk5NmY0MjQxMjA0YWQ4ZTNiYTk3Nw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MDhlNWEwMTY5ZjY2ZGJiYjViMjdhZWI5Nzg5NGI4MTM3ZmZmODk0OQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YmVlMDY3ZTBkOTJkNzA2OWExNWRiYjM3ZDhhNGNkMTJkZDE2MGQ0YWM1OTIz
|
10
|
+
Y2NiNDQwNzA0YzljODUzNTM4YmU1MGRmOWVkZDI1MGVhYzg1MWJkNWUzZDY3
|
11
|
+
ZGQ2MzA5NTBjZTZjZjJkZTRmODdjOGExOTE2NDVmY2IxZjVmOWM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NTFjZjdkMjAzNDgxOWIxYjdiYmEyYTI3ZGMxNWM2YWViMDJlZmZlNzA3Yzc2
|
14
|
+
YWEwMzNlZmVjOTIxNjQ5M2JmYzE0YTRjMjhmOGFmZmUxZThlMWQ5NWQwZDFi
|
15
|
+
ODIyMzY3NDY2Zjc1Y2NkM2JhMGE0YThlNDJiZWMyN2ViNWJiOTU=
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
2014-11
|
4
4
|
|
5
|
+
* add from_json method to attributes module, to create new objects from a json string
|
5
6
|
* remove as_json method(alias of as_schema_json) conflicting with active_resource/active_record. Nothing stops one from using/aliasing it in his own code.
|
6
7
|
|
7
8
|
2014-10
|
data/README.md
CHANGED
@@ -186,6 +186,27 @@ Of course the low level hash method also supports all of these options:
|
|
186
186
|
client_hash = SchemaTools::Hash.from_schema(peter, fields:['id', 'name'])
|
187
187
|
#=> "client"=>{"id"=>12, "name"=> "Peter"}
|
188
188
|
```
|
189
|
+
|
190
|
+
## Object from JSON
|
191
|
+
|
192
|
+
On any remote site you'll need to instantiate new objects from an incoming json
|
193
|
+
string. Let's assume a contact class which you need for incoming contact json
|
194
|
+
data. (Example taken from attributes_spec.rb)
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
SchemaTools.schema_path = '/path/to/schema-json-files-with-contact-def'
|
198
|
+
class Contact
|
199
|
+
include SchemaTools::Modules::Attributes
|
200
|
+
has_schema_attrs :contact
|
201
|
+
end
|
202
|
+
|
203
|
+
json_str = '{"id": "123456", "last_name": "Meier","first_name": "Peter"}
|
204
|
+
c = Contact.from_json(json_str)
|
205
|
+
c.id #=> 123456
|
206
|
+
|
207
|
+
```
|
208
|
+
|
209
|
+
|
189
210
|
## Parameter cleaning
|
190
211
|
|
191
212
|
Hate people spamming your api with wrong object fields? Use the Cleaner to
|
@@ -8,10 +8,10 @@ module SchemaTools
|
|
8
8
|
# class Contact
|
9
9
|
# has_schema_attrs :contact
|
10
10
|
# end
|
11
|
-
# Contact.schema_name
|
12
|
-
# Contact.
|
13
|
-
# Contact.as_hash
|
14
|
-
# Contact.schema
|
11
|
+
# Contact.schema_name #=> contact
|
12
|
+
# Contact.as_schema_json #=> json string
|
13
|
+
# Contact.as_hash #=> ruby hash
|
14
|
+
# Contact.schema #=> json schema hash
|
15
15
|
module Attributes
|
16
16
|
extend ActiveSupport::Concern
|
17
17
|
include SchemaTools::Modules::AsSchema
|
@@ -37,12 +37,41 @@ module SchemaTools
|
|
37
37
|
|
38
38
|
self.schema= reader.read(schema_name, schema_location)
|
39
39
|
self.schema_name(schema_name)
|
40
|
-
# make getter / setter
|
40
|
+
# make getter / setter methods
|
41
41
|
self.schema[:properties].each do |key, prop|
|
42
42
|
define_method(key) { schema_attrs[key] }
|
43
43
|
define_method("#{key}=") { |value| schema_attrs[key] = value } unless prop[:readonly]
|
44
44
|
end
|
45
|
-
|
45
|
+
end
|
46
|
+
|
47
|
+
# Create a new object from a json string. Auto-detects nesting by
|
48
|
+
# checking for a hash key with the same name as the schema_name:
|
49
|
+
#
|
50
|
+
# class Contact
|
51
|
+
# include SchemaTools::Modules::Attributes
|
52
|
+
# has_schema_attrs :contact
|
53
|
+
# end
|
54
|
+
# c = Contact.from_json('{ "id": "123456", "last_name": "Meier" }')
|
55
|
+
# c.id #=>123456
|
56
|
+
# c = Contact.from_json('{"contact:{ "id": "123456", "last_name": "Meier" }}')
|
57
|
+
#
|
58
|
+
# @param [String] json
|
59
|
+
def from_json(json)
|
60
|
+
hash = JSON.parse(json)
|
61
|
+
# test if hash is nested and shift up
|
62
|
+
if hash.length == 1 && hash["#{schema_name}"]
|
63
|
+
hash = hash["#{schema_name}"]
|
64
|
+
end
|
65
|
+
obj = new
|
66
|
+
hash.each do |key, val|
|
67
|
+
next unless obj.respond_to?(key)
|
68
|
+
# set values to raw schema attributes, even if there are no setters
|
69
|
+
# assuming this objects comes from a remote site
|
70
|
+
# TODO type conversion string/integer/number/date/datetime?
|
71
|
+
obj.schema_attrs[key] = val
|
72
|
+
# TODO if val is a hash / array => look for nested class
|
73
|
+
end
|
74
|
+
obj
|
46
75
|
end
|
47
76
|
|
48
77
|
# @param [Hash] schema_hash
|
data/lib/schema_tools/version.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe SchemaTools::Reader do
|
4
|
-
BROKEN_SCHEMA_PATH = File.
|
4
|
+
BROKEN_SCHEMA_PATH = File.join(fixture_path, 'schemata_broken')
|
5
5
|
context 'circular references' do
|
6
6
|
it 'should raise exception for circular $refs' do
|
7
7
|
expect{
|
@@ -49,7 +49,7 @@ describe SchemaTools::Hash do
|
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'should use custom schema path' do
|
52
|
-
custom_path = File.expand_path('../../fixtures', __FILE__)
|
52
|
+
custom_path = File.expand_path('../../fixtures/schemata', __FILE__)
|
53
53
|
hash = SchemaTools::Hash.from_schema(contact, path: custom_path)
|
54
54
|
hash['last_name'].should == 'Paul'
|
55
55
|
end
|
@@ -1,8 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class TestClient
|
4
4
|
include SchemaTools::Modules::Attributes
|
5
5
|
has_schema_attrs :client
|
6
|
+
end
|
7
|
+
|
8
|
+
class TestContact
|
9
|
+
include SchemaTools::Modules::Attributes
|
10
|
+
has_schema_attrs :contact
|
6
11
|
end
|
7
12
|
|
8
13
|
class Numbers
|
@@ -13,7 +18,7 @@ end
|
|
13
18
|
describe SchemaTools::Modules::Attributes do
|
14
19
|
|
15
20
|
context 'included' do
|
16
|
-
subject {
|
21
|
+
subject { TestClient.new }
|
17
22
|
|
18
23
|
it 'should add getter methods' do
|
19
24
|
subject.should respond_to(:last_name)
|
@@ -41,6 +46,33 @@ describe SchemaTools::Modules::Attributes do
|
|
41
46
|
end
|
42
47
|
end
|
43
48
|
|
49
|
+
context 'new from_json' do
|
50
|
+
|
51
|
+
it 'creates new object' do
|
52
|
+
str = load_fixture_data('contact_plain.json')
|
53
|
+
hash = JSON.parse(str)
|
54
|
+
obj = TestContact.from_json(str)
|
55
|
+
expect(obj.id).to eq hash['id']
|
56
|
+
expect(obj.organisation).to eq hash['organisation']
|
57
|
+
expect(obj.contact_source).to eq hash['contact_source']
|
58
|
+
expect(obj.first_name).to eq hash['first_name']
|
59
|
+
expect(obj.last_name).to eq hash['last_name']
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'creates new object from nested response' do
|
63
|
+
str = load_fixture_data('contact_nested.json')
|
64
|
+
hash = JSON.parse(str)['contact']
|
65
|
+
obj = TestContact.from_json(str)
|
66
|
+
expect(obj.id).to eq hash['id']
|
67
|
+
expect(obj.organisation).to eq hash['organisation']
|
68
|
+
expect(obj.contact_source).to eq hash['contact_source']
|
69
|
+
expect(obj.first_name).to eq hash['first_name']
|
70
|
+
expect(obj.last_name).to eq hash['last_name']
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
|
44
76
|
context 'attributes from dynamic schema' do
|
45
77
|
subject { Numbers.new }
|
46
78
|
|
data/spec/spec_helper.rb
CHANGED
@@ -18,6 +18,6 @@ end
|
|
18
18
|
|
19
19
|
I18n.enforce_available_locales = false if I18n.respond_to?('enforce_available_locales=')
|
20
20
|
# set global json schema path for examples
|
21
|
-
SchemaTools.schema_path = File.
|
21
|
+
SchemaTools.schema_path = File.join(fixture_path,'schemata')
|
22
22
|
|
23
23
|
puts "Testing with ActiveModel Version: #{ActiveModel.version rescue ActiveModel::VERSION::STRING}"
|
data/spec/test_helpers.rb
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
# Set global fixtures path
|
2
|
+
def fixture_path
|
3
|
+
File.expand_path('../fixtures/', __FILE__)
|
4
|
+
end
|
5
|
+
# load a json data object file returns the json string
|
6
|
+
# @return[String] file contents
|
7
|
+
def load_fixture_data(name)
|
8
|
+
File.open(File.join(fixture_path,'data', name ), 'r') { |f| f.read }
|
9
|
+
end
|
10
|
+
|
1
11
|
def schema_as_ruby_object
|
2
12
|
{
|
3
13
|
"type" => "object",
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Georg Leciejewski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -96,22 +96,24 @@ files:
|
|
96
96
|
- lib/schema_tools/ref_resolver.rb
|
97
97
|
- lib/schema_tools/schema.rb
|
98
98
|
- lib/schema_tools/version.rb
|
99
|
-
- spec/fixtures/
|
100
|
-
- spec/fixtures/
|
101
|
-
- spec/fixtures/
|
102
|
-
- spec/fixtures/
|
103
|
-
- spec/fixtures/
|
104
|
-
- spec/fixtures/
|
105
|
-
- spec/fixtures/
|
106
|
-
- spec/fixtures/
|
107
|
-
- spec/fixtures/
|
108
|
-
- spec/fixtures/
|
109
|
-
- spec/fixtures/
|
110
|
-
- spec/fixtures/
|
111
|
-
- spec/
|
112
|
-
- spec/
|
113
|
-
- spec/
|
114
|
-
- spec/
|
99
|
+
- spec/fixtures/data/contact_nested.json
|
100
|
+
- spec/fixtures/data/contact_plain.json
|
101
|
+
- spec/fixtures/schemata/address.json
|
102
|
+
- spec/fixtures/schemata/basic_definitions.json
|
103
|
+
- spec/fixtures/schemata/circular_references_twice.json
|
104
|
+
- spec/fixtures/schemata/client.json
|
105
|
+
- spec/fixtures/schemata/contact.json
|
106
|
+
- spec/fixtures/schemata/includes_basic_definitions.json
|
107
|
+
- spec/fixtures/schemata/includes_deep_nested_refs.json
|
108
|
+
- spec/fixtures/schemata/lead.json
|
109
|
+
- spec/fixtures/schemata/nested_schemas/person.json
|
110
|
+
- spec/fixtures/schemata/one_of_definition.json
|
111
|
+
- spec/fixtures/schemata/page.json
|
112
|
+
- spec/fixtures/schemata/relative_paths/includes_relative_path.json
|
113
|
+
- spec/fixtures/schemata_broken/circular_references.json
|
114
|
+
- spec/fixtures/schemata_broken/circular_references_multi.json
|
115
|
+
- spec/fixtures/schemata_broken/circular_references_multi_file_one.json
|
116
|
+
- spec/fixtures/schemata_broken/circular_references_multi_file_two.json
|
115
117
|
- spec/schema_tools/circuluar_ref_spec.rb
|
116
118
|
- spec/schema_tools/cleaner_spec.rb
|
117
119
|
- spec/schema_tools/hash_spec.rb
|
File without changes
|
File without changes
|
/data/spec/fixtures/{circular_references_twice.json → schemata/circular_references_twice.json}
RENAMED
File without changes
|
File without changes
|
File without changes
|
/data/spec/fixtures/{includes_basic_definitions.json → schemata/includes_basic_definitions.json}
RENAMED
File without changes
|
/data/spec/fixtures/{includes_deep_nested_refs.json → schemata/includes_deep_nested_refs.json}
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
/data/spec/{fixtures_broken → fixtures/schemata_broken}/circular_references_multi_file_one.json
RENAMED
File without changes
|
/data/spec/{fixtures_broken → fixtures/schemata_broken}/circular_references_multi_file_two.json
RENAMED
File without changes
|