json-api-vanilla 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c5760a75bcea2f2870e2641f246b36bd63ffcdb9
4
+ data.tar.gz: 8208a9f5cd8089bf6c356e4548c3d22c4ceea73a
5
+ SHA512:
6
+ metadata.gz: 6c30f227cd52a825173bcfc39ca3f319c9d3796e812542611482e2a29ed65cd4a9427d537453498524891c669abbcfad6d7b5490708784831be17f1bc27062af
7
+ data.tar.gz: aa5d645da7d059f6bb1f436093b6bb3161174f777b8485eac56e3de5fda5700700f84bc77f40acf14580b25ef155181bb680478f5e766a483ec19ad1b3c9acc6
@@ -0,0 +1,4 @@
1
+ Gemfile.lock
2
+ .ruby-version
3
+ .rvmrc
4
+ *.gem
@@ -0,0 +1,17 @@
1
+ Contributor Agreement
2
+ Thank you for your interest in Trainline. In order to clarify the intellectual property license granted with code contributions from any person or entity, Trainline.com Limited ("Trainline") requires a Contributor License Agreement on file that has been signed by each contributor, indicating agreement to the license terms below. This license is for your protection as a Contributor as well as the protection of Trainline and its users; it does not change your rights to use your own contributions for any other purpose.
3
+ Please send confirmation that you accept the terms set out below to osscontribs@thetrainline.com. Please read the terms carefully before accepting and keep a copy for your records.
4
+ You accept and agree to the following terms and conditions for Your present and future Contributions submitted to Trainline. Except for the license granted herein to Trainline and recipients of software distributed by Trainline, You reserve all right, title, and interest in and to Your Contributions.
5
+
6
+ 1. Definitions.
7
+ "You" (or "Your") shall mean the copyright owner or legal entity authorized by the copyright owner that is making this Agreement with Trainline. For legal entities, the entity making a Contribution and all other entities that control, are controlled by, or are under common control with that entity are considered to be a single Contributor. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty per cent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
8
+ "Contribution" shall mean any original work of authorship, including any modifications or additions to an existing work, that is intentionally submitted by You to Trainline for inclusion in, or documentation of, any of the products owned or managed by Trainline (the "Work"). For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to Trainline or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, Trainline for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by You as "Not a Contribution."
9
+ 2. Grant of Copyright License. Subject to the terms and conditions of this Agreement, You hereby grant to Trainline and to recipients of software distributed by Trainline a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute Your Contributions and such derivative works.
10
+ 3. Grant of Patent License. Subject to the terms and conditions of this Agreement, You hereby grant to Trainline and to recipients of software distributed by Trainline a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by You that are necessarily infringed by Your Contribution(s) alone or by combination of Your Contribution(s) with the Work to which such Contribution(s) was submitted. If any entity institutes patent litigation against You or any other entity (including a cross-claim or counterclaim in a lawsuit) alleging that your Contribution, or the Work to which you have contributed, constitutes direct or contributory patent infringement, then any patent licenses granted to that entity under this Agreement for that Contribution or Work shall terminate as of the date such litigation is filed.
11
+ 4. You represent that you are legally entitled to grant the above license. If your employer(s) has rights to intellectual property that you create that includes your Contributions, you represent that you have received permission to make Contributions on behalf of that employer, that your employer has waived such rights for your Contributions to Trainline, or that your employer has executed a separate agreement with Trainline.
12
+ 5. You represent that each of Your Contributions is Your original creation (see section 7 for submissions on behalf of others). You represent that Your Contribution submissions include complete details of any third-party license or other restriction (including, but not limited to, related patents and trademarks) of which you are personally aware and which are associated with any part of Your Contributions.
13
+ 6. You are not expected to provide support for Your Contributions, except to the extent You desire to provide support. You may provide support for free, for a fee, or not at all. Unless required by applicable law or agreed to in writing, You provide Your Contributions on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
14
+ 7. Should You wish to submit work that is not Your original creation, You may submit it to Trainline separately from any Contribution, identifying the complete details of its source and of any license or other restriction (including, but not limited to, related patents, trademarks, and license agreements) of which you are personally aware, and conspicuously marking the work as "Submitted on behalf of a third-party: [named here]".
15
+ 8. You agree to notify Trainline of any facts or circumstances of which you become aware that would make these representations inaccurate in any respect.
16
+
17
+ Please sign: __________________________________ Date: ________________
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+ gemspec
3
+
4
+ group :test do
5
+ gem 'rake'
6
+ end
@@ -0,0 +1,13 @@
1
+ Copyright 2016 Trainline.com Ltd
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,5 @@
1
+ # Copyright © Trainline Limited, 2016. All rights reserved. See LICENSE.txt in the project root for license information.
2
+ install:
3
+ gem build json-api-vanilla.gemspec && gem install ./json-api-vanilla-*.gem
4
+
5
+ .PHONY: install
File without changes
@@ -0,0 +1,51 @@
1
+ # JSON API *VANILLA*
2
+
3
+ Deserialize JSON API formats into *vanilla* Ruby objects.
4
+ The simplest JSON API library at all altitudes above Earth centre.
5
+
6
+ ```ruby
7
+ # gem install json-api-vanilla
8
+ require "json-api-vanilla"
9
+ json = IO.read("articles.json") # From http://jsonapi.org
10
+ doc = JSON::Api::Vanilla.parse(json)
11
+ doc.data[0].comments[1].author.last_name # "Gebhardt"
12
+ ```
13
+
14
+ Compare with [jsonapi](https://github.com/beauby/jsonapi):
15
+
16
+ ```ruby
17
+ # gem install jsonapi --pre
18
+ require "jsonapi"
19
+ json = IO.read("articles.json")
20
+ doc = JSONAPI.parse(json)
21
+ comment_ref = doc.data[0].relationships.comments.data[1]
22
+ comment = doc.included.select do |obj|
23
+ obj.type == comment_ref.type && obj.id == comment_ref.id
24
+ end[0]
25
+ author_ref = comment.relationships.author.data
26
+ author = doc.included.select do |obj|
27
+ obj.type == author_ref.type && obj.id == author_ref.id
28
+ end[0]
29
+ author.attributes['last-name']
30
+ ```
31
+
32
+ # Documentation
33
+
34
+ `JSON::Api::Vanilla.parse(json_string)` returns a document with the following
35
+ fields:
36
+
37
+ - `data` is an object corresponding to the JSON API's data object.
38
+ - `errors` is an array containing [errors](http://jsonapi.org/format/#error-objects). Each error is a Hash.
39
+ - `links` is a Hash from objects (obtained from `data`) to their links, as a
40
+ Hash.
41
+ - `rel_links` is a Hash from objects' relationships (obtained from `data`) to
42
+ the links defined in that relationship, as a Hash.
43
+ - `meta` is a Hash from objects to their meta information (a Hash).
44
+ - `find('type', 'id')` returns the object with that type and that id.
45
+ - `find_all('type')` returns an Array of all objects with that type.
46
+ - `keys` is a Hash from objects to a Hash from their original field names
47
+ (non-snake\_case'd) to the corresponding object.
48
+
49
+ # License
50
+
51
+ Copyright © Trainline.com Limited. All rights reserved. See LICENSE.txt in the project root for license information.
@@ -0,0 +1,10 @@
1
+ # Copyright © Trainline Limited, 2016. All rights reserved. See LICENSE.txt in the project root for license information.
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require 'bundler'
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ task default: :spec
@@ -0,0 +1,18 @@
1
+ Contributor Agreement
2
+ Thank you for your interest in Trainline. In order to clarify the intellectual property license granted with code contributions from any person or entity, Trainline.com Limited ("Trainline") requires a Contributor License Agreement on file that has been signed by each contributor, indicating agreement to the license terms below. This license is for your protection as a Contributor as well as the protection of Trainline and its users; it does not change your rights to use your own contributions for any other purpose.
3
+ Please send confirmation that you accept the terms set out below to osscontribs@thetrainline.com. Please read the terms carefully before accepting and keep a copy for your records.
4
+ You accept and agree to the following terms and conditions for Your present and future Contributions submitted to Trainline. Except for the license granted herein to Trainline and recipients of software distributed by Trainline, You reserve all right, title, and interest in and to Your Contributions.
5
+
6
+ 1. Definitions.
7
+ "You" (or "Your") shall mean the copyright owner or legal entity authorized by the copyright owner that is making this Agreement with Trainline. For legal entities, the entity making a Contribution and all other entities that control, are controlled by, or are under common control with that entity are considered to be a single Contributor. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty per cent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
8
+ "Contribution" shall mean any original work of authorship, including any modifications or additions to an existing work, that is intentionally submitted by You to Trainline for inclusion in, or documentation of, any of the products owned or managed by Trainline (the "Work"). For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to Trainline or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, Trainline for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by You as "Not a Contribution."
9
+ 2. Grant of Copyright License. Subject to the terms and conditions of this Agreement, You hereby grant to Trainline and to recipients of software distributed by Trainline a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute Your Contributions and such derivative works.
10
+ 3. Grant of Patent License. Subject to the terms and conditions of this Agreement, You hereby grant to Trainline and to recipients of software distributed by Trainline a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by You that are necessarily infringed by Your Contribution(s) alone or by combination of Your Contribution(s) with the Work to which such Contribution(s) was submitted. If any entity institutes patent litigation against You or any other entity (including a cross-claim or counterclaim in a lawsuit) alleging that your Contribution, or the Work to which you have contributed, constitutes direct or contributory patent infringement, then any patent licenses granted to that entity under this Agreement for that Contribution or Work shall terminate as of the date such litigation is filed.
11
+ 4. You represent that you are legally entitled to grant the above license. If your employer(s) has rights to intellectual property that you create that includes your Contributions, you represent that you have received permission to make Contributions on behalf of that employer, that your employer has waived such rights for your Contributions to Trainline, or that your employer has executed a separate agreement with Trainline.
12
+ 5. You represent that each of Your Contributions is Your original creation (see section 7 for submissions on behalf of others). You represent that Your Contribution submissions include complete details of any third-party license or other restriction (including, but not limited to, related patents and trademarks) of which you are personally aware and which are associated with any part of Your Contributions.
13
+ 6. You are not expected to provide support for Your Contributions, except to the extent You desire to provide support. You may provide support for free, for a fee, or not at all. Unless required by applicable law or agreed to in writing, You provide Your Contributions on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
14
+ 7. Should You wish to submit work that is not Your original creation, You may submit it to Trainline separately from any Contribution, identifying the complete details of its source and of any license or other restriction (including, but not limited to, related patents, trademarks, and license agreements) of which you are personally aware, and conspicuously marking the work as "Submitted on behalf of a third-party: [named here]".
15
+ 8. You agree to notify Trainline of any facts or circumstances of which you become aware that would make these representations inaccurate in any respect.
16
+
17
+ Please sign: Tadas Tamošauskas
18
+ Date: 2016-10-06
@@ -0,0 +1,16 @@
1
+ # Copyright © Trainline Limited, 2016. All rights reserved. See LICENSE.txt in the project root for license information.
2
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
3
+ require 'json-api-vanilla/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'json-api-vanilla'
7
+ s.license = 'Apache-2.0'
8
+ s.version = JSON::Api::Vanilla::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ['Thaddée Tyl']
11
+ s.email = ['thaddee.tyl@gmail.com']
12
+ s.homepage = 'http://github.com/trainline/json-api-vanilla'
13
+ s.summary = %q{Deserialize JSON API formats into vanilla Ruby objects.}
14
+ s.description = %q{Given a JSON API string, we parse it and return a document that can be browsed — as if the objects defined in the file were plain old Ruby objects.}
15
+ s.files = `git ls-files`.split("\n")
16
+ end
@@ -0,0 +1,3 @@
1
+ # Copyright © Trainline Limited, 2016. All rights reserved. See LICENSE.txt in the project root for license information.
2
+ require 'json-api-vanilla/parser'
3
+ require 'json-api-vanilla/version'
@@ -0,0 +1,235 @@
1
+ # Copyright © Trainline Limited, 2016. All rights reserved. See LICENSE.txt in the project root for license information.
2
+ require "json"
3
+
4
+ module JSON::Api; end
5
+ module JSON::Api::Vanilla
6
+ class InvalidRootStructure < StandardError; end
7
+
8
+ # Convert a String JSON API payload to vanilla Ruby objects.
9
+ #
10
+ # Example:
11
+ # >> json = IO.read("articles.json") # From http://jsonapi.org
12
+ # >> doc = JSON::Api::Vanilla.parse(json)
13
+ # >> doc.data[0].comments[1].author.last_name
14
+ # => "Gebhardt"
15
+ #
16
+ # @param json [String] the JSON API payload.
17
+ # @return [JSON::Api::Vanilla::Document] a wrapper for the objects.
18
+ def self.parse(json)
19
+ hash = JSON.parse(json)
20
+ build(hash)
21
+ end
22
+
23
+ # Convert a ruby hash JSON API representation to vanilla Ruby objects.
24
+ # Similar to .parse but takes hash as a parameter.
25
+ #
26
+ # Example:
27
+ # >> hash = { errors: [{ source: { pointer: "" }, detail: "Missing `data` Member at document's top level." }]}
28
+ # >> doc = JSON::Api::Vanilla.build(hash)
29
+ # >> doc.errors.first["detail"]
30
+ # => "Missing `data` Member at document's top level."
31
+ #
32
+ # @param hash [Hash] parsed JSON API payload.
33
+ # @return [JSON::Api::Vanilla::Document] a wrapper for the objects.
34
+ def self.build(hash)
35
+ naive_validate(hash)
36
+ # Object storage.
37
+ container = Module.new
38
+ superclass = Class.new
39
+
40
+ data_hash = hash['data']
41
+ data_hash_array = if data_hash.is_a?(Array)
42
+ data_hash
43
+ else
44
+ [data_hash].compact
45
+ end
46
+ obj_hashes = (hash['included'] || []) + data_hash_array
47
+ errors = hash['errors']
48
+
49
+ # Create all the objects.
50
+ # Store them in the `objects` hash from [type, id] to the object.
51
+ objects = {}
52
+ links = {} # Object links.
53
+ rel_links = {} # Relationship links.
54
+ meta = {} # Meta information.
55
+ # Map from objects to map from keys to values, for use when two keys are
56
+ # converted to the same ruby method identifier.
57
+ original_keys = {}
58
+
59
+ obj_hashes.each do |o_hash|
60
+ klass = prepare_class(o_hash, superclass, container)
61
+ obj = klass.new
62
+ obj.type = o_hash['type']
63
+ obj.id = o_hash['id']
64
+ if o_hash['attributes']
65
+ o_hash['attributes'].each do |key, value|
66
+ set_key(obj, key, value, original_keys)
67
+ end
68
+ end
69
+ if o_hash['links']
70
+ links[obj] = o_hash['links']
71
+ end
72
+ objects[[obj.type, obj.id]] = obj
73
+ end
74
+
75
+ # Now that all objects have been created, we can link everything together.
76
+ obj_hashes.each do |o_hash|
77
+ klass = container.const_get(ruby_class_name(o_hash['type']).to_sym)
78
+ obj = objects[[o_hash['type'], o_hash['id']]]
79
+ if o_hash['relationships']
80
+ o_hash['relationships'].each do |key, value|
81
+ if value['data']
82
+ data = value['data']
83
+ if data.is_a?(Array)
84
+ # One-to-many relationship.
85
+ ref = data.map do |ref_hash|
86
+ objects[[ref_hash['type'], ref_hash['id']]]
87
+ end
88
+ else
89
+ ref = objects[[data['type'], data['id']]]
90
+ end
91
+ end
92
+
93
+ ref = ref || Object.new
94
+ set_key(obj, key, ref, original_keys)
95
+
96
+ rel_links[ref] = value['links']
97
+ meta[ref] = value['meta']
98
+ end
99
+ end
100
+ end
101
+
102
+ # Create the main object.
103
+ data = if data_hash.is_a?(Array)
104
+ data_hash.map do |o_hash|
105
+ objects[[o_hash['type'], o_hash['id']]]
106
+ end
107
+ elsif data_hash
108
+ objects[[data_hash['type'], data_hash['id']]]
109
+ end
110
+ links[data] = hash['links']
111
+ meta[data] = hash['meta']
112
+ Document.new(data, links: links, rel_links: rel_links, meta: meta,
113
+ objects: objects, keys: original_keys, errors: errors,
114
+ container: container, superclass: superclass)
115
+ end
116
+
117
+ def self.prepare_class(hash, superclass, container)
118
+ name = ruby_class_name(hash['type']).to_sym
119
+ if container.const_defined?(name)
120
+ klass = container.const_get(name)
121
+ else
122
+ klass = generate_object(name, superclass, container)
123
+ end
124
+ add_accessor(klass, 'id')
125
+ add_accessor(klass, 'type')
126
+ attr_keys = hash['attributes'] ? hash['attributes'].keys : []
127
+ rel_keys = hash['relationships'] ? hash['relationships'].keys : []
128
+ (attr_keys + rel_keys).each do |key|
129
+ add_accessor(klass, key)
130
+ end
131
+ klass
132
+ end
133
+
134
+ def self.generate_object(ruby_name, superclass, container)
135
+ klass = Class.new(superclass)
136
+ container.const_set(ruby_name, klass)
137
+ klass
138
+ end
139
+
140
+ def self.add_accessor(klass, name)
141
+ ruby_name = ruby_ident_name(name)
142
+ if !klass.method_defined?(ruby_name)
143
+ klass.send(:attr_accessor, ruby_name)
144
+ end
145
+ end
146
+
147
+ # Set a value to an object's key through its setter.
148
+ # original_keys is a map from objects to a map from String keys to their
149
+ # values.
150
+ def self.set_key(obj, key, value, original_keys)
151
+ ruby_key = ruby_ident_name(key)
152
+ obj.send("#{ruby_key}=", value)
153
+ original_keys[obj] ||= {}
154
+ original_keys[obj][key] = value
155
+ end
156
+
157
+ # Convert a name String to a String that is a valid Ruby class name.
158
+ def self.ruby_class_name(name)
159
+ name.scan(/[a-zA-Z_][a-zA-Z_0-9]+/).map(&:capitalize).join
160
+ end
161
+
162
+ # Convert a name String to a String that is a valid snake-case Ruby
163
+ # identifier.
164
+ def self.ruby_ident_name(name)
165
+ name.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
166
+ .gsub(/([a-z\d])([A-Z])/,'\1_\2')
167
+ .tr("-", "_")
168
+ .downcase
169
+ end
170
+
171
+ # Naïvely validate the top level document structure
172
+ # @param hash [Hash] json:api document as a hash
173
+ # @raise [InvalidRootStructure] raised if the document doesn't have
174
+ # data, errors nor meta objects at its root.
175
+ def self.naive_validate(hash)
176
+ root_keys = %w(data errors meta)
177
+ present_structures = root_keys.map do |key|
178
+ obj = hash[key]
179
+ obj.respond_to?(:empty?) ? !obj.empty? : !!obj
180
+ end
181
+ if present_structures.none?
182
+ raise InvalidRootStructure.new("JSON:API document must contain at least one of these objects: #{root_keys.join(', ')}")
183
+ end
184
+ end
185
+
186
+ class Document
187
+ # @return [Object, Array<Object>] the content of the JSON API data.
188
+ attr_reader :data
189
+ # @return [Hash] a map from objects (obtained from .data) to their links,
190
+ # as a Hash.
191
+ attr_reader :links
192
+ # @return [Hash] a map from objects' relationships (obtained from .data)
193
+ # to the links defined in that relationship, as a Hash.
194
+ attr_reader :rel_links
195
+ # @return [Hash] a map from objects to their meta information (a Hash).
196
+ attr_reader :meta
197
+ # @return [Array] a list of errors, if any, otherwise nil.
198
+ attr_reader :errors
199
+ # @return [Hash] a map from objects to a Hash from their original field
200
+ # names (non-snake_case'd) to the corresponding object.
201
+ attr_reader :keys
202
+ attr_reader :container
203
+ attr_reader :superclass
204
+ def initialize(data, links: {}, rel_links: {}, meta: {},
205
+ keys: {}, objects: {}, errors: [],
206
+ container: Module.new, superclass: Class.new)
207
+ @data = data
208
+ @links = links
209
+ @rel_links = rel_links
210
+ @meta = meta
211
+ @keys = keys
212
+ @objects = objects
213
+ @errors = errors
214
+ @container = container
215
+ @superclass = superclass
216
+ end
217
+
218
+ # Get a JSON API object.
219
+ #
220
+ # @param type [String] the type of the object we want returned.
221
+ # @param id [String] its id.
222
+ # @return [Object] the object with that type and id.
223
+ def find(type, id)
224
+ @objects[[type, id]]
225
+ end
226
+
227
+ # Get all JSON API objects of a given type.
228
+ #
229
+ # @param type [String] the type of the objects we want returned.
230
+ # @return [Array<Object>] the list of objects with that type.
231
+ def find_all(type)
232
+ @objects.values.select { |obj| obj.type == type }
233
+ end
234
+ end
235
+ end
@@ -0,0 +1,8 @@
1
+ # Copyright © Trainline Limited, 2016. All rights reserved. See LICENSE.txt in the project root for license information.
2
+ module JSON
3
+ module Api
4
+ module Vanilla
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,88 @@
1
+ # Copyright © Trainline Limited, 2016. All rights reserved. See LICENSE.txt in the project root for license information.
2
+ require 'spec_helper'
3
+
4
+ describe JSON::Api::Vanilla do
5
+ let(:doc) { JSON::Api::Vanilla.parse(IO.read("#{__dir__}/example.json")) }
6
+
7
+ it "should cross arrays and fields of objects" do
8
+ expect(doc.data[0].comments[1].author.last_name).to eql("Gebhardt")
9
+ end
10
+
11
+ it "should read relationship links" do
12
+ expect(doc.rel_links[doc.data[0].comments]['related']).to eql("http://example.com/articles/1/comments")
13
+ end
14
+
15
+ it "should read object links" do
16
+ expect(doc.links[doc.data[0].author]['self']).to eql("http://example.com/people/9")
17
+ end
18
+
19
+ it "should read links at the root" do
20
+ expect(doc.links[doc.data]['self']).to eql("http://example.com/articles")
21
+ end
22
+
23
+ it "should find objects by type and id" do
24
+ expect(doc.find('comments', '5').body).to eql("First!")
25
+ end
26
+
27
+ it "should find all objects given a type" do
28
+ expect(doc.find_all('comments').size).to eql(2)
29
+ end
30
+
31
+ it "should give access to data through the original key" do
32
+ expect(doc.keys[doc.find('people', '9')]['first-name']).to eql("Dan")
33
+ end
34
+
35
+ it "should give access to meta information" do
36
+ expect(doc.meta[doc.data]['from']).to eql("http://jsonapi.org")
37
+ end
38
+
39
+ it "should support reference cycles" do
40
+ json = <<-JSON
41
+ {
42
+ "data": {
43
+ "type": "cycle",
44
+ "id": "1",
45
+ "relationships": { "cycle": { "data": { "type": "cycle", "id": "2" } } }
46
+ },
47
+ "included": [{
48
+ "type": "cycle",
49
+ "id": "2",
50
+ "attributes": { "body": "content" },
51
+ "relationships": { "cycle": { "data": { "type": "cycle", "id": "2" } } }
52
+ }]
53
+ }
54
+ JSON
55
+ doc = JSON::Api::Vanilla.parse(json)
56
+ expect(doc.data.cycle.cycle.cycle.body).to eql("content")
57
+ end
58
+
59
+ it "should support errors when present" do
60
+ json = <<-JSON
61
+ {
62
+ "errors": [{
63
+ "status": "400",
64
+ "detail": "JSON parse error - Expecting property name at line 1 column 2 (char 1)."
65
+ }]
66
+ }
67
+ JSON
68
+ doc = JSON::Api::Vanilla.parse(json)
69
+ expect(doc.errors.size).to eql(1)
70
+ expect(doc.errors.first["status"]).to eql("400")
71
+ expect(doc.errors.first["detail"]).to eql("JSON parse error - Expecting property name at line 1 column 2 (char 1).")
72
+ end
73
+
74
+ it "should return nil for errors when there are no errors" do
75
+ expect(doc.errors).to be_nil
76
+ end
77
+
78
+ it "should raise an error if the document does not contain required root elements" do
79
+ json = <<-JSON
80
+ {
81
+ "jsonapi": { "version": "1" }
82
+ }
83
+ JSON
84
+ expect do
85
+ JSON::Api::Vanilla.parse(json)
86
+ end.to raise_error(JSON::Api::Vanilla::InvalidRootStructure)
87
+ end
88
+ end
@@ -0,0 +1,79 @@
1
+ {
2
+ "meta": {
3
+ "from": "http://jsonapi.org"
4
+ },
5
+ "links": {
6
+ "self": "http://example.com/articles",
7
+ "next": "http://example.com/articles?page[offset]=2",
8
+ "last": "http://example.com/articles?page[offset]=10"
9
+ },
10
+ "data": [{
11
+ "type": "articles",
12
+ "id": "1",
13
+ "attributes": {
14
+ "title": "JSON API paints my bikeshed!"
15
+ },
16
+ "relationships": {
17
+ "author": {
18
+ "links": {
19
+ "self": "http://example.com/articles/1/relationships/author",
20
+ "related": "http://example.com/articles/1/author"
21
+ },
22
+ "data": { "type": "people", "id": "9" }
23
+ },
24
+ "comments": {
25
+ "links": {
26
+ "self": "http://example.com/articles/1/relationships/comments",
27
+ "related": "http://example.com/articles/1/comments"
28
+ },
29
+ "data": [
30
+ { "type": "comments", "id": "5" },
31
+ { "type": "comments", "id": "12" }
32
+ ]
33
+ }
34
+ },
35
+ "links": {
36
+ "self": "http://example.com/articles/1"
37
+ }
38
+ }],
39
+ "included": [{
40
+ "type": "people",
41
+ "id": "9",
42
+ "attributes": {
43
+ "first-name": "Dan",
44
+ "last-name": "Gebhardt",
45
+ "twitter": "dgeb"
46
+ },
47
+ "links": {
48
+ "self": "http://example.com/people/9"
49
+ }
50
+ }, {
51
+ "type": "comments",
52
+ "id": "5",
53
+ "attributes": {
54
+ "body": "First!"
55
+ },
56
+ "relationships": {
57
+ "author": {
58
+ "data": { "type": "people", "id": "2" }
59
+ }
60
+ },
61
+ "links": {
62
+ "self": "http://example.com/comments/5"
63
+ }
64
+ }, {
65
+ "type": "comments",
66
+ "id": "12",
67
+ "attributes": {
68
+ "body": "I like XML better"
69
+ },
70
+ "relationships": {
71
+ "author": {
72
+ "data": { "type": "people", "id": "9" }
73
+ }
74
+ },
75
+ "links": {
76
+ "self": "http://example.com/comments/12"
77
+ }
78
+ }]
79
+ }
@@ -0,0 +1,5 @@
1
+ # Copyright © Trainline Limited, 2016. All rights reserved. See LICENSE.txt in the project root for license information.
2
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
3
+
4
+ require 'rubygems'
5
+ require 'json-api-vanilla'
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json-api-vanilla
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Thaddée Tyl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Given a JSON API string, we parse it and return a document that can be
14
+ browsed — as if the objects defined in the file were plain old Ruby objects.
15
+ email:
16
+ - thaddee.tyl@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ContributorAgreement.txt
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - Makefile
26
+ - NOTICE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - contributors/TadasTamosauskas.txt
30
+ - json-api-vanilla.gemspec
31
+ - lib/json-api-vanilla.rb
32
+ - lib/json-api-vanilla/parser.rb
33
+ - lib/json-api-vanilla/version.rb
34
+ - spec/json-api-vanilla/diff_spec.rb
35
+ - spec/json-api-vanilla/example.json
36
+ - spec/spec_helper.rb
37
+ homepage: http://github.com/trainline/json-api-vanilla
38
+ licenses:
39
+ - Apache-2.0
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.5.1
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Deserialize JSON API formats into vanilla Ruby objects.
61
+ test_files: []