json-api 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 174874158c21768263db5ba08be05a222b6205ce
4
- data.tar.gz: 8f3f7988fc68a106009353734a2ba1960e119862
3
+ metadata.gz: 610c75d5238c1945fe0c60fc82f3cfc5dc4e37f1
4
+ data.tar.gz: e2c58493d9d7097279abb64676248ed9152a67ae
5
5
  SHA512:
6
- metadata.gz: 8388f07f2573ff2ee3760ab272e66b2412eec71c0e809118c29104b814302ba303cfdeb0fdb4cf1b2de4ff1ad91a34829ab3e3d628c2120a8cd64ed405f4aa47
7
- data.tar.gz: b743f7c2ac9267a407751114ca56e6e53d056fe1882ae450127bf9937fb59cb8cc9e5fe2d9b3381ed1a3218e608c9dd7e4c66e00ba766eaca258c83a5f3fa7ba
6
+ metadata.gz: aa9880e2e534248caffa4b29e6ba276c2b03612275ed87c6003b33c15690d318058564b135900d1775803fed69f5482a8205eda0f56abe732fd845440105053e
7
+ data.tar.gz: 16ddd0cda8d10301ce49ec6a07a7b42f9265c25a5694c7b845ced89eb20c3b1ba65fa878f3defe3f91cf14353df016945d382a78b5336053a02cf3a8d1eda1d6
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .ruby-version
19
+ .ruby-gemset
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "json-schema"
21
+ spec.add_dependency "json-schema"
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
@@ -15,9 +15,8 @@ module JSON
15
15
  end
16
16
 
17
17
  def self.validate(source, opts={})
18
- json = JSON.parse(source) if source.kind_of?(String)
19
-
20
- JSON::Validator.validate!('lib/json/api/schema.json', json)
18
+ source = JSON.parse(source) if source.kind_of?(String)
19
+ JSON::Validator.validate!(File.expand_path('api/schema.json', File.dirname(__FILE__)), source)
21
20
  end
22
21
  end
23
22
  end
@@ -1,3 +1,42 @@
1
1
  {
2
- "$schema": "http://json-schema.org/draft-04/schema#"
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "title": "JSON API Schema",
4
+ "description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
5
+ "type": "object",
6
+ "patternProperties": {
7
+ "^(?!href$)(?!links$)(?!id$)(?!meta)(?!linked)": {
8
+ "type": "array",
9
+ "items": {
10
+ "type": "object",
11
+ "properties": {
12
+ "links": {
13
+ "type": "object"
14
+ }
15
+ },
16
+ "required": ["id"]
17
+ }
18
+ }
19
+ },
20
+ "properties": {
21
+ "meta": {
22
+ "type": "object"
23
+ },
24
+ "linked": {
25
+ "type": "object",
26
+ "patternProperties": {
27
+ ".*": {
28
+ "type": "array",
29
+ "items": {
30
+ "type": "object",
31
+ "properties": {
32
+ "links": {
33
+ "type": "object"
34
+ }
35
+ },
36
+ "required": ["id"]
37
+ }
38
+ }
39
+ }
40
+ }
41
+ }
3
42
  }
@@ -1,5 +1,5 @@
1
1
  module JSON
2
2
  module Api
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -1,7 +1,7 @@
1
1
  require 'json/api'
2
2
 
3
- class AcceptanceTest < MiniTest::Unit::TestCase
4
- def test_homepage_id_style
3
+ class AcceptanceIdStyleTest < MiniTest::Unit::TestCase
4
+ def test_homepage
5
5
  json = <<-JSON
