json_api_ruby 0.0.1.alpha
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 +7 -0
- data/.gitignore +1 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +92 -0
- data/Guardfile +69 -0
- data/Rakefile +1 -0
- data/json_api_ruby.gemspec +27 -0
- data/lib/json_api_ruby/configuration.rb +15 -0
- data/lib/json_api_ruby/error_resource.rb +11 -0
- data/lib/json_api_ruby/exceptions.rb +3 -0
- data/lib/json_api_ruby/resource.rb +55 -0
- data/lib/json_api_ruby/resources/base.rb +61 -0
- data/lib/json_api_ruby/resources/discovery.rb +40 -0
- data/lib/json_api_ruby/resources/dsl.rb +54 -0
- data/lib/json_api_ruby/resources/relationships.rb +106 -0
- data/lib/json_api_ruby/serializer.rb +100 -0
- data/lib/json_api_ruby/version.rb +3 -0
- data/lib/json_api_ruby.rb +8 -0
- data/spec/json_api_ruby/resource_spec.rb +90 -0
- data/spec/json_api_ruby/resources/discover_spec.rb +23 -0
- data/spec/json_api_ruby/resources/relationships_spec.rb +110 -0
- data/spec/json_api_ruby/serializer_spec.rb +82 -0
- data/spec/spec_helper.rb +102 -0
- data/spec/support/json_api_matchers.rb +54 -0
- data/spec/support/resource_objects.rb +114 -0
- metadata +160 -0
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe JsonApi::Resource do
|
4
|
+
subject(:serialized_person) do
|
5
|
+
person = Person.new('Brad J. Armbruster', 'ace@airforce.mil')
|
6
|
+
PersonResource.new(person).to_hash
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'is follows the JSON API spec' do
|
10
|
+
expect(serialized_person).to be_valid_json_api
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'has a name with the value "Brad J. Armbruster"' do
|
14
|
+
expect(serialized_person).to have_attribute(:name).with_value('Brad J. Armbruster')
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'has an email with the value "ace@airforce.mil"' do
|
18
|
+
expect(serialized_person).to have_attribute(:email_address).with_value('ace@airforce.mil')
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'has a created_at field' do
|
22
|
+
expect(serialized_person).to have_attribute(:created_at)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'has an updated_at timestamp' do
|
26
|
+
expect(serialized_person).to have_attribute(:updated_at)
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'relationships' do
|
30
|
+
subject(:serialized_article) do
|
31
|
+
article = Person.new('Anatoly Fyodorovich Krimov', 'red_star@kremlin.mil').articles.first
|
32
|
+
ArticleResource.new(article, include: [:author, :comments]).to_hash
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'with a cardinality of one' do
|
36
|
+
it 'includes the relationship keys' do
|
37
|
+
expect(serialized_article).to have_relationship('author')
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'have a valid author relationship' do
|
41
|
+
expect(serialized_article['relationships']['author']['data']).to be_valid_json_api
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'with a cardinality of many' do
|
46
|
+
it 'includes relationship data' do
|
47
|
+
expect(serialized_article).to have_relationship('comments')
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'includes the type for the relationship' do
|
51
|
+
serialized_article['relationships']['comments']['data'].map do |comment|
|
52
|
+
expect(comment).to be_valid_json_api
|
53
|
+
expect(comment['type']).to eq 'comments'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'includes the id for the relationship' do
|
58
|
+
serialized_article['relationships']['comments']['data'].map do |comment|
|
59
|
+
expect(comment['id'].length).to eq 36
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'relationship links' do
|
65
|
+
subject(:serialized_article) do
|
66
|
+
article = Person.new('Anatoly Fyodorovich Krimov', 'red_star@kremlin.mil').articles.first
|
67
|
+
ArticleResource.new(article).to_hash
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'includes links to for the comments relationship' do
|
71
|
+
expect(serialized_article).to have_links_for('comments')
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'includes links for the author relationship' do
|
75
|
+
expect(serialized_article).to have_links_for('author')
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'link paths' do
|
81
|
+
let(:person) { Person.new('Sherman R. Guderian', 'heavy_metal@airforce.mil') }
|
82
|
+
subject(:serialized_person) do
|
83
|
+
PersonResource.new(person).to_hash
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'returns a full URL to the resource' do
|
87
|
+
expect(serialized_person['links']['self']).to eq("http://localhost:3000/people/#{person.id}")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
RSpec.describe JsonApi::Resources::Discovery do
|
3
|
+
describe 'resource discovery' do
|
4
|
+
it 'finds a resource class created within the same namespace' do
|
5
|
+
rs1 = Namespace::OneResource.new(nil)
|
6
|
+
expect(JsonApi::Resources::Discovery.resource_for_name(Two.new, {parent_resource: rs1})).to eq Namespace::TwoResource
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'allows specifying a different namespace' do
|
10
|
+
expect(JsonApi::Resources::Discovery.resource_for_name(Three.new, namespace: 'different_namespace')).to eq DifferentNamespace::ThreeResource
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'allows explicitly providing a resource class' do
|
14
|
+
expect(JsonApi::Resources::Discovery.resource_for_name(Two.new, resource_class: 'DifferentNamespace::ThreeResource')).to eq DifferentNamespace::ThreeResource
|
15
|
+
end
|
16
|
+
|
17
|
+
it "raises an error if the resource can't be found" do
|
18
|
+
expect {
|
19
|
+
JsonApi::Resources::Discovery.resource_for_name(nil)
|
20
|
+
}.to raise_error JsonApi::ResourceNotFound
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe JsonApi::Resources::Relationships do
|
4
|
+
let(:article) do
|
5
|
+
Article.new('How to raise Triops', 'Triops are hardy little creatures whose eggs can be frozen for as long as 40 years')
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:article_resource) do
|
9
|
+
ArticleResource.new(article)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:author_relation) do
|
13
|
+
article_resource.class.relationships.find do |rel|
|
14
|
+
rel.name == 'author'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:comments_relation) do
|
19
|
+
article_resource.class.relationships.find do |rel|
|
20
|
+
rel.name == 'comments'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'link serialization' do
|
25
|
+
subject(:links_object) do
|
26
|
+
author_relation.build_resources(parent_resource: article_resource)
|
27
|
+
author_relation.to_hash
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'includes a links object' do
|
31
|
+
expect(links_object).to include('links')
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'has a "self" object' do
|
35
|
+
expect(links_object['links']).to include('self')
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'has a "related" object' do
|
39
|
+
expect(links_object['links']).to include('related')
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'self object' do
|
43
|
+
it 'is a link to the relationship on the parent object' do
|
44
|
+
expect(links_object['links']['self']).to eq "http://localhost:3000/articles/#{article.uuid}/relationships/author"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'related object' do
|
49
|
+
it 'is a link to the base resource of the related object' do
|
50
|
+
expect(links_object['links']['related']).to eq "http://localhost:3000/articles/#{article.uuid}/author"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'data serialization' do
|
56
|
+
subject(:serialized_object) do
|
57
|
+
author_relation.build_resources(parent_resource: article_resource, included: true)
|
58
|
+
author_relation.to_hash
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'has a data top level object' do
|
62
|
+
expect(serialized_object).to include('data')
|
63
|
+
end
|
64
|
+
|
65
|
+
describe 'data object' do
|
66
|
+
context 'when an array' do
|
67
|
+
subject(:serialized_object) do
|
68
|
+
comments_relation.build_resources(parent_resource: article_resource, included: true)
|
69
|
+
comments_relation.to_hash
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'has an identity hash for each object' do
|
73
|
+
expect(serialized_object['data'].flat_map(&:keys).uniq).to eq ['id', 'type']
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'when a single object' do
|
78
|
+
it 'has an identity hash' do
|
79
|
+
expect(serialized_object['data'].keys).to eq ['id', 'type']
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe 'identity hash' do
|
86
|
+
it 'returns an identity hash given a model and parent resource' do
|
87
|
+
author_relation.build_resources(parent_resource: article_resource, included: true)
|
88
|
+
serialized_object = author_relation.to_hash
|
89
|
+
expect(serialized_object['data'].keys).to eq ['id', 'type']
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe 'relationship serialization' do
|
94
|
+
context 'when the option "include" is true' do
|
95
|
+
it 'includes the data object' do
|
96
|
+
author_relation.build_resources(parent_resource: article_resource, included: true)
|
97
|
+
serialized_data = author_relation.to_hash
|
98
|
+
expect(serialized_data).to include('data')
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'when the option "include" is falsey' do
|
103
|
+
it 'does not include data object' do
|
104
|
+
author_relation.build_resources(parent_resource: article_resource)
|
105
|
+
serialized_data = author_relation.to_hash
|
106
|
+
expect(serialized_data).to_not include('data')
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe JsonApi::Serializer do
|
4
|
+
context 'a single object' do
|
5
|
+
let(:person) do
|
6
|
+
Person.new('Brad J. Armbruster', 'ace@airforce.mil')
|
7
|
+
end
|
8
|
+
|
9
|
+
subject(:serialized_data) do
|
10
|
+
JsonApi.serialize(person, meta: { meta_key: 'meta value' })
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'has a top level data object' do
|
14
|
+
expect(serialized_data).to have_data
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'passes meta through' do
|
18
|
+
expect(serialized_data).to have_meta
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'with included resources' do
|
22
|
+
subject(:serialized) do
|
23
|
+
JsonApi.serialize(person, meta: { meta_key: 'meta value' }, include: [:articles])
|
24
|
+
end
|
25
|
+
it 'has a top level included object' do
|
26
|
+
expect(serialized['included']).to be_present
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'includes the resources as expected' do
|
30
|
+
found_document = serialized['included'].select do |document|
|
31
|
+
document['id'] == person.articles.first.uuid
|
32
|
+
end
|
33
|
+
expect(found_document).to be_present
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'a collection of objects' do
|
39
|
+
let(:people) do
|
40
|
+
[
|
41
|
+
Person.new('Brad J. Armbruster', 'ace@airforce.mil'),
|
42
|
+
Person.new('Sabastian Bludd', 'sbludd@cobra.mil'),
|
43
|
+
Person.new('John Zullo', 'ace@specops.mil')
|
44
|
+
]
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'without included resources' do
|
48
|
+
subject(:serialized_resources) do
|
49
|
+
JsonApi.serialize(people, meta: {'I' => 'have meta'})
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'has three data objects' do
|
53
|
+
expect(serialized_resources).to have_data
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'passes meta through' do
|
57
|
+
expect(serialized_resources).to have_meta
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'with included resources' do
|
62
|
+
subject(:serialized_resources) do
|
63
|
+
JsonApi.serialize(people, meta: {'I' => 'have meta'}, include: [:articles])
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'has included resources' do
|
67
|
+
expect(serialized_resources['included']).to be_present
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'includes resources only once' do
|
71
|
+
included_identifiers = serialized_resources['included'].map do |resource|
|
72
|
+
resource['id'] + resource['type']
|
73
|
+
end
|
74
|
+
unique_identifiers = included_identifiers.uniq
|
75
|
+
unique_identifiers.each do |id|
|
76
|
+
all_ids = included_identifiers.select {|inc_id| inc_id == id}
|
77
|
+
expect(all_ids.length).to eq 1
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'pry-byebug'
|
4
|
+
require_relative '../lib/json_api_ruby'
|
5
|
+
require_relative 'support/resource_objects'
|
6
|
+
require_relative 'support/json_api_matchers'
|
7
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
8
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
9
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
10
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
11
|
+
# files.
|
12
|
+
#
|
13
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
14
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
15
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
16
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
17
|
+
# a separate helper file that requires the additional dependencies and performs
|
18
|
+
# the additional setup, and require it from the spec files that actually need
|
19
|
+
# it.
|
20
|
+
#
|
21
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
22
|
+
# users commonly want.
|
23
|
+
#
|
24
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
25
|
+
RSpec.configure do |config|
|
26
|
+
# rspec-expectations config goes here. You can use an alternate
|
27
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
28
|
+
# assertions if you prefer.
|
29
|
+
config.expect_with :rspec do |expectations|
|
30
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
31
|
+
# and `failure_message` of custom matchers include text for helper methods
|
32
|
+
# defined using `chain`, e.g.:
|
33
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
34
|
+
# # => "be bigger than 2 and smaller than 4"
|
35
|
+
# ...rather than:
|
36
|
+
# # => "be bigger than 2"
|
37
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
38
|
+
end
|
39
|
+
|
40
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
41
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
42
|
+
config.mock_with :rspec do |mocks|
|
43
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
44
|
+
# a real object. This is generally recommended, and will default to
|
45
|
+
# `true` in RSpec 4.
|
46
|
+
mocks.verify_partial_doubles = true
|
47
|
+
end
|
48
|
+
|
49
|
+
# The settings below are suggested to provide a good initial experience
|
50
|
+
# with RSpec, but feel free to customize to your heart's content.
|
51
|
+
=begin
|
52
|
+
# These two settings work together to allow you to limit a spec run
|
53
|
+
# to individual examples or groups you care about by tagging them with
|
54
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
55
|
+
# get run.
|
56
|
+
config.filter_run :focus
|
57
|
+
config.run_all_when_everything_filtered = true
|
58
|
+
|
59
|
+
# Allows RSpec to persist some state between runs in order to support
|
60
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
61
|
+
# you configure your source control system to ignore this file.
|
62
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
63
|
+
|
64
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
65
|
+
# recommended. For more details, see:
|
66
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
67
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
68
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
69
|
+
config.disable_monkey_patching!
|
70
|
+
|
71
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
72
|
+
# be too noisy due to issues in dependencies.
|
73
|
+
config.warnings = true
|
74
|
+
|
75
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
76
|
+
# file, and it's useful to allow more verbose output when running an
|
77
|
+
# individual spec file.
|
78
|
+
if config.files_to_run.one?
|
79
|
+
# Use the documentation formatter for detailed output,
|
80
|
+
# unless a formatter has already been configured
|
81
|
+
# (e.g. via a command-line flag).
|
82
|
+
config.default_formatter = 'doc'
|
83
|
+
end
|
84
|
+
|
85
|
+
# Print the 10 slowest examples and example groups at the
|
86
|
+
# end of the spec run, to help surface which specs are running
|
87
|
+
# particularly slow.
|
88
|
+
config.profile_examples = 10
|
89
|
+
|
90
|
+
# Run specs in random order to surface order dependencies. If you find an
|
91
|
+
# order dependency and want to debug it, you can fix the order by providing
|
92
|
+
# the seed, which is printed after each run.
|
93
|
+
# --seed 1234
|
94
|
+
config.order = :random
|
95
|
+
|
96
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
97
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
98
|
+
# test failures related to randomization by passing the same `--seed` value
|
99
|
+
# as the one that triggered the failure.
|
100
|
+
Kernel.srand config.seed
|
101
|
+
=end
|
102
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rspec/expectations'
|
2
|
+
|
3
|
+
RSpec::Matchers.define :have_relationship do |expected|
|
4
|
+
match do |actual|
|
5
|
+
relationships = Hash(actual).stringify_keys['relationships']
|
6
|
+
Hash(relationships).keys.include?(expected)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
RSpec::Matchers.define :have_data do
|
11
|
+
match do |actual|
|
12
|
+
data = Hash(actual).stringify_keys['data']
|
13
|
+
data.present?
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
RSpec::Matchers.define :have_meta do
|
18
|
+
match do |actual|
|
19
|
+
data = Hash(actual).stringify_keys['meta']
|
20
|
+
data.present?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
RSpec::Matchers.define :have_attribute do |key_name|
|
25
|
+
match do |actual|
|
26
|
+
attributes = Hash(actual).stringify_keys['attributes']
|
27
|
+
return false unless attributes.keys.include?(key_name.to_s)
|
28
|
+
return true if @chained.blank?
|
29
|
+
attributes[key_name.to_s] == @expected_value
|
30
|
+
end
|
31
|
+
|
32
|
+
chain :with_value do |key_value|
|
33
|
+
@chained = true
|
34
|
+
@expected_value = key_value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
RSpec::Matchers.define :be_valid_json_api do
|
39
|
+
match do |actual|
|
40
|
+
is_a_hash = actual.is_a?(Hash)
|
41
|
+
actual = Hash(actual).stringify_keys
|
42
|
+
has_id_and_type = actual['id'].present? && actual['type'].present?
|
43
|
+
|
44
|
+
is_a_hash && has_id_and_type
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
RSpec::Matchers.define :have_links_for do |expected|
|
49
|
+
match do |actual|
|
50
|
+
relationships = Hash(actual).stringify_keys['relationships']
|
51
|
+
relation = Hash(relationships)[expected]
|
52
|
+
Hash(relation).keys.include?('links')
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
module Identifiers
|
2
|
+
def id
|
3
|
+
self.object_id
|
4
|
+
end
|
5
|
+
|
6
|
+
def uuid
|
7
|
+
@uuid ||= SecureRandom.uuid
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Comment
|
12
|
+
include Identifiers
|
13
|
+
attr_accessor :author, :comment_text, :created_at, :updated_at
|
14
|
+
|
15
|
+
def initialize(comment_text)
|
16
|
+
@created_at = 1.day.ago
|
17
|
+
@updated_at = 30.minutes.ago
|
18
|
+
assign_author
|
19
|
+
end
|
20
|
+
|
21
|
+
def assign_author
|
22
|
+
@author = Person.new('Archibald Monev', 'dr_venom@cobra.mil')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Article
|
27
|
+
include Identifiers
|
28
|
+
attr_accessor :publish_date, :title, :short_description, :created_at, :updated_at
|
29
|
+
attr_reader :author
|
30
|
+
attr_reader :comments
|
31
|
+
|
32
|
+
def initialize(title, desc, author=nil)
|
33
|
+
@publish_date = Time.now
|
34
|
+
@created_at = 2.days.ago
|
35
|
+
@updated_at = 1.day.ago
|
36
|
+
@title = title
|
37
|
+
@short_description = desc
|
38
|
+
@author = author
|
39
|
+
generate_comments
|
40
|
+
end
|
41
|
+
|
42
|
+
def author
|
43
|
+
@author ||= Person.new('Ettienne R. LaFitte', 'gung_ho@recondo.mil')
|
44
|
+
end
|
45
|
+
|
46
|
+
def generate_comments
|
47
|
+
@comments = [
|
48
|
+
Comment.new("This article really cleared it up for me"),
|
49
|
+
Comment.new("No idea what comment 1 is all about, this article was a muddled mess")
|
50
|
+
]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class Person
|
55
|
+
include Identifiers
|
56
|
+
attr_accessor :name, :email_address, :created_at, :updated_at
|
57
|
+
|
58
|
+
def initialize(name, email)
|
59
|
+
@name = name
|
60
|
+
@email_address = email
|
61
|
+
@created_at = 1.month.ago
|
62
|
+
@updated_at = 1.month.ago
|
63
|
+
end
|
64
|
+
|
65
|
+
def articles
|
66
|
+
@articles ||= [ Article.new("How to Conquer the World", "10 simple steps to world domination", self) ]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
class PersonResource < JsonApi::Resource
|
71
|
+
attribute :name
|
72
|
+
attribute :email_address
|
73
|
+
attribute :created_at
|
74
|
+
attribute :updated_at
|
75
|
+
|
76
|
+
has_many :articles
|
77
|
+
end
|
78
|
+
|
79
|
+
class ArticleResource < JsonApi::Resource
|
80
|
+
id_field :uuid
|
81
|
+
attributes :publish_date, :title, :short_description, :created_at, :updated_at
|
82
|
+
|
83
|
+
has_one :author
|
84
|
+
has_many :comments
|
85
|
+
end
|
86
|
+
|
87
|
+
class CommentResource < JsonApi::Resource
|
88
|
+
id_field :uuid
|
89
|
+
attribute :author
|
90
|
+
attribute :comment_text
|
91
|
+
attribute :created_at
|
92
|
+
attribute :updated_at
|
93
|
+
end
|
94
|
+
|
95
|
+
# namespaced resources
|
96
|
+
class Two
|
97
|
+
end
|
98
|
+
|
99
|
+
class Three
|
100
|
+
end
|
101
|
+
|
102
|
+
module Namespace
|
103
|
+
class OneResource < JsonApi::Resource
|
104
|
+
end
|
105
|
+
|
106
|
+
class TwoResource < JsonApi::Resource
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
module DifferentNamespace
|
111
|
+
class ThreeResource < JsonApi::Resource
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|