json_api_toolbox 0.2.0 → 0.3.0
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 +4 -4
- data/README.md +113 -0
- data/json_api_toolbox.gemspec +3 -0
- data/lib/json_api_toolbox.rb +2 -0
- data/lib/json_api_toolbox/spec_support/shared_examples_for_controllers.rb +55 -0
- data/lib/json_api_toolbox/version.rb +1 -1
- data/lib/renderizable.rb +56 -0
- metadata +46 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4044c6acd33567f9a618c59e603d30881e90806f
|
4
|
+
data.tar.gz: adf91b78f4c5fcf19f91653ecefd268f278d8d8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4f3e7ed1bb9b974bdc6489371b7e9a0a7b643a20d7db0e01d2ee0949f6ef36405c67c542ec034cb835360134cea67b73368cae54115f9ad2b7988c57b5ec4ab
|
7
|
+
data.tar.gz: 55d6c9ed248b0b8c57c1cefdb89e517a75c1d63275b55adcd1c3d2cfe1b84445f57baa2e7049e6b8824595f882685e526df8e109b744b922157a1acce58bb509
|
data/README.md
CHANGED
@@ -33,3 +33,116 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
33
33
|
## Contributing
|
34
34
|
|
35
35
|
Bug reports and pull requests are welcome on GitHub at https://github.com/abacha/json_api_toolbox.
|
36
|
+
|
37
|
+
## Examples
|
38
|
+
|
39
|
+
##ShareExamplesForControllers
|
40
|
+
|
41
|
+
it_behaves_like 'a http method'
|
42
|
+
validate if your response have http status 200
|
43
|
+
|
44
|
+
it_behaves_like 'a get method'
|
45
|
+
validate if your response have http status 200
|
46
|
+
validate if your response responde to body method
|
47
|
+
|
48
|
+
it_behaves_like 'a json api response with included node'
|
49
|
+
validate if yor response.body has included node
|
50
|
+
|
51
|
+
it_behaves_like 'a get with jsonapi with default value of', SomeModel
|
52
|
+
validate if all attributes are present on your data
|
53
|
+
obs:
|
54
|
+
1 - for enums who have '_cd' in the end of attribute name, '_cd' is removed.
|
55
|
+
ex: enum_cd => enum
|
56
|
+
2 - id attributes are removed from validation
|
57
|
+
3 - for attributes who have _id in the end of attribute name, they are
|
58
|
+
removed from validation
|
59
|
+
|
60
|
+
it_behaves_like 'a json api response with all relations of', SomeModel
|
61
|
+
validate if all relations are included on your data
|
62
|
+
|
63
|
+
## Example Tests
|
64
|
+
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
# frozen_string_literal: true
|
68
|
+
|
69
|
+
require 'rails_helper'
|
70
|
+
|
71
|
+
RSpec.describe ManagersController, type: :controller do
|
72
|
+
let(:body) { JSON.parse(response.body) }
|
73
|
+
let(:data) { body['data'] }
|
74
|
+
|
75
|
+
describe 'GET' do
|
76
|
+
let!(:manager) { create(:manager) }
|
77
|
+
let(:params) { {} }
|
78
|
+
|
79
|
+
describe '#index' do
|
80
|
+
let(:data) { body['data'].first }
|
81
|
+
|
82
|
+
before { get :index, params: params }
|
83
|
+
|
84
|
+
it_behaves_like 'a get method'
|
85
|
+
it_behaves_like 'a json api response with all relations of', Manager
|
86
|
+
it_behaves_like 'a get with jsonapi with default value of', Manager
|
87
|
+
|
88
|
+
context 'including contacts' do
|
89
|
+
let(:params) { { includes: :contacts } }
|
90
|
+
it { expect(data['relationships']['contacts']['data'].size).to eq(1) }
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe '#show' do
|
95
|
+
before { get :show, params: { id: manager.id } }
|
96
|
+
|
97
|
+
it_behaves_like 'a get method'
|
98
|
+
it_behaves_like 'a json api response with included node'
|
99
|
+
it_behaves_like 'a json api response with all relations of', Manager
|
100
|
+
it_behaves_like 'a get with jsonapi with default value of', Manager
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe 'POST' do
|
105
|
+
describe '#create' do
|
106
|
+
let(:params) do
|
107
|
+
attributes_for(:manager).merge(
|
108
|
+
contacts: attributes_for_list(:contact, 2)
|
109
|
+
)
|
110
|
+
end
|
111
|
+
|
112
|
+
before { post :create, params: params }
|
113
|
+
|
114
|
+
it_behaves_like 'a get method'
|
115
|
+
it_behaves_like 'a json api response with included node'
|
116
|
+
it_behaves_like 'a json api response with all relations of', Manager
|
117
|
+
it_behaves_like 'a get with jsonapi with default value of', Manager
|
118
|
+
|
119
|
+
it { expect(data['relationships']['contacts']['data'].count).to eq(2) }
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe 'PATCH' do
|
124
|
+
describe '#update' do
|
125
|
+
let(:manager) { create(:manager) }
|
126
|
+
let(:new_name) { 'Manager 100' }
|
127
|
+
let(:params) do
|
128
|
+
{
|
129
|
+
id: manager.id,
|
130
|
+
name: new_name,
|
131
|
+
short_name: manager.short_name
|
132
|
+
}
|
133
|
+
end
|
134
|
+
|
135
|
+
before { post :update, params: params }
|
136
|
+
|
137
|
+
it_behaves_like 'a get method'
|
138
|
+
it_behaves_like 'a json api response with included node'
|
139
|
+
it_behaves_like 'a json api response with all relations of', Manager
|
140
|
+
it_behaves_like 'a get with jsonapi with default value of', Manager
|
141
|
+
|
142
|
+
it 'validates if values were updated' do
|
143
|
+
expect(data['attributes']['name']).to eq(new_name)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
```
|
data/json_api_toolbox.gemspec
CHANGED
@@ -18,9 +18,12 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
20
|
spec.add_dependency "activesupport", ">= 4.0"
|
21
|
+
spec.add_dependency "jsonapi-rails", ">= 0.2"
|
21
22
|
|
22
23
|
spec.add_development_dependency "bundler", "~> 1.15"
|
23
24
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
25
|
spec.add_development_dependency "rspec", "~> 3.0"
|
25
26
|
spec.add_development_dependency "pry-byebug"
|
27
|
+
spec.add_development_dependency "json"
|
28
|
+
spec.add_development_dependency "pry"
|
26
29
|
end
|
data/lib/json_api_toolbox.rb
CHANGED
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JsonApiToolbox
|
4
|
+
module SpecSupport
|
5
|
+
module SharedExamplesForControllers
|
6
|
+
RSpec.shared_examples 'a http method' do
|
7
|
+
it 'returns http success' do
|
8
|
+
expect(response).to have_http_status(:success)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
RSpec.shared_examples 'a get method' do
|
13
|
+
it_behaves_like 'a http method'
|
14
|
+
|
15
|
+
it 'response with JSON body' do
|
16
|
+
expect { response.body }.not_to raise_exception
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
RSpec.shared_examples 'a json api response with included node' do
|
21
|
+
it 'returns with included node' do
|
22
|
+
expect(JSON.parse(response.body)['included'].count).not_to eql(0)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
RSpec.shared_examples 'a get with jsonapi with default value of' do |model|
|
27
|
+
let(:attributes) { data['attributes'].keys }
|
28
|
+
let(:model_keys) do
|
29
|
+
model.new.attributes.keys.map do |attr|
|
30
|
+
if attr.match?(/_cd$/)
|
31
|
+
attr.gsub(/_cd/, '')
|
32
|
+
elsif attr.match?(/_id$|^id$/)
|
33
|
+
nil
|
34
|
+
else
|
35
|
+
attr
|
36
|
+
end
|
37
|
+
end.compact
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'returns response with default values' do
|
41
|
+
expect(attributes).to match_array(model_keys)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
RSpec.shared_examples 'a json api response with all relations of' do |model|
|
46
|
+
let(:attributes) { data['relationships'].keys }
|
47
|
+
let(:relation_keys) { model.reflections.keys }
|
48
|
+
|
49
|
+
it 'returns response with default relationships' do
|
50
|
+
expect(attributes).to match_array(relation_keys)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/renderizable.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
module JsonApiToolbox
|
5
|
+
module Renderizable
|
6
|
+
def render_with_errors(object_errors)
|
7
|
+
errors = object_errors.details.map do |field, error_details|
|
8
|
+
error_details.each_with_index.map do |item, error_i|
|
9
|
+
build_line(item[:error], field, object_errors.messages[field][error_i])
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
render json: { errors: errors }
|
14
|
+
end
|
15
|
+
|
16
|
+
def render_object(object)
|
17
|
+
if object.respond_to?(:valid?) && !object.valid?
|
18
|
+
render_with_errors(object.errors)
|
19
|
+
else
|
20
|
+
render jsonapi: object, include: includes_params
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def includes_params
|
25
|
+
params.fetch(:includes, {})
|
26
|
+
end
|
27
|
+
|
28
|
+
def permitted_params
|
29
|
+
parsed_params = params.permit(permitted_attributes).to_h
|
30
|
+
JsonApiToolbox.normalize_post(parsed_params, model_name)
|
31
|
+
end
|
32
|
+
|
33
|
+
def default_includes_params
|
34
|
+
model_relationships =
|
35
|
+
model_name.reflect_on_all_associations(:has_one).map(&:name)
|
36
|
+
model_relationships +=
|
37
|
+
model_name.reflect_on_all_associations(:has_many).map(&:name)
|
38
|
+
|
39
|
+
params.merge!(includes: model_relationships)
|
40
|
+
end
|
41
|
+
|
42
|
+
def model_name
|
43
|
+
controller_name.singularize.camelize.constantize
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def build_line(title, field, message)
|
49
|
+
{
|
50
|
+
title: title,
|
51
|
+
source: field,
|
52
|
+
detail: message
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_api_toolbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adriano Bacha
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jsonapi-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.2'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +94,34 @@ dependencies:
|
|
80
94
|
- - ">="
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: json
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
83
125
|
description:
|
84
126
|
email:
|
85
127
|
- abacha@gmail.com
|
@@ -96,7 +138,9 @@ files:
|
|
96
138
|
- Rakefile
|
97
139
|
- json_api_toolbox.gemspec
|
98
140
|
- lib/json_api_toolbox.rb
|
141
|
+
- lib/json_api_toolbox/spec_support/shared_examples_for_controllers.rb
|
99
142
|
- lib/json_api_toolbox/version.rb
|
143
|
+
- lib/renderizable.rb
|
100
144
|
homepage: http://bitbucket.org/guideinvestimentos/json_api_toolbox
|
101
145
|
licenses: []
|
102
146
|
metadata: {}
|