jsonapi-consumer 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +27 -0
  3. data/.gitignore +1 -0
  4. data/Gemfile +6 -4
  5. data/README.md +9 -38
  6. data/Rakefile +17 -6
  7. data/bin/console +14 -0
  8. data/bin/setup +8 -0
  9. data/jsonapi-consumer.gemspec +10 -11
  10. data/lib/jsonapi/consumer/associations/base_association.rb +26 -0
  11. data/lib/jsonapi/consumer/associations/belongs_to.rb +30 -0
  12. data/lib/jsonapi/consumer/associations/has_many.rb +26 -0
  13. data/lib/jsonapi/consumer/associations/has_one.rb +19 -0
  14. data/lib/jsonapi/consumer/connection.rb +36 -0
  15. data/lib/jsonapi/consumer/error_collector.rb +91 -0
  16. data/lib/jsonapi/consumer/errors.rb +34 -76
  17. data/lib/jsonapi/consumer/formatter.rb +145 -0
  18. data/lib/jsonapi/consumer/helpers/callbacks.rb +27 -0
  19. data/lib/jsonapi/consumer/helpers/dirty.rb +71 -0
  20. data/lib/jsonapi/consumer/helpers/dynamic_attributes.rb +83 -0
  21. data/lib/jsonapi/consumer/helpers/uri.rb +9 -0
  22. data/lib/jsonapi/consumer/implementation.rb +12 -0
  23. data/lib/jsonapi/consumer/included_data.rb +49 -0
  24. data/lib/jsonapi/consumer/linking/links.rb +22 -0
  25. data/lib/jsonapi/consumer/linking/top_level_links.rb +39 -0
  26. data/lib/jsonapi/consumer/meta_data.rb +19 -0
  27. data/lib/jsonapi/consumer/middleware/json_request.rb +26 -0
  28. data/lib/jsonapi/consumer/middleware/parse_json.rb +22 -23
  29. data/lib/jsonapi/consumer/middleware/status.rb +41 -0
  30. data/lib/jsonapi/consumer/paginating/paginator.rb +89 -0
  31. data/lib/jsonapi/consumer/parsers/parser.rb +113 -0
  32. data/lib/jsonapi/consumer/query/builder.rb +212 -0
  33. data/lib/jsonapi/consumer/query/requestor.rb +67 -0
  34. data/lib/jsonapi/consumer/relationships/relations.rb +56 -0
  35. data/lib/jsonapi/consumer/relationships/top_level_relations.rb +30 -0
  36. data/lib/jsonapi/consumer/resource.rb +514 -54
  37. data/lib/jsonapi/consumer/result_set.rb +25 -0
  38. data/lib/jsonapi/consumer/schema.rb +153 -0
  39. data/lib/jsonapi/consumer/utils.rb +28 -0
  40. data/lib/jsonapi/consumer/version.rb +1 -1
  41. data/lib/jsonapi/consumer.rb +59 -34
  42. metadata +51 -111
  43. data/.rspec +0 -2
  44. data/CHANGELOG.md +0 -36
  45. data/lib/jsonapi/consumer/middleware/raise_error.rb +0 -21
  46. data/lib/jsonapi/consumer/middleware/request_headers.rb +0 -20
  47. data/lib/jsonapi/consumer/middleware/request_timeout.rb +0 -9
  48. data/lib/jsonapi/consumer/middleware.rb +0 -5
  49. data/lib/jsonapi/consumer/parser.rb +0 -75
  50. data/lib/jsonapi/consumer/query/base.rb +0 -34
  51. data/lib/jsonapi/consumer/query/create.rb +0 -9
  52. data/lib/jsonapi/consumer/query/delete.rb +0 -10
  53. data/lib/jsonapi/consumer/query/find.rb +0 -16
  54. data/lib/jsonapi/consumer/query/new.rb +0 -15
  55. data/lib/jsonapi/consumer/query/update.rb +0 -11
  56. data/lib/jsonapi/consumer/query.rb +0 -5
  57. data/lib/jsonapi/consumer/resource/association_concern.rb +0 -203
  58. data/lib/jsonapi/consumer/resource/attributes_concern.rb +0 -70
  59. data/lib/jsonapi/consumer/resource/connection_concern.rb +0 -99
  60. data/lib/jsonapi/consumer/resource/finders_concern.rb +0 -28
  61. data/lib/jsonapi/consumer/resource/object_build_concern.rb +0 -28
  62. data/lib/jsonapi/consumer/resource/serializer_concern.rb +0 -63
  63. data/spec/fixtures/.gitkeep +0 -0
  64. data/spec/fixtures/resources.rb +0 -45
  65. data/spec/fixtures/responses.rb +0 -64
  66. data/spec/jsonapi/consumer/associations_spec.rb +0 -166
  67. data/spec/jsonapi/consumer/attributes_spec.rb +0 -27
  68. data/spec/jsonapi/consumer/connection_spec.rb +0 -147
  69. data/spec/jsonapi/consumer/error_handling_spec.rb +0 -37
  70. data/spec/jsonapi/consumer/object_build_spec.rb +0 -20
  71. data/spec/jsonapi/consumer/parser_spec.rb +0 -39
  72. data/spec/jsonapi/consumer/resource_spec.rb +0 -62
  73. data/spec/jsonapi/consumer/serializer_spec.rb +0 -41
  74. data/spec/spec_helper.rb +0 -97
  75. data/spec/support/.gitkeep +0 -0
  76. data/spec/support/load_fixtures.rb +0 -4
@@ -1,45 +0,0 @@
1
- class Base
2
- include JSONAPI::Consumer::Resource
3
-
4
- self.host = 'http://localhost:3000/api/'
5
- end
6
-
7
-
8
- class BasicResource < Base
9
-
10
- end
11
-
12
- class BuildRequest < Base
13
- self.request_new_object_on_build = true
14
- end
15
-
16
-
17
- # BEGIN - Blog example
18
- module Blog
19
- class Author < Base
20
- has_many :posts, class_name: 'Blog::Post'
21
- end
22
-
23
- class Post < Base
24
- has_one :user, class_name: 'Blog::User'
25
- has_many :comments, class_name: 'Blog::Comment'
26
- end
27
-
28
- class User < Base
29
-
30
- end
31
-
32
- class Comment < Base
33
- # belongs_to :post, class_name: 'Blog::Post'
34
- # has_one :author, class_name: 'Blog::Author'
35
- end
36
- end
37
- # END - Blog example
38
-
39
-
40
- class NotCalled < Faraday::Response::Middleware
41
- class ::NotCalledError < StandardError; end
42
- def parse(body)
43
- raise NotCalledError, "this should not be called"
44
- end
45
- end
@@ -1,64 +0,0 @@
1
- module Responses
2
- def self.sideload
3
- {
4
- posts: [
5
- {
6
- links: {
7
- comments: [
8
- "82083863-bba9-480e-a281-f5d34e7dc0ca",
9
- "3b402e8a-7c35-4915-8c72-07ea7779ab76"
10
- ],
11
- user: "6a45992f-cd20-497a-a753-21b2a1a82356"
12
- },
13
- id: "e6d1b7ac-80d8-40dd-877d-f5bd40feabfb",
14
- title: "Friday Post",
15
- created_at: "2014-10-19T22:32:52.913Z",
16
- updated_at: "2014-10-19T22:32:52.967Z"
17
- },
18
- {
19
- links: {
20
- comments: [
21
- "9c9ba83b-024c-4d4c-9573-9fd41b95fc14",
22
- "27fcf6e8-24b0-41db-94b1-812046a10f54"
23
- ],
24
- user: "d65dda70-73eb-461a-bb34-5484e6e8c194"
25
- },
26
- id: "ea006f14-6d05-4e87-bfe7-ee8ae3358840",
27
- title: "Monday Post",
28
- created_at: "2014-10-19T22:32:52.933Z",
29
- updated_at: "2014-10-19T22:32:52.969Z"
30
- }
31
- ],
32
- linked: {
33
- users: [
34
- {
35
- id: "6a45992f-cd20-497a-a753-21b2a1a82356",
36
- name: "Jane Smith"
37
- },
38
- {
39
- id: "d65dda70-73eb-461a-bb34-5484e6e8c194",
40
- name: "Jim Bob"
41
- }
42
- ],
43
- comments: [
44
- {
45
- id: "82083863-bba9-480e-a281-f5d34e7dc0ca",
46
- content: "Awesome article",
47
- created_at: "2014-10-19T22:32:52.933Z",
48
- updated_at: "2014-10-19T22:32:52.969Z"
49
- },
50
- {
51
- id: "3b402e8a-7c35-4915-8c72-07ea7779ab76",
52
- content: "Hated it",
53
- created_at: "2014-10-19T22:32:52.933Z",
54
- updated_at: "2014-10-19T22:32:52.969Z"
55
- }
56
- ]
57
- },
58
- links: {
59
- :"posts.comments" => "http://localhost:3000/api/comments/{posts.comments}",
60
- :"posts.user" => "http://localhost:3000/api/comments/{posts.user}"
61
- }
62
- }.with_indifferent_access
63
- end
64
- end
@@ -1,166 +0,0 @@
1
- RSpec.describe 'Associations', 'has_many' do
2
- let(:user_class) do
3
- User ||= Class.new do
4
- include JSONAPI::Consumer::Resource
5
-
6
- has_many :posts, class_name: 'Post'
7
- end
8
- end
9
-
10
- let!(:post_class) do
11
- Post ||= Class.new do
12
- include JSONAPI::Consumer::Resource
13
- end
14
- end
15
-
16
- subject(:user_instance) { user_class.new(username: 'foobar') }
17
-
18
- it 'lists association names in #association_names' do
19
- expect(user_instance.association_names).to eql([:posts])
20
- end
21
-
22
- describe 'defined accessors for assocation' do
23
- it { is_expected.to respond_to(:posts) }
24
- it { is_expected.to respond_to(:posts=) }
25
- end
26
-
27
- describe 'adding objects to array' do
28
- it 'can be added as a single value' do
29
- expect {
30
- user_instance.posts = '1'
31
- }.to change{user_instance.post_ids}.from(nil).to(['1'])
32
- end
33
-
34
- it 'can be added as a list of items' do
35
- expect {
36
- user_instance.posts = ['1','2','3']
37
- }.to change{user_instance.post_ids}.from(nil).to(['1','2','3'])
38
- end
39
-
40
- it 'can be blanked out' do
41
- user_instance.posts = '1'
42
- expect {
43
- user_instance.posts = nil
44
- }.to change{user_instance.post_ids}.from(['1']).to(nil)
45
- end
46
- end
47
-
48
- describe 'the links payload' do
49
- subject(:payload_hash) { user_instance.serializable_hash }
50
-
51
- describe 'when populated' do
52
- before do
53
- user_instance.posts = ['1']
54
- end
55
-
56
- it 'has links in output, if present' do
57
- expect(payload_hash).to have_key(:links)
58
- expect(payload_hash[:links]).to eql({posts: ['1']})
59
- expect(payload_hash[:links][:posts]).to eql(["1"])
60
- end
61
- end
62
-
63
- describe 'when empty' do
64
- it 'has no links in output' do
65
- expect(payload_hash).to_not have_key(:links)
66
- end
67
- end
68
-
69
- describe 'when persisted and empty' do
70
- before do
71
- allow(user_instance).to receive(:persisted?).and_return(true)
72
- end
73
-
74
- it 'has a links hash in the output' do
75
- expect(payload_hash).to have_key(:links)
76
- expect(payload_hash[:links]).to eql({posts: []})
77
- end
78
- end
79
- end
80
- end
81
-
82
- RSpec.describe 'Associations', 'belongs_to' do
83
- let(:comment_class) do
84
- Comment ||= Class.new do
85
- include JSONAPI::Consumer::Resource
86
-
87
- # belongs_to :comment, class_name: 'Comment'
88
- belongs_to :user, class_name: 'User'
89
- end
90
- end
91
-
92
- let!(:user_class) do
93
- User ||= Class.new do
94
- include JSONAPI::Consumer::Resource
95
- end
96
- end
97
-
98
- subject(:comment_instance) { comment_class.new }
99
-
100
- it 'lists association names in #association_names' do
101
- expect(comment_instance.association_names).to eql([:user])
102
- end
103
-
104
- describe 'defined accessors for assocation' do
105
- it { is_expected.to respond_to(:user) }
106
- it { is_expected.to respond_to(:user=) }
107
- end
108
-
109
- describe 'adding objects to belongs_to relationship' do
110
- it 'can be added as a single value' do
111
- expect {
112
- comment_instance.user = '1'
113
- }.to change{comment_instance.user_id}.from(nil).to('1')
114
- end
115
-
116
- it 'can be blanked out' do
117
- comment_instance.user = '1'
118
- expect {
119
- comment_instance.user = nil
120
- }.to change{comment_instance.user_id}.from('1').to(nil)
121
- end
122
- end
123
- end
124
-
125
- RSpec.describe 'Associations', 'has_one' do
126
- let(:post_class) do
127
- Poster ||= Class.new do
128
- include JSONAPI::Consumer::Resource
129
-
130
- has_one :author, class_name: 'Customer'
131
- end
132
- end
133
-
134
- let!(:user_class) do
135
- Customer ||= Class.new do
136
- include JSONAPI::Consumer::Resource
137
- end
138
- end
139
-
140
- subject(:post_instance) { post_class.new }
141
-
142
- it 'lists association names in #association_names' do
143
- expect(post_instance.association_names).to eql([:author])
144
- end
145
-
146
- describe 'defined accessors for assocation' do
147
- it { is_expected.to respond_to(:author) }
148
- it { is_expected.to respond_to(:author=) }
149
- end
150
-
151
- describe 'adding objects to belongs_to relationship' do
152
- it 'can be added as a single value' do
153
- expect {
154
- post_instance.author = '1'
155
- }.to change{post_instance.author_id}.from(nil).to('1')
156
- end
157
-
158
- it 'can be blanked out' do
159
- post_instance.author = '1'
160
- expect {
161
- post_instance.author = nil
162
- }.to change{post_instance.author_id}.from('1').to(nil)
163
- end
164
- end
165
- end
166
-
@@ -1,27 +0,0 @@
1
- RSpec.describe 'Attributes' do
2
- let(:test_class) do
3
- Class.new do
4
- include JSONAPI::Consumer::Resource
5
- end
6
- end
7
-
8
- subject(:obj) { test_class.new }
9
-
10
- its(:primary_key) { is_expected.to eql(:id) }
11
-
12
- describe 'changing the primary key' do
13
- it 'is updatable on the class' do
14
- expect {
15
- obj.class.primary_key = :name
16
- }.to change{obj.primary_key}.to(:name)
17
- end
18
- end
19
-
20
- describe '#persisted?' do
21
- it 'uses the primary key to decide' do
22
- expect {
23
- obj.id = '8'
24
- }.to change{obj.persisted?}.from(false).to(true)
25
- end
26
- end
27
- end
@@ -1,147 +0,0 @@
1
- RSpec.describe 'Connection' do
2
- let(:test_class) do
3
- Record ||= Class.new do
4
- include JSONAPI::Consumer::Resource
5
- self.host = 'http://localhost:3000/api/'
6
-
7
- has_one :domain
8
- end
9
- end
10
-
11
- let(:obj) { test_class.new(name: 'jsonapi.example') }
12
-
13
- describe 'custom connection middleware' do
14
-
15
- it 'handles custom middleware' do
16
- stub_request(:get, "http://localhost:3000/api/basic_resources")
17
- .with(headers: {accept: 'application/json'})
18
- .to_return(headers: {content_type: "application/json"}, body: {
19
- basic_resources: [
20
- {id: '1'}
21
- ]
22
- }.to_json)
23
-
24
- expect { BasicResource.all }.to_not raise_error
25
-
26
- BasicResource.middleware do |conn|
27
- conn.use NotCalled
28
- end
29
-
30
- expect { BasicResource.all }.to raise_error(NotCalledError)
31
- end
32
- end
33
-
34
- describe '.all' do
35
- it 'returns all results as objects' do
36
- stub_request(:get, "http://localhost:3000/api/records")
37
- .with(headers: {accept: 'application/json'})
38
- .to_return(headers: {content_type: "application/json"}, body: {
39
- records: [
40
- {id: '1', name: "foo.example"},
41
- {id: '2', name: "bar.example"},
42
- {id: '3', name: "baz.example"}
43
- ]
44
- }.to_json)
45
-
46
- records = test_class.all
47
- expect(records.size).to eql(3)
48
-
49
- record = records.first
50
- expect(record).to be_a(Record)
51
- end
52
-
53
- it 'accepts additional params' do
54
- stub_request(:get, "http://localhost:3000/api/records")
55
- .with(headers: {accept: 'application/json'})
56
- .to_return(headers: {content_type: "application/json"}, body: {
57
- records: []
58
- }.to_json) # This should not get called.
59
-
60
- stub_request(:get, "http://localhost:3000/api/records?name=foo&email=bar@example.com")
61
- .with(headers: {accept: 'application/json'})
62
- .to_return(headers: {content_type: "application/json"}, body: {
63
- records: [
64
- {id: '1', name: 'bar', email: "bar.example"},
65
- {id: '2', name: 'foo', email: "bar.example"},
66
- ]
67
- }.to_json)
68
-
69
- records = test_class.all(name: 'foo', email: 'bar@example.com')
70
- expect(records.size).to eql(2)
71
- end
72
- end
73
-
74
- describe '.find' do
75
- it 'returns proper objects' do
76
- stub_request(:get, "http://localhost:3000/api/records/1")
77
- .with(headers: {accept: 'application/json'})
78
- .to_return(headers: {content_type: "application/json"}, body: {
79
- records: [
80
- {id: '1', name: "foobar.example"}
81
- ]
82
- }.to_json)
83
-
84
- records = test_class.find(1)
85
- expect(records.size).to eql(1)
86
-
87
- record = records.first
88
- expect(record).to be_a(Record)
89
- expect(record.id).to eql('1')
90
- expect(record.name).to eql('foobar.example')
91
- end
92
- end
93
-
94
- describe '#save' do
95
- it 'can save successfully if called on a new item' do
96
- stub_request(:post, "http://localhost:3000/api/records")
97
- .with(headers: {accept: 'application/json', content_type: "application/json"})
98
- .to_return(headers: {content_type: "application/json"}, status: 201, body: {
99
- records: [
100
- {id: '1', name: "foobar.example", created_at: "2014-10-16T18:49:40Z", updated_at: "2014-10-18T18:59:40Z"}
101
- ]
102
- }.to_json)
103
-
104
- expect(obj.save).to eql(true)
105
-
106
- expect(obj.id).to eql('1')
107
- expect(obj.name).to eql('foobar.example')
108
-
109
- expect(obj.created_at).to eql('2014-10-16T18:49:40Z')
110
- expect(obj.updated_at).to eql('2014-10-18T18:59:40Z')
111
-
112
- expect(obj).to respond_to(:created_at)
113
- expect(obj).to respond_to(:updated_at)
114
- expect(obj.persisted?).to eql(true)
115
- end
116
-
117
- it 'can update when called on an existing item' do
118
- stub_request(:put, "http://localhost:3000/api/records/1")
119
- .with(headers: {accept: 'application/json', content_type: "application/json"})
120
- .to_return(headers: {content_type: "application/json"}, body: {
121
- records: [
122
- {id: '1', name: "foobar.example", created_at: "2014-10-16T18:49:40Z", updated_at: "2016-10-18T18:59:40Z"}
123
- ]
124
- }.to_json)
125
-
126
- obj.id = '1'
127
- obj.updated_at = "2014-10-18T18:59:40Z"
128
- expect(obj.updated_at).to eql("2014-10-18T18:59:40Z")
129
-
130
- expect(obj.save).to eql(true)
131
- expect(obj.updated_at).to eql("2016-10-18T18:59:40Z")
132
- end
133
- end
134
-
135
- describe '#destroy' do
136
- before { obj.id = '1' }
137
-
138
- it 'returns true when successful' do
139
- stub_request(:delete, "http://localhost:3000/api/records/1")
140
- .with(headers: {accept: "application/json"})
141
- .to_return(status: 204, body: nil)
142
-
143
- expect(obj.destroy).to eql(true)
144
- end
145
- end
146
- end
147
-
@@ -1,37 +0,0 @@
1
- RSpec.describe 'Error handling' do
2
- let(:test_class) do
3
- Record ||= Class.new do
4
- include JSONAPI::Consumer::Resource
5
- self.host = 'http://localhost:3000/api/'
6
-
7
- has_one :domain
8
- end
9
- end
10
-
11
- let(:obj) { test_class.new(name: 'jsonapi.example') }
12
-
13
- context 'on model' do
14
- it 'adds to the errors object on the model' do
15
- stub_request(:post, "http://localhost:3000/api/records")
16
- .to_return(headers: {content_type: "application/json"}, status: 400, body: {
17
- errors: [
18
- {title: 'cannot be blank', path: "/name", detail: 'name cannot be blank'},
19
- {title: 'is invalid', path: '/type', detail: 'type is invalid'}
20
- ]
21
- }.to_json)
22
-
23
- expect(obj.save).to eql(false)
24
- expect(obj.is_valid?).to eql(false)
25
- end
26
- end
27
-
28
- context 'in general' do
29
- it 'handles timeout errors' do
30
- stub_request(:any, "http://localhost:3000/api/records").to_timeout
31
-
32
- expect {
33
- obj.save
34
- }.to raise_error(JSONAPI::Consumer::Errors::ServerNotResponding)
35
- end
36
- end
37
- end
@@ -1,20 +0,0 @@
1
- RSpec.describe 'Object building' do
2
- let(:class_name) { BuildRequest }
3
-
4
- subject(:obj) { class_name.build }
5
-
6
- it 'returns an object with populated items' do
7
- stub_request(:get, "http://localhost:3000/api/build_requests/new")
8
- .to_return(headers: {content_type: "application/json"}, body: {
9
- build_requests: [
10
- {name: "", title: "default value"}
11
- ]
12
- }.to_json)
13
-
14
- expect(obj).to respond_to(:name)
15
- expect(obj).to respond_to(:title)
16
-
17
- expect(obj.name).to be_blank
18
- expect(obj.title).to eql("default value")
19
- end
20
- end
@@ -1,39 +0,0 @@
1
- RSpec.describe 'Response Parsing' do
2
- let(:response) { OpenStruct.new(body: Responses.sideload) }
3
- let(:parser) { JSONAPI::Consumer::Parser.new(Blog::Post, response) }
4
-
5
- subject(:results) { parser.build }
6
-
7
- it 'can handle linked associations' do
8
- stub_request(:get, 'http://localhost:3000/api/comments/9c9ba83b-024c-4d4c-9573-9fd41b95fc14')
9
- .to_return(headers: {content_type: "application/json"}, body: {
10
- comments: [
11
- {
12
- id: '9c9ba83b-024c-4d4c-9573-9fd41b95fc14',
13
- content: "i found this useful."
14
- }
15
- ]
16
- }.to_json)
17
-
18
- stub_request(:get, 'http://localhost:3000/api/comments/27fcf6e8-24b0-41db-94b1-812046a10f54')
19
- .to_return(headers: {content_type: "application/json"}, body: {
20
- comments: [
21
- {
22
- id: '27fcf6e8-24b0-41db-94b1-812046a10f54',
23
- content: "i found this useful too."
24
- }
25
- ]
26
- }.to_json)
27
-
28
- expect(results.size).to eql(2)
29
-
30
- result = results.first
31
- expect(result.comments.size).to eql(2)
32
- expect(result.user).to be_a(Blog::User)
33
-
34
- last = results.last
35
- expect(last.comments.first).to be_a(Blog::Comment)
36
- expect(last.comments.first.id).to eql("9c9ba83b-024c-4d4c-9573-9fd41b95fc14")
37
- expect(last.comments.first.content).to eql("i found this useful.")
38
- end
39
- end
@@ -1,62 +0,0 @@
1
- RSpec.describe 'Resource' do
2
- let(:sample_attrs) {
3
- {
4
- id: SecureRandom.uuid,
5
- name: 'foo',
6
- content: 'bar'
7
- }
8
- }
9
-
10
- subject(:test_class) do
11
- Paper ||= Class.new do
12
- include JSONAPI::Consumer::Resource
13
- end
14
- end
15
-
16
- it 'it errors on undefined methods' do
17
- obj = test_class.new
18
- expect {
19
- obj.paper
20
- }.to raise_error(NoMethodError)
21
- end
22
-
23
- describe 'accepts any passed in params through #attributes=' do
24
- subject(:instance) { test_class.new }
25
-
26
- before do
27
- instance.attributes = sample_attrs
28
- end
29
-
30
- it { is_expected.to respond_to(:id) }
31
- it { is_expected.to respond_to(:name) }
32
- it { is_expected.to respond_to(:content) }
33
- end
34
-
35
- describe 'accepts attributes through .new' do
36
- subject(:instance) { test_class.new(sample_attrs) }
37
-
38
- it { is_expected.to respond_to(:id) }
39
- it { is_expected.to respond_to(:name) }
40
- it { is_expected.to respond_to(:content) }
41
- end
42
-
43
- describe '#serializable_hash' do
44
- subject(:obj_hash) { test_class.new(sample_attrs).serializable_hash }
45
-
46
- it 'has proper keys' do
47
- expect(obj_hash).to have_key(:id)
48
- expect(obj_hash).to have_key(:name)
49
- expect(obj_hash).to have_key(:content)
50
- end
51
- end
52
-
53
- describe '#to_json' do
54
- subject(:obj_hash) { test_class.new(sample_attrs).to_json }
55
-
56
- it 'has all attributes root key' do
57
- json_hash = JSON.parse(obj_hash)
58
- expect(json_hash.keys).to eql(['id', 'name', 'content'])
59
- end
60
- end
61
-
62
- end
@@ -1,41 +0,0 @@
1
- RSpec.describe 'Serializer' do
2
- let(:test_class) do
3
- SerializerTestClass ||= Class.new do
4
- include JSONAPI::Consumer::Resource
5
-
6
- has_many :users, class_name: 'Account'
7
- has_one :owner, class_name: 'Owner'
8
- end
9
- end
10
-
11
- let(:owner) {
12
- Owner ||= Class.new do
13
- include JSONAPI::Consumer::Resource
14
-
15
- def to_param
16
- 1
17
- end
18
- end
19
- }
20
-
21
- let(:user) {
22
- Account ||= Class.new do
23
- include JSONAPI::Consumer::Resource
24
- end
25
- }
26
-
27
- subject(:obj_hash) { test_class.new(id: '76', owner: owner.new, users: [user.new(id: 'a'), user.new(id: 'b')]).serializable_hash }
28
-
29
- it 'outputs the associated has_one' do
30
- expect(obj_hash).to have_key(:links)
31
- expect(obj_hash[:links]).to have_key(:owner)
32
- expect(obj_hash[:links][:owner]).to eql(1)
33
- end
34
-
35
- it 'outputs the associated has_many' do
36
- expect(obj_hash).to have_key(:links)
37
- expect(obj_hash[:links]).to have_key(:users)
38
- expect(obj_hash[:links][:users]).to eql(['a', 'b'])
39
- end
40
-
41
- end