json_schema_tools 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +5 -2
- data/lib/schema_tools/modules/attributes.rb +21 -4
- data/lib/schema_tools/version.rb +1 -1
- data/spec/schema_tools/modules/attributes_spec.rb +14 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MWUzNzQ4ZTc1NjNjNzRmYzc3Y2Y1ODg2MDBjZWU3NzdhYjNlMzBmOQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NGI2YjAzZGQ2M2UxMDY1M2M5NWNiY2E2NzRhYzYxZDhlZTZiY2E5Zg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MzAxYjIwZTgzN2JhMmMyYTQ5ZGNmNjIyYjdhYzg0ZGNkM2E0ODk2ZTcwZDU2
|
10
|
+
NzU2OWI1MjU1YjA2ZjEzYjc0ZjI1YmU0ZDg5MzEzNGY1ZjRjYTYxNGU1NmYy
|
11
|
+
ZDQyM2NmM2I4NWMxOGViOWZlYzA2YzQ5MmRiMWQ3NmE1M2ViNTI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OTcxMDg5YmQ1NjQ4NzcxMzhkN2U4MjVhYTk2MWU5NjQzODAwZGI5MzU1MTkz
|
14
|
+
OGM3MmFmMGM1ZTUwYzM4YzhmNzRhYjUwZTFiZTE1YTZiNjU2YzkxOGM1NGVl
|
15
|
+
MGQzYjBiZTgzZmE2ZmQzNDQzZjVhYmNiMDIxNTA3NWY1MTdmMDg=
|
data/README.md
CHANGED
@@ -200,9 +200,12 @@ class Contact
|
|
200
200
|
has_schema_attrs :contact
|
201
201
|
end
|
202
202
|
|
203
|
-
json_str = '{"id": "123456", "last_name": "Meier","first_name": "Peter"}
|
203
|
+
json_str = '{"id": "123456", "last_name": "Meier","first_name": "Peter"}'
|
204
204
|
c = Contact.from_json(json_str)
|
205
|
-
c.id #=> 123456
|
205
|
+
c.id #=> "123456"
|
206
|
+
|
207
|
+
# new object from hash (if your favorite http tool already parsed the json)
|
208
|
+
c = Contact.from_hash({"id"=>123456, "last_name"=>"Meier","first_name"=>"Peter"})
|
206
209
|
|
207
210
|
```
|
208
211
|
|
@@ -44,8 +44,9 @@ module SchemaTools
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
# Create a new object from a json string
|
48
|
-
#
|
47
|
+
# Create a new object from a json string or a ruby hash (already created
|
48
|
+
# from json string). Auto-detects nesting by checking for a hash key
|
49
|
+
# with the same name as the schema_name:
|
49
50
|
#
|
50
51
|
# class Contact
|
51
52
|
# include SchemaTools::Modules::Attributes
|
@@ -53,11 +54,27 @@ module SchemaTools
|
|
53
54
|
# end
|
54
55
|
# c = Contact.from_json('{ "id": "123456", "last_name": "Meier" }')
|
55
56
|
# c.id #=>123456
|
56
|
-
# c = Contact.from_json('
|
57
|
+
# c = Contact.from_json( {'contact'=>{ "id=>"123456", "last_name"=>"Meier" }} )
|
57
58
|
#
|
58
|
-
# @param [String] json
|
59
|
+
# @param [String|Hash{String=>Mixed}] json string or hash
|
59
60
|
def from_json(json)
|
60
61
|
hash = JSON.parse(json)
|
62
|
+
from_hash(hash)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Create a new object from a ruby hash (e.g parsed from json string).
|
66
|
+
# Auto-detects nesting by checking for a hash key with the same name as
|
67
|
+
# the schema_name:
|
68
|
+
#
|
69
|
+
# class Contact
|
70
|
+
# include SchemaTools::Modules::Attributes
|
71
|
+
# has_schema_attrs :contact
|
72
|
+
# end
|
73
|
+
# c = Contact.from_hash( {'contact'=>{ "id=>"123456", "last_name"=>"Meier" }} )
|
74
|
+
# c.id #=>123456
|
75
|
+
#
|
76
|
+
# @param [String|Hash{String=>Mixed}] json string or hash
|
77
|
+
def from_hash(hash)
|
61
78
|
# test if hash is nested and shift up
|
62
79
|
if hash.length == 1 && hash["#{schema_name}"]
|
63
80
|
hash = hash["#{schema_name}"]
|
data/lib/schema_tools/version.rb
CHANGED
@@ -46,7 +46,7 @@ describe SchemaTools::Modules::Attributes do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
context '
|
49
|
+
context '.from_json' do
|
50
50
|
|
51
51
|
it 'creates new object' do
|
52
52
|
str = load_fixture_data('contact_plain.json')
|
@@ -68,6 +68,19 @@ describe SchemaTools::Modules::Attributes do
|
|
68
68
|
expect(obj.contact_source).to eq hash['contact_source']
|
69
69
|
expect(obj.first_name).to eq hash['first_name']
|
70
70
|
expect(obj.last_name).to eq hash['last_name']
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context '.from_hash' do
|
75
|
+
|
76
|
+
it 'creates new object from hash' do
|
77
|
+
hash = JSON.parse(load_fixture_data('contact_nested.json'))
|
78
|
+
obj = TestContact.from_hash(hash)
|
79
|
+
expect(obj.id).to eq hash['contact']['id']
|
80
|
+
expect(obj.organisation).to eq hash['contact']['organisation']
|
81
|
+
expect(obj.contact_source).to eq hash['contact']['contact_source']
|
82
|
+
expect(obj.first_name).to eq hash['contact']['first_name']
|
83
|
+
expect(obj.last_name).to eq hash['contact']['last_name']
|
71
84
|
end
|
72
85
|
|
73
86
|
end
|