6
6
  {
7
7
  "posts": [{
@@ -18,7 +18,119 @@ JSON
18
18
  assert JSON::Api.parse(json), "failed to parse"
19
19
  end
20
20
 
21
- def test_homepage_url_style
21
+ def test_singular_resources
22
+ json = <<-JSON
23
+ {
24
+ "posts": [{
25
+ "id": 1
26
+ }]
27
+ }
28
+ JSON
29
+
30
+ assert JSON::Api.parse(json), "failed to parse"
31
+ end
32
+
33
+ def test_multiple_resources
34
+ json = <<-JSON
35
+ {
36
+ "posts": [{
37
+ "id": 1
38
+ },{
39
+ "id": 2
40
+ }]
41
+ }
42
+ JSON
43
+
44
+ assert JSON::Api.parse(json), "failed to parse"
45
+ end
46
+
47
+ def test_attributes
48
+ json = <<-JSON
49
+ {
50
+ "posts": [{
51
+ "id": "1",
52
+ "title": "Rails is Omakase"
53
+ }]
54
+ }
55
+ JSON
56
+
57
+ assert JSON::Api.parse(json), "failed to parse"
58
+ end
59
+
60
+ def test_relationships
61
+ json = <<-JSON
62
+ {
63
+ "posts": [{
64
+ "id": "1",
65
+ "title": "Rails is Omakase",
66
+ "links": {
67
+ "author": "9",
68
+ "comments": [ "5", "12", "17", "20" ]
69
+ }
70
+ }]
71
+ }
72
+ JSON
73
+
74
+ assert JSON::Api.parse(json), "failed to parse"
75
+ end
76
+
77
+ def test_many_to_many_relationships
78
+ json = <<-JSON
79
+ {
80
+ "posts": [{
81
+ "id": "1",
82
+ "title": "Rails is Omakase",
83
+ "links": {
84
+ "comments": [ "5", "12", "17", "20" ]
85
+ }
86
+ }]
87
+ }
88
+ JSON
89
+
90
+ assert JSON::Api.parse(json), "failed to parse"
91
+ end
92
+
93
+ def test_to_one_relationships
94
+ json = <<-JSON
95
+ {
96
+ "posts": [{
97
+ "id": "1",
98
+ "title": "Rails is Omakase",
99
+ "links": {
100
+ "author": "17"
101
+ }
102
+ }]
103
+ }
104
+ JSON
105
+
106
+ assert JSON::Api.parse(json), "failed to parse"
107
+ end
108
+
109
+ def test_compound_documents
110
+ json = <<-JSON
111
+ {
112
+ "posts": [{
113
+ "id": "1",
114
+ "title": "Rails is Omakase",
115
+ "links": {
116
+ "author": "9"
117
+ }
118
+ }],
119
+ "linked": {
120
+ "people": [{
121
+ "id": "9",
122
+ "name": "@d2h"
123
+ }]
124
+ }
125
+ }
126
+ JSON
127
+
128
+ assert JSON::Api.parse(json), "failed to parse"
129
+ end
130
+ end
131
+
132
+ class AcceptanceUrlStyleTest < MiniTest::Unit::TestCase
133
+ def test_homepage
22
134
  json = <<-JSON
23
135
  {
24
136
  "posts": [{
@@ -30,6 +142,370 @@ JSON
30
142
  }
31
143
  }]
32
144
  }
145
+ JSON
146
+
147
+ assert JSON::Api.parse(json), "failed to parse"
148
+ end
149
+
150
+ def test_singular_resource
151
+ json = <<-JSON
152
+ {
153
+ "posts": [{
154
+ "id": 1
155
+ }]
156
+ }
157
+ JSON
158
+
159
+ assert JSON::Api.parse(json), "failed to parse"
160
+ end
161
+
162
+ def test_collections
163
+ json = <<-JSON
164
+ {
165
+ "posts": [{
166
+ "id": 1
167
+ }, {
168
+ "id": 2
169
+ }]
170
+ }
171
+ JSON
172
+
173
+ assert JSON::Api.parse(json), "failed to parse"
174
+ end
175
+
176
+ def test_id
177
+ json = <<-JSON
178
+ {
179
+ "posts": [{
180
+ "id": "1",
181
+ "title": "Rails is Omakase"
182
+ }]
183
+ }
184
+ JSON
185
+
186
+ assert JSON::Api.parse(json), "failed to parse"
187
+ end
188
+
189
+ def test_relationships
190
+ json = <<-JSON
191
+ {
192
+ "posts": [{
193
+ "id": "1",
194
+ "title": "Rails is Omakase",
195
+ "links": {
196
+ "author": "http://example.com/people/1",
197
+ "comments": "http://example.com/comments/5,12,17,20"
198
+ }
199
+ }]
200
+ }
201
+ JSON
202
+
203
+ assert JSON::Api.parse(json), "failed to parse"
204
+ end
205
+
206
+ def test_many_to_many
207
+ json = <<-JSON
208
+ {
209
+ "posts": [{
210
+ "id": "1",
211
+ "title": "Rails is Omakase",
212
+ "links": {
213
+ "comments": "http://example.com/posts/1/comments"
214
+ }
215
+ }]
216
+ }
217
+ JSON
218
+
219
+ assert JSON::Api.parse(json), "failed to parse"
220
+ end
221
+
222
+ def test_to_one
223
+ json = <<-JSON
224
+ {
225
+ "posts": [{
226
+ "id": "1",
227
+ "title": "Rails is Omakase",
228
+ "links": {
229
+ "author": "http://example.com/people/17"
230
+ }
231
+ }]
232
+ }
233
+ JSON
234
+
235
+ assert JSON::Api.parse(json), "failed to parse"
236
+ end
237
+
238
+ def test_url_template
239
+ json = <<-JSON
240
+ {
241
+ "links": {
242
+ "posts.comments": "http://example.com/posts/{posts.id}/comments"
243
+ },
244
+ "posts": [{
245
+ "id": "1",
246
+ "title": "Rails is Omakase"
247
+ }, {
248
+ "id": "2",
249
+ "title": "The Parley Letter"
250
+ }]
251
+ }
252
+ JSON
253
+
254
+ assert JSON::Api.parse(json), "failed to parse"
255
+ end
256
+
257
+ def test_url_template_2
258
+ json = <<-JSON
259
+ {
260
+ "links": {
261
+ "posts.comments": "http://example.com/comments/{posts.comments}"
262
+ },
263
+ "posts": [{
264
+ "id": "1",
265
+ "title": "Rails is Omakase",
266
+ "links": {
267
+ "comments": [ "1", "2", "3", "4" ]
268
+ }
269
+ }]
270
+ }
271
+ JSON
272
+
273
+ assert JSON::Api.parse(json), "failed to parse"
274
+ end
275
+
276
+ def test_url_template_3
277
+ json = <<-JSON
278
+ {
279
+ "links": {
280
+ "posts.author": "http://example.com/people/{posts.author}"
281
+ },
282
+ "posts": [{
283
+ "id": "1",
284
+ "title": "Rails is Omakase",
285
+ "links": {
286
+ "author": "12"
287
+ }
288
+ }, {
289
+ "id": "2",
290
+ "title": "The Parley Letter",
291
+ "links": {
292
+ "author": "12"
293
+ }
294
+ }, {
295
+ "id": "3",
296
+ "title": "Dependency Injection is Not a Virtue",
297
+ "links": {
298
+ "author": "12"
299
+ }
300
+ }]
301
+ }
302
+ JSON
303
+
304
+ assert JSON::Api.parse(json), "failed to parse"
305
+ end
306
+
307
+ def test_compound_documents
308
+ json = <<-JSON
309
+ {
310
+ "links": {
311
+ "posts.author": {
312
+ "href": "http://example.com/people/{posts.author}",
313
+ "type": "people"
314
+ },
315
+ "posts.comments": {
316
+ "href": "http://example.com/comments/{posts.comments}",
317
+ "type": "comments"
318
+ }
319
+ },
320
+ "posts": [{
321
+ "id": "1",
322
+ "title": "Rails is Omakase",
323
+ "links": {
324
+ "author": "9",
325
+ "comments": [ "1", "2", "3" ]
326
+ }}, {
327
+ "id": "2",
328
+ "title": "The Parley Letter",
329
+ "links": {
330
+ "author": "9",
331
+ "comments": [ "4", "5" ]
332
+ }}, {
333
+ "id": "1",
334
+ "title": "Dependency Injection is Not a Virtue",
335
+ "links": {
336
+ "author": "9",
337
+ "comments": [ "6" ]
338
+ }
339
+ }],
340
+ "linked": {
341
+ "people": [{
342
+ "id": "9",
343
+ "name": "@d2h"
344
+ }],
345
+ "comments": [{
346
+ "id": "1",
347
+ "body": "Mmmmmakase"
348
+ }, {
349
+ "id": "2",
350
+ "body": "I prefer unagi"
351
+ }, {
352
+ "id": "3",
353
+ "body": "What's Omakase?"
354
+ }, {
355
+ "id": "4",
356
+ "body": "Parley is a discussion, especially one between enemies"
357
+ }, {
358
+ "id": "5",
359
+ "body": "The parsley letter"
360
+ }, {
361
+ "id": "6",
362
+ "body": "Dependency Injection is Not a Vice"
363
+ }]
364
+ }
365
+ }
366
+ JSON
367
+
368
+ assert JSON::Api.parse(json), "failed to parse"
369
+ end
370
+
371
+ def test_compound_documents_2
372
+ json = <<-JSON
373
+ {
374
+ "links": {
375
+ "posts.author": {
376
+ "href": "http://example.com/people/{posts.author}",
377
+ "type": "people"
378
+ },
379
+ "posts.comments": {
380
+ "href": "http://example.com/comments/{posts.comments}",
381
+ "type": "comments"
382
+ }
383
+ },
384
+ "posts": [{
385
+ "id": "1",
386
+ "title": "Rails is Omakase",
387
+ "links": {
388
+ "author": "9",
389
+ "comments": [ "1", "2", "3" ]
390
+ }}, {
391
+ "id": "2",
392
+ "title": "The Parley Letter",
393
+ "links": {
394
+ "author": "9",
395
+ "comments": [ "4", "5" ]
396
+ }}, {
397
+ "id": "1",
398
+ "title": "Dependency Injection is Not a Virtue",
399
+ "links": {
400
+ "author": "9",
401
+ "comments": [ "6" ]
402
+ }
403
+ }],
404
+ "linked": {
405
+ "people": [{
406
+ "id": "9",
407
+ "name": "@d2h"
408
+ }],
409
+ "comments": [{
410
+ "href": "http://example.com/comments/1",
411
+ "id": "1",
412
+ "body": "Mmmmmakase"
413
+ }, {
414
+ "href": "http://example.com/comments/2",
415
+ "id": "2",
416
+ "body": "I prefer unagi"
417
+ }, {
418
+ "href": "http://example.com/comments/3",
419
+ "id": "3",
420
+ "body": "What's Omakase?"
421
+ }, {
422
+ "href": "http://example.com/comments/4",
423
+ "id": "4",
424
+ "body": "Parley is a discussion, especially one between enemies"
425
+ }, {
426
+ "href": "http://example.com/comments/5",
427
+ "id": "5",
428
+ "body": "The parsley letter"
429
+ }, {
430
+ "href": "http://example.com/comments/6",
431
+ "id": "6",
432
+ "body": "Dependency Injection is Not a Vice"
433
+ }]
434
+ }
435
+ }
436
+ JSON
437
+
438
+ assert JSON::Api.parse(json), "failed to parse"
439
+ end
440
+ end
441
+
442
+ class AcceptanceResponseTest < MiniTest::Unit::TestCase
443
+ def test_created
444
+ json = <<-JSON
445
+ {
446
+ "photos": [{
447
+ "id": "550e8400-e29b-41d4-a716-446655440000",
448
+ "href": "http://example.com/photos/12",
449
+ "title": "Ember Hamster",
450
+ "src": "http://example.com/images/productivity.png"
451
+ }]
452
+ }
453
+ JSON
454
+
455
+ assert JSON::Api.parse(json), "failed to parse"
456
+ end
457
+
458
+ def test_patch_1
459
+ json = <<-JSON
460
+ {
461
+ "photos": [{
462
+ "id": "1",
463
+ "title": "Productivity",
464
+ "src": "http://example.com/productivity.png"
465
+ }]
466
+ }
467
+ JSON
468
+
469
+ assert JSON::Api.parse(json), "failed to parse"
470
+ end
471
+
472
+ def test_patch_2
473
+ json = <<-JSON
474
+ {
475
+ "links": {
476
+ "photos.author": "http://example.com/people/{photos.author}"
477
+ },
478
+ "photos": [{
479
+ "id": "1",
480
+ "href": "http://example.com/photos/1",
481
+ "title": "Hamster",
482
+ "src": "images/hamster.png",
483
+ "links": {
484
+ "author": "1"
485
+ }
486
+ }]
487
+ }
488
+ JSON
489
+
490
+ assert JSON::Api.parse(json), "failed to parse"
491
+ end
492
+
493
+ def test_patch_3
494
+ json = <<-JSON
495
+ {
496
+ "links": {
497
+ "photos.comments": "http://example.com/comments/{photos.comments}"
498
+ },
499
+ "photos": [{
500
+ "id": "1",
501
+ "href": "http://example.com/photos/1",
502
+ "title": "Hamster",
503
+ "src": "images/hamster.png",
504
+ "links": {
505
+ "comments": [ "1", "5", "12", "17" ]
506
+ }
507
+ }]
508
+ }
33
509
  JSON
34
510
 
35
511
  assert JSON::Api.parse(json), "failed to parse"
@@ -0,0 +1,20 @@
1
+ require 'json/api'
2
+
3
+ class SchemaTest < MiniTest::Unit::TestCase
4
+ def test_homepage_id_style
5
+ json = <<-JSON
6
+ {
7
+ "posts": [{
8
+ "id": "1",
9
+ "title": "Rails is Omakase",
10
+ "links": {
11
+ "author": "9",
12
+ "comments": [ "5", "12", "17", "20" ]
13
+ }
14
+ }]
15
+ }
16
+ JSON
17
+
18
+ assert JSON::Api.parse(json), "failed to parse"
19
+ end
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Klabnik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-10 00:00:00.000000000 Z
11
+ date: 2013-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -17,7 +17,7 @@ dependencies:
17
17
  - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
- type: :development
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
@@ -69,6 +69,7 @@ files:
69
69
  - lib/json/api/schema.json
70
70
  - lib/json/api/version.rb
71
71
  - test/acceptance_test.rb
72
+ - test/schema_test.rb
72
73
  - test/test_helper.rb
73
74
  homepage: http://jsonapi.org
74
75
  licenses:
@@ -96,4 +97,5 @@ specification_version: 4
96
97
  summary: A JSON API parser.
97
98
  test_files:
98
99
  - test/acceptance_test.rb
100
+ - test/schema_test.rb
99
101
  - test/test_helper.rb