json_schema_tools 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
 
4
4
  2013-10
5
-
5
+ * add base_url option to schema hash creation. Prepends an url to all links of a rendered object
6
+ * add to_schema_json for simpler model to json conversion
6
7
  * add option to exclude_root in to_schema hash method
7
8
 
8
9
  2013-06
data/README.md CHANGED
@@ -100,9 +100,9 @@ peter.as_schema_hash
100
100
  The AsSchema module is a tiny wrapper for following low level method:
101
101
 
102
102
  ```ruby
103
- paul = Contact.new name: 'Peter'
103
+ paul = Contact.new name: 'Paul'
104
104
  contact_hash = SchemaTools::Hash.from_schema(paul)
105
- #=> "contact"=>{"id"=>12, "name"=> "Peter", "email"=>"",..}
105
+ #=> "contact"=>{"id"=>12, "name"=> "Paul",..}
106
106
  # to_json is up to you .. or your rails controller
107
107
  ```
108
108
 
@@ -117,12 +117,6 @@ Only use some fields e.g. to save bandwidth
117
117
  peter.as_schema_json(fields:['id', 'name'])
118
118
  #=> "client":{"id":12, "name": "Peter"}
119
119
  ```
120
- Of course the low level hash method also supports all of these options:
121
-
122
- ```ruby
123
- client_hash = SchemaTools::Hash.from_schema(peter, fields:['id', 'name'])
124
- #=> "client"=>{"id"=>12, "name"=> "Peter"}
125
- ```
126
120
 
127
121
  Use a custom schema name e.g. to represent a client as contact. Assumes you also
128
122
  have a schema named contact.json
@@ -148,11 +142,17 @@ inline:
148
142
 
149
143
  peter.as_schema_json( exclude_root: true )
150
144
 
151
- client_hash = SchemaTools::Hash.from_schema(peter, exclude_root: true)
152
- #=> {"id"=>12, "name"=> "Peter",
153
- # "_class_name":"client", "_links":[ .. are inlined .. ]}
145
+ #=> {"id":12, "name"=> "Peter",
146
+ # "_class_name":"client",
147
+ # "_links":[ .. ] }
154
148
  ```
155
149
 
150
+ Of course the low level hash method also supports all of these options:
151
+
152
+ ```ruby
153
+ client_hash = SchemaTools::Hash.from_schema(peter, fields:['id', 'name'])
154
+ #=> "client"=>{"id"=>12, "name"=> "Peter"}
155
+ ```
156
156
  ## Parameter cleaning
157
157
 
158
158
  Hate people spamming your api with wrong object fields? Use the Cleaner to
@@ -32,6 +32,7 @@ module SchemaTools
32
32
  # @options opts [Array<String>] :fields to return. If not set all schema
33
33
  # properties are used.
34
34
  # @options opts [String] :path of the schema files overriding global one
35
+ # @options opts [String] :base_url used in all links
35
36
  # @options opts [Boolean] :exclude_root if set objects are not nested under
36
37
  # their class name and the object hash gets _links and _class_name inline.
37
38
  #
@@ -39,27 +40,17 @@ module SchemaTools
39
40
  # { 'invoice' => {'title'=>'hello world', 'number'=>'4711' } }
40
41
  #
41
42
  def from_schema(obj, opts={})
42
- fields = opts[:fields]
43
+
43
44
  # get objects class name without inheritance
44
45
  real_class_name = obj.class.name.split('::').last.underscore
45
46
  class_name = opts[:class_name] || real_class_name
46
47
 
47
- data = {}
48
48
  # get schema
49
49
  schema = SchemaTools::Reader.read(class_name, opts[:path])
50
50
  # iterate over the defined schema fields
51
- schema['properties'].each do |field, prop|
52
- next if fields && !fields.include?(field)
53
- if prop['type'] == 'array'
54
- data[field] = parse_list(obj, field, prop, opts)
55
- elsif prop['type'] == 'object' # a singular related object
56
- data[field] = parse_object(obj, field, prop, opts)
57
- else # a simple field is only added if the object knows it
58
- data[field] = obj.send(field) if obj.respond_to?(field)
59
- end
60
- end
51
+ data = parse_properties(obj, schema, opts)
61
52
  #get links if present
62
- links = parse_links(obj, schema)
53
+ links = parse_links(obj, schema, opts)
63
54
 
64
55
  if opts[:exclude_root]
65
56
  hsh = data
@@ -74,12 +65,37 @@ module SchemaTools
74
65
 
75
66
  private
76
67
 
68
+ def parse_properties(obj, schema, opts)
69
+ fields = opts[:fields]
70
+ data = {}
71
+ schema['properties'].each do |field, prop|
72
+ next if fields && !fields.include?(field)
73
+ if prop['type'] == 'array'
74
+ data[field] = parse_list(obj, field, prop, opts)
75
+ elsif prop['type'] == 'object' # a singular related object
76
+ data[field] = parse_object(obj, field, prop, opts)
77
+ else # a simple field is only added if the object knows it
78
+ data[field] = obj.send(field) if obj.respond_to?(field)
79
+ end
80
+ end
81
+ data
82
+ end
83
+
77
84
  # Parse the link section of the schema by replacing {id} in urls
85
+ # @param [Object] obj object being parsed
86
+ # @param [Hash] schema
87
+ # @param [Hash] opts
88
+ # @options opts [String] :base_url prepended to link href, WATCH possible double //
78
89
  # @return [Array<Hash{String=>String}> | Nil]
79
- def parse_links(obj, schema)
90
+ def parse_links(obj, schema, opts={})
80
91
  links = []
81
92
  schema['links'] && schema['links'].each do |link|
82
- links << { 'rel' => link['rel'], 'href' => link['href'].gsub(/\{id\}/, "#{obj.id}") }
93
+ # substitute placeholders
94
+ href = link['href'].gsub(/\{id\}/, "#{obj.id}")
95
+ href = "#{opts[:base_url]}/#{href}" if opts[:base_url]
96
+
97
+ links << { 'rel' => link['rel'],
98
+ 'href' => href }
83
99
  end
84
100
  links.uniq
85
101
  # return links only if not empty
@@ -1,3 +1,3 @@
1
1
  module SchemaTools
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -51,8 +51,8 @@ describe SchemaTools::Hash do
51
51
  end
52
52
 
53
53
  it 'should use custom schema' do
54
- hash = SchemaTools::Hash.from_schema(contact, class_name: :contact)
55
- hash['contact']['last_name'].should == 'Paul'
54
+ hash = SchemaTools::Hash.from_schema(contact, class_name: :client)
55
+ hash['client']['last_name'].should == 'Paul'
56
56
  end
57
57
 
58
58
  it 'should use only give fields' do
@@ -157,5 +157,27 @@ describe SchemaTools::Hash do
157
157
  end
158
158
 
159
159
  end
160
+
161
+ context 'with links' do
162
+ let(:client){Client.new}
163
+ before :each do
164
+ client.first_name = 'Peter'
165
+ client.id = 'SomeID'
166
+ end
167
+ after :each do
168
+ SchemaTools::Reader.registry_reset
169
+ end
170
+
171
+ it 'should have links' do
172
+ hash = SchemaTools::Hash.from_schema(client)
173
+ hash['links'].length.should == 8
174
+ end
175
+
176
+ it 'should prepend base_url' do
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'
179
+ end
180
+
181
+ end
160
182
  end
161
183
 
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.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-12 00:00:00.000000000 Z
12
+ date: 2013-10-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -128,18 +128,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
128
  - - ! '>='
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
- segments:
132
- - 0
133
- hash: -1056530391654598731
134
131
  required_rubygems_version: !ruby/object:Gem::Requirement
135
132
  none: false
136
133
  requirements:
137
134
  - - ! '>='
138
135
  - !ruby/object:Gem::Version
139
136
  version: '0'
140
- segments:
141
- - 0
142
- hash: -1056530391654598731
143
137
  requirements: []
144
138
  rubyforge_project:
145
139
  rubygems_version: 1.8.24