json_schema_tools 0.2.1 → 0.2.2
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/CHANGELOG.md +1 -0
- data/lib/schema_tools/modules/hash.rb +10 -2
- data/lib/schema_tools/version.rb +1 -1
- data/spec/fixtures/client.json +3 -10
- data/spec/schema_tools/hash_spec.rb +8 -3
- metadata +1 -1
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
2013-10
|
5
|
+
* allow all object properties in link href placeholders => contacts/{id}/{number}
|
5
6
|
* add base_url option to schema hash creation. Prepends an url to all links of a rendered object
|
6
7
|
* add to_schema_json for simpler model to json conversion
|
7
8
|
* add option to exclude_root in to_schema hash method
|
@@ -90,8 +90,16 @@ module SchemaTools
|
|
90
90
|
def parse_links(obj, schema, opts={})
|
91
91
|
links = []
|
92
92
|
schema['links'] && schema['links'].each do |link|
|
93
|
-
|
94
|
-
|
93
|
+
href = link['href'].dup
|
94
|
+
# placeholders: find all {xy}, create replacement ary with
|
95
|
+
# values, than replace
|
96
|
+
matches = href.scan(/{(\w+)}/) #{abc} => abc
|
97
|
+
replaces = []
|
98
|
+
matches.each do |match|
|
99
|
+
obj_val = obj.send(match[0]) if obj.respond_to?(match[0])
|
100
|
+
replaces << ["{#{match[0]}}", obj_val] if obj_val
|
101
|
+
end
|
102
|
+
replaces.each {|r| href.gsub!(r[0], r[1])}
|
95
103
|
href = "#{opts[:base_url]}/#{href}" if opts[:base_url]
|
96
104
|
|
97
105
|
links << { 'rel' => link['rel'],
|
data/lib/schema_tools/version.rb
CHANGED
data/spec/fixtures/client.json
CHANGED
@@ -98,11 +98,6 @@
|
|
98
98
|
"description": "Sort the results by the given field => number",
|
99
99
|
"enum":["organisation", "number","email","first_name","last_name", "created_at", "updated_at"],
|
100
100
|
"type": "string"
|
101
|
-
},
|
102
|
-
"sort":{
|
103
|
-
"title" : "Sort",
|
104
|
-
"enum":["ASC","DESC"],
|
105
|
-
"description": "Sort the results in ASC or DESC"
|
106
101
|
}
|
107
102
|
}
|
108
103
|
},
|
@@ -121,11 +116,9 @@
|
|
121
116
|
{ "rel": "documents",
|
122
117
|
"href": "clients/{id}/documents"
|
123
118
|
},
|
124
|
-
{ "rel": "
|
125
|
-
"href": "clients/{id}/
|
126
|
-
},
|
127
|
-
{ "rel": "invoices",
|
128
|
-
"href": "clients/{id}/invoices"
|
119
|
+
{ "rel": "test replacement",
|
120
|
+
"href": "clients/{id}/{first_name}"
|
129
121
|
}
|
122
|
+
|
130
123
|
]
|
131
124
|
}
|
@@ -71,7 +71,7 @@ describe SchemaTools::Hash do
|
|
71
71
|
|
72
72
|
it 'should have _links on object if exclude root' do
|
73
73
|
hash = SchemaTools::Hash.from_schema(contact, exclude_root: true, class_name: :client)
|
74
|
-
hash['_links'].length.should ==
|
74
|
+
hash['_links'].length.should == 7
|
75
75
|
end
|
76
76
|
|
77
77
|
it 'should have _class_name on object if exclude root' do
|
@@ -170,12 +170,17 @@ describe SchemaTools::Hash do
|
|
170
170
|
|
171
171
|
it 'should have links' do
|
172
172
|
hash = SchemaTools::Hash.from_schema(client)
|
173
|
-
hash['links'].length.should ==
|
173
|
+
hash['links'].length.should == 7
|
174
174
|
end
|
175
175
|
|
176
176
|
it 'should prepend base_url' do
|
177
177
|
hash = SchemaTools::Hash.from_schema(client, base_url: 'http://json-hell.com')
|
178
|
-
hash['links'].first['href'].should
|
178
|
+
hash['links'].first['href'].should include( 'http://json-hell.com')
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'should replace placeholders' do
|
182
|
+
hash = SchemaTools::Hash.from_schema(client, base_url: 'http://json-hell.com')
|
183
|
+
hash['links'].last['href'].should == 'http://json-hell.com/clients/SomeID/Peter'
|
179
184
|
end
|
180
185
|
|
181
186
|
end
|