json_schema_tools 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
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
- # substitute placeholders
94
- href = link['href'].gsub(/\{id\}/, "#{obj.id}")
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'],
@@ -1,3 +1,3 @@
1
1
  module SchemaTools
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
@@ -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": "attachments",
125
- "href": "clients/{id}/attachments"
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 == 8
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 == 8
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 == 'http://json-hell.com/clients/SomeID'
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schema_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: