json_api_toolbox 1.0.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +30 -0
- data/README.md +15 -0
- data/bitbucket-pipelines.yml +2 -1
- data/json_api_toolbox.gemspec +10 -8
- data/lib/json_api_toolbox.rb +2 -0
- data/lib/json_api_toolbox/paginable.rb +15 -3
- data/lib/json_api_toolbox/spec_support/shared_examples_for_controllers.rb +11 -2
- data/lib/json_api_toolbox/version.rb +1 -1
- data/lib/postable.rb +1 -1
- data/lib/renderizable.rb +5 -6
- data/lib/renderizable_exceptions.rb +5 -1
- data/lib/service.rb +13 -17
- metadata +22 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2bd96792ca86fca2089b5b5f5e837b0d5555e9d8ad549d17f4d2697034288c8c
|
4
|
+
data.tar.gz: be654d88ed537bae350035da7ef869a50dbde04271e491b693815398e3fdfb3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a6b0f774066482fe66821665f9a39cdb55e7f345b765fe1ad7c131994595a2c98fdc8853b0b3314f0e317a450babb944cc1052c92caaaa6405e8710eb86c86e
|
7
|
+
data.tar.gz: 4ab8137ebee0a96c5ee2680b1725a206632d31b69281e0d86be0f4fea7596331a60ef3ea243d7e536781ed1088bb5c489735deef75871c67846f02c87cf88d7b
|
data/CHANGELOG.md
CHANGED
@@ -4,8 +4,38 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## [Unreleased]
|
8
|
+
### Added
|
9
|
+
|
7
10
|
## [Released]
|
8
11
|
|
12
|
+
## [1.5.1] - 2021-05-26
|
13
|
+
### Fixed
|
14
|
+
- Fixed a pagination bug that happened when building the resource_path from the controller name
|
15
|
+
|
16
|
+
## [1.5.0] - 2021-02-04
|
17
|
+
### Added
|
18
|
+
- Require `will_paginate`
|
19
|
+
- Remove limit of version to `log_toolbox` dependence
|
20
|
+
- Update ruby of 2.4.x to 2.5.x
|
21
|
+
|
22
|
+
## [1.4.0] - 2020‑11‑16
|
23
|
+
### Added
|
24
|
+
- Adding per_page limit
|
25
|
+
|
26
|
+
## [1.3.0] - 2020-02-19
|
27
|
+
### Added
|
28
|
+
- Remove gem rescue responsibility
|
29
|
+
|
30
|
+
## [1.2.0] - 2020-01-16
|
31
|
+
### Added
|
32
|
+
- Added param `headers` to `#get`, `#post`, `#patch`, `#put`, to send extra headers
|
33
|
+
- Added new shared example `a failed attempt to retrieve a resource`
|
34
|
+
|
35
|
+
## [1.1.0] - 2019-05-28
|
36
|
+
### Added
|
37
|
+
- Added log with log_toolbox
|
38
|
+
|
9
39
|
## [1.0.0] - 2019-05-13
|
10
40
|
### Added
|
11
41
|
- Added status internal_server_error on exception renderizable
|
data/README.md
CHANGED
@@ -120,6 +120,9 @@ it_behaves_like 'a get with jsonapi with default value of', SomeModel
|
|
120
120
|
it_behaves_like 'a json api response with all relations of', SomeModel
|
121
121
|
validate if all relations are included on your data
|
122
122
|
|
123
|
+
it_behaves_like 'a failed attempt to retrieve a resource'
|
124
|
+
check if the api response will include an error when `find` raise a exception retrieving a resource.
|
125
|
+
|
123
126
|
## Example Tests
|
124
127
|
|
125
128
|
|
@@ -158,6 +161,12 @@ RSpec.describe ManagersController, type: :controller do
|
|
158
161
|
it_behaves_like 'a json api response with included node'
|
159
162
|
it_behaves_like 'a json api response with all relations of', Manager
|
160
163
|
it_behaves_like 'a get with jsonapi with default value of', Manager
|
164
|
+
|
165
|
+
context 'When the manager does not exist' do
|
166
|
+
before { get :show, params: { id: 300 } }
|
167
|
+
|
168
|
+
it_behaves_like 'a failed attempt to retrieve a resource'
|
169
|
+
end
|
161
170
|
end
|
162
171
|
end
|
163
172
|
|
@@ -202,6 +211,12 @@ RSpec.describe ManagersController, type: :controller do
|
|
202
211
|
it 'validates if values were updated' do
|
203
212
|
expect(data['attributes']['name']).to eq(new_name)
|
204
213
|
end
|
214
|
+
|
215
|
+
context 'When the manager does not exist' do
|
216
|
+
before { get :show, params: { id: 300 } }
|
217
|
+
|
218
|
+
it_behaves_like 'a failed attempt to retrieve a resource'
|
219
|
+
end
|
205
220
|
end
|
206
221
|
end
|
207
222
|
end
|
data/bitbucket-pipelines.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
image: ruby:2.
|
1
|
+
image: ruby:2.5.8
|
2
2
|
|
3
3
|
pipelines:
|
4
4
|
default:
|
@@ -15,6 +15,7 @@ pipelines:
|
|
15
15
|
- step:
|
16
16
|
script:
|
17
17
|
- gem build json_api_toolbox.gemspec
|
18
|
+
- mkdir /root/.gem
|
18
19
|
- curl -u $RUBYGEMS_USERNAME:$RUBYGEMS_PASSWORD https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials
|
19
20
|
- chmod 0600 ~/.gem/credentials
|
20
21
|
- gem push $(ls *.gem)
|
data/json_api_toolbox.gemspec
CHANGED
@@ -6,15 +6,16 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
6
6
|
require 'json_api_toolbox/version'
|
7
7
|
|
8
8
|
Gem::Specification.new do |spec|
|
9
|
-
spec.
|
10
|
-
spec.
|
11
|
-
spec.
|
12
|
-
spec.
|
9
|
+
spec.required_ruby_version = '~> 2.5.0'
|
10
|
+
spec.name = 'json_api_toolbox'
|
11
|
+
spec.version = JsonApiToolbox::VERSION
|
12
|
+
spec.authors = ['Adriano Bacha']
|
13
|
+
spec.email = ['abacha@gmail.com']
|
13
14
|
|
14
|
-
spec.summary
|
15
|
-
spec.homepage
|
15
|
+
spec.summary = 'Json API usefull tools'
|
16
|
+
spec.homepage = 'http://bitbucket.org/guideinvestimentos/json_api_toolbox'
|
16
17
|
|
17
|
-
spec.files
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
19
|
f.match(%r{^(test|spec|features)/})
|
19
20
|
end
|
20
21
|
spec.require_paths = ['lib']
|
@@ -22,8 +23,9 @@ Gem::Specification.new do |spec|
|
|
22
23
|
spec.add_dependency 'actionpack', '>= 4.0.0'
|
23
24
|
spec.add_dependency 'activerecord', '>= 4.0.0'
|
24
25
|
spec.add_dependency 'activesupport', '>= 4.0.0'
|
25
|
-
spec.add_dependency 'json-api-vanilla', '~> 1.0.1'
|
26
26
|
spec.add_dependency 'jsonapi-serializable', '>= 0.3.0'
|
27
|
+
spec.add_dependency 'json-api-vanilla', '~> 1.0.1'
|
28
|
+
spec.add_dependency 'log_toolbox'
|
27
29
|
spec.add_dependency 'request_store', '~> 1.3.2'
|
28
30
|
spec.add_dependency 'rest-client', '>= 2.0.1'
|
29
31
|
spec.add_dependency 'rspec', '>= 3.0.0'
|
data/lib/json_api_toolbox.rb
CHANGED
@@ -8,6 +8,8 @@ require 'postable'
|
|
8
8
|
require 'service'
|
9
9
|
require 'jsonapi/serializable'
|
10
10
|
require 'json_api_toolbox/paginable'
|
11
|
+
require 'will_paginate'
|
12
|
+
require 'will_paginate/active_record'
|
11
13
|
|
12
14
|
require_relative 'json_api_toolbox/spec_support/shared_examples_for_controllers'
|
13
15
|
require_relative 'json_api_toolbox/serializables/serializable_enum_option'
|
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
module JsonApiToolbox
|
4
4
|
module Paginable
|
5
|
+
DEFAULT_PER_PAGE = 20
|
6
|
+
MAX_PER_PAGE = 100
|
7
|
+
|
5
8
|
def pagination_meta(collection_resource)
|
6
9
|
{
|
7
10
|
'current-page': collection_resource.current_page,
|
@@ -30,14 +33,23 @@ module JsonApiToolbox
|
|
30
33
|
end
|
31
34
|
end
|
32
35
|
|
36
|
+
def per_page
|
37
|
+
return DEFAULT_PER_PAGE if params[:per_page].nil?
|
38
|
+
|
39
|
+
per_page_limit = (ENV['MAX_PER_PAGE'] || MAX_PER_PAGE).to_i
|
40
|
+
|
41
|
+
[per_page_limit, params[:per_page].to_i].min
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
33
46
|
def create_page_link(link, page)
|
34
|
-
link_path = collection_resource_path(page: page,
|
35
|
-
per_page: params[:per_page])
|
47
|
+
link_path = collection_resource_path(page: page, per_page: per_page)
|
36
48
|
{ link => link_path }
|
37
49
|
end
|
38
50
|
|
39
51
|
def collection_resource_path(options)
|
40
|
-
path_method = "#{
|
52
|
+
path_method = "#{controller_path.parameterize.pluralize.underscore}_path"
|
41
53
|
public_send(path_method, options)
|
42
54
|
end
|
43
55
|
end
|
@@ -37,10 +37,11 @@ module JsonApiToolbox
|
|
37
37
|
let(:model_keys) do
|
38
38
|
enums = []
|
39
39
|
model.new.attributes.keys.map do |attr|
|
40
|
-
|
40
|
+
case attr
|
41
|
+
when /_cd$/
|
41
42
|
enums << attr
|
42
43
|
attr.gsub(/_cd/, '')
|
43
|
-
|
44
|
+
when /_id$|^id$/
|
44
45
|
nil
|
45
46
|
else
|
46
47
|
attr
|
@@ -72,6 +73,14 @@ module JsonApiToolbox
|
|
72
73
|
it { expect(data).to have_key('id') }
|
73
74
|
it { expect(data).to have_key('attributes') }
|
74
75
|
end
|
76
|
+
|
77
|
+
RSpec.shared_examples 'a failed attempt to retrieve a resource' do
|
78
|
+
let(:errors) { JSON.parse(response.body)['errors'] }
|
79
|
+
|
80
|
+
it 'returns response with error\'s title including RecordNotFound' do
|
81
|
+
expect(errors.first['title']).to eq('ActiveRecord::RecordNotFound')
|
82
|
+
end
|
83
|
+
end
|
75
84
|
end
|
76
85
|
end
|
77
86
|
end
|
data/lib/postable.rb
CHANGED
data/lib/renderizable.rb
CHANGED
@@ -20,9 +20,9 @@ module JsonApiToolbox
|
|
20
20
|
def fields
|
21
21
|
return {} unless fields?
|
22
22
|
|
23
|
-
params[:fields].permit!.to_h.
|
24
|
-
|
25
|
-
end
|
23
|
+
params[:fields].permit!.to_h.transform_values do |attributes|
|
24
|
+
attributes.split(',')
|
25
|
+
end
|
26
26
|
end
|
27
27
|
|
28
28
|
def permitted_params
|
@@ -82,9 +82,8 @@ module JsonApiToolbox
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def build_error_options(object, options)
|
85
|
-
if options[:status].blank?
|
86
|
-
|
87
|
-
end
|
85
|
+
options = options.merge(status: :unprocessable_entity) if options[:status].blank?
|
86
|
+
|
88
87
|
{ jsonapi_errors: object.errors }.merge(options)
|
89
88
|
end
|
90
89
|
end
|
@@ -11,7 +11,11 @@ module JsonApiToolbox
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def render_any_exception(exception)
|
14
|
-
|
14
|
+
if Gem.loaded_specs.key?('log_toolbox')
|
15
|
+
log_critical(exception)
|
16
|
+
else
|
17
|
+
puts_build_log_error(exception)
|
18
|
+
end
|
15
19
|
|
16
20
|
render build_jsonapi(exception)
|
17
21
|
end
|
data/lib/service.rb
CHANGED
@@ -6,19 +6,17 @@ require 'json-api-vanilla'
|
|
6
6
|
|
7
7
|
module JsonApiToolbox
|
8
8
|
class Service
|
9
|
-
attr_reader :http_method, :url, :body
|
9
|
+
attr_reader :http_method, :url, :body, :extra_headers
|
10
10
|
|
11
|
-
def initialize(http_method, url, body)
|
11
|
+
def initialize(http_method, url, body, extra_headers = {})
|
12
12
|
@http_method = http_method
|
13
13
|
@url = url
|
14
14
|
@body = body
|
15
|
+
@extra_headers = extra_headers
|
15
16
|
end
|
16
17
|
|
17
18
|
def execute
|
18
|
-
|
19
|
-
return if response.code == 404
|
20
|
-
|
21
|
-
Service.parse_response(response)
|
19
|
+
Service.parse_response(request)
|
22
20
|
end
|
23
21
|
|
24
22
|
def request
|
@@ -28,8 +26,6 @@ module JsonApiToolbox
|
|
28
26
|
payload: build_body,
|
29
27
|
headers: build_header
|
30
28
|
)
|
31
|
-
rescue RestClient::ExceptionWithResponse => e
|
32
|
-
e.response
|
33
29
|
end
|
34
30
|
|
35
31
|
def build_header
|
@@ -38,7 +34,7 @@ module JsonApiToolbox
|
|
38
34
|
'Content-Type' => 'application/json'
|
39
35
|
}
|
40
36
|
headers[:params] = body if http_method == :get
|
41
|
-
headers
|
37
|
+
headers.merge(extra_headers)
|
42
38
|
end
|
43
39
|
|
44
40
|
def build_body
|
@@ -48,21 +44,21 @@ module JsonApiToolbox
|
|
48
44
|
end
|
49
45
|
|
50
46
|
class << self
|
51
|
-
def get(url: nil, includes: nil, query_string: nil)
|
47
|
+
def get(url: nil, includes: nil, query_string: nil, headers: {})
|
52
48
|
body = build_query_string(includes, query_string)
|
53
|
-
new(:get, url, body).execute
|
49
|
+
new(:get, url, body, headers).execute
|
54
50
|
end
|
55
51
|
|
56
|
-
def post(url: nil, body: nil)
|
57
|
-
new(:post, url, body).execute
|
52
|
+
def post(url: nil, body: nil, headers: {})
|
53
|
+
new(:post, url, body, headers).execute
|
58
54
|
end
|
59
55
|
|
60
|
-
def patch(url: nil, body: nil)
|
61
|
-
new(:patch, url, body).execute
|
56
|
+
def patch(url: nil, body: nil, headers: {})
|
57
|
+
new(:patch, url, body, headers).execute
|
62
58
|
end
|
63
59
|
|
64
|
-
def put(url: nil, body: nil)
|
65
|
-
new(:put, url, body).execute
|
60
|
+
def put(url: nil, body: nil, headers: {})
|
61
|
+
new(:put, url, body, headers).execute
|
66
62
|
end
|
67
63
|
|
68
64
|
def parse_response(response)
|
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: 1.
|
4
|
+
version: 1.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adriano Bacha
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 4.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: jsonapi-serializable
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.3.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.3.0
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: json-api-vanilla
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -67,19 +81,19 @@ dependencies:
|
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: 1.0.1
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
84
|
+
name: log_toolbox
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
87
|
- - ">="
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version: 0
|
89
|
+
version: '0'
|
76
90
|
type: :runtime
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
94
|
- - ">="
|
81
95
|
- !ruby/object:Gem::Version
|
82
|
-
version: 0
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: request_store
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -273,17 +287,16 @@ require_paths:
|
|
273
287
|
- lib
|
274
288
|
required_ruby_version: !ruby/object:Gem::Requirement
|
275
289
|
requirements:
|
276
|
-
- - "
|
290
|
+
- - "~>"
|
277
291
|
- !ruby/object:Gem::Version
|
278
|
-
version:
|
292
|
+
version: 2.5.0
|
279
293
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
280
294
|
requirements:
|
281
295
|
- - ">="
|
282
296
|
- !ruby/object:Gem::Version
|
283
297
|
version: '0'
|
284
298
|
requirements: []
|
285
|
-
|
286
|
-
rubygems_version: 2.6.13
|
299
|
+
rubygems_version: 3.0.3
|
287
300
|
signing_key:
|
288
301
|
specification_version: 4
|
289
302
|
summary: Json API usefull tools
|