json_api_toolbox 0.17.0 → 1.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ad7345c4b37202ed05ab044fd99d6bfcd8e6e47
4
- data.tar.gz: b742d7b754794c1ff35f586851562930cc8e068c
3
+ metadata.gz: 7449efd8dd09c17235fcb78b359a4bd7050851a6
4
+ data.tar.gz: c1a744a1eefa42bc00a6bd6cc8172df8a287f008
5
5
  SHA512:
6
- metadata.gz: 94985f43e75243383ef8e5a4e76f3187383a6a52079c005bba484c31df492b256ede3e6e48efae0df2c36bae76890b6d91fb4a29ef1b1cc9ea5534ec7a1aafc4
7
- data.tar.gz: c782dffcd059fa36a99e4f1e49ec9af3182a9f642cd34769e76aa4421c556f37167efcaa860f3c860ad488f60ff93f89e4e588d417589cc59dc28f2c7650588a
6
+ metadata.gz: 90b05221b72edaef537bbf173313ed6159db759203187b7aa2b3b3211a1403e20896e892cb91bb11aaf7534b4c8c56c7982aea99ff053b112eef3312d49124f6
7
+ data.tar.gz: b5ba639f2fa06caeda67ecaaa5b87b01aeaba2cadcc7b99e2249b1dd40053c48c0a45a83bbdf6f979e2c62eb29c320b31bb9c1cba0a14b81c682ce3cbd5aaa1d
@@ -5,6 +5,26 @@ 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
7
  ## [Unreleased]
8
+ ### Added
9
+
10
+ ## [Released]
11
+ ## [1.3.0] - 2020-02-19
12
+ ### Added
13
+ - Remove gem rescue responsibility
14
+
15
+ ## [1.2.0] - 2020-01-16
16
+ ### Added
17
+ - Added param `headers` to `#get`, `#post`, `#patch`, `#put`, to send extra headers
18
+ - Added new shared example `a failed attempt to retrieve a resource`
19
+
20
+ ## [1.1.0] - 2019-05-28
21
+ ### Added
22
+ - Added log with log_toolbox
23
+
24
+ ## [1.0.0] - 2019-05-13
25
+ ### Added
26
+ - Added status internal_server_error on exception renderizable
27
+ - Added status unprocessable_entity on renderizable when object have errors
8
28
 
9
29
  ## [0.17.0] - 2018-08-14
10
30
  ### Added
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
@@ -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.name = 'json_api_toolbox'
10
- spec.version = JsonApiToolbox::VERSION
11
- spec.authors = ['Adriano Bacha']
12
- spec.email = ['abacha@gmail.com']
9
+ spec.required_ruby_version = '>= 2.4.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 = 'Json API usefull tools'
15
- spec.homepage = 'http://bitbucket.org/guideinvestimentos/json_api_toolbox'
15
+ spec.summary = 'Json API usefull tools'
16
+ spec.homepage = 'http://bitbucket.org/guideinvestimentos/json_api_toolbox'
16
17
 
17
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
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', '>= 1.0.0'
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'
@@ -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,9 +33,18 @@ 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
 
@@ -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
- if attr.match?(/_cd$/)
40
+ case attr
41
+ when /_cd$/
41
42
  enums << attr
42
43
  attr.gsub(/_cd/, '')
43
- elsif attr.match?(/_id$|^id$/)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonApiToolbox
4
- VERSION = '0.17.0'
4
+ VERSION = '1.4.0'
5
5
  end
@@ -4,7 +4,7 @@ module JsonApiToolbox
4
4
  module Renderizable
5
5
  def render_object(object, options = {})
6
6
  if object.respond_to?(:errors) && object.errors.any?
7
- render({ jsonapi_errors: object.errors }.merge(options))
7
+ render(build_error_options(object, options))
8
8
  else
9
9
  renderization = { jsonapi: object,
10
10
  include: includes_params,
@@ -20,9 +20,9 @@ module JsonApiToolbox
20
20
  def fields
21
21
  return {} unless fields?
22
22
 
23
- params[:fields].permit!.to_h.map do |field, attributes|
24
- [field, attributes.split(',')]
25
- end.to_h
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
@@ -80,5 +80,11 @@ module JsonApiToolbox
80
80
  {}
81
81
  end
82
82
  end
83
+
84
+ def build_error_options(object, options)
85
+ options = options.merge(status: :unprocessable_entity) if options[:status].blank?
86
+
87
+ { jsonapi_errors: object.errors }.merge(options)
88
+ end
83
89
  end
84
90
  end
@@ -11,7 +11,11 @@ module JsonApiToolbox
11
11
  end
12
12
 
13
13
  def render_any_exception(exception)
14
- puts_build_log_error(exception)
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
@@ -25,7 +29,7 @@ module JsonApiToolbox
25
29
  source: {} },
26
30
  jsonapi_class: {
27
31
  class: { Hash: JsonApiToolbox::Serializables::SerializableException }
28
- } }
32
+ }, status: :internal_server_error }
29
33
  end
30
34
 
31
35
  def puts_build_log_error(error)
@@ -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
- response = request
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: 0.17.0
4
+ version: 1.4.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: 2018-11-29 00:00:00.000000000 Z
11
+ date: 2020-11-16 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: jsonapi-serializable
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.3.0
89
+ version: 1.0.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.3.0
96
+ version: 1.0.0
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: request_store
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -275,7 +289,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
275
289
  requirements:
276
290
  - - ">="
277
291
  - !ruby/object:Gem::Version
278
- version: '0'
292
+ version: 2.4.0
279
293
  required_rubygems_version: !ruby/object:Gem::Requirement
280
294
  requirements:
281
295
  - - ">="