json_api_toolbox 1.1.0 → 1.2.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: cf01239707604258774757a852af9f3abcb8016a
4
- data.tar.gz: 384bf04edfce2f0433704f59e7e93610d65a7d46
3
+ metadata.gz: 7038c336858ee4ac23c97beaab29792b1ded14e7
4
+ data.tar.gz: 94f61f5dee12030051d01bf760aed3e8976fa78b
5
5
  SHA512:
6
- metadata.gz: 13bd778d079b84c75676ea9eedadbcfe605c1051f5114588133072dfa038e6258f8e22643b340f888decbf117ef308135898e33b2c6304f0c8963a2a10762748
7
- data.tar.gz: 9176fb7de866f06e100bbec0b510fa9afdc27cae6baba03ca23f3290004383d0c80502a3c441c4d69457fb5efdcd4813d4f16020f29482a1a7463f7c165912f8
6
+ metadata.gz: 51f83d87385cce4c0830c6dbede7e304f5ffe47e32de626e5aed3915a54d9eb3962eb8146218bf5689aa0a90c14a27ffc3d57ff0fe7c66710279508b86f80d10
7
+ data.tar.gz: 4056d9c5a4fe7a1457b8a5afb76d98302beac94a0faf5b5afcd02f88aba23403ea133b9f0f0486ac3ddd032d0a4192e61b273f5b425e79c859c8a711ae78a743
@@ -4,7 +4,14 @@ 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]
11
+ ## [1.2.0] - 2020-01-16
12
+ ### Added
13
+ - Added param `headers` to `#get`, `#post`, `#patch`, `#put`, to send extra headers
14
+ - Added new shared example `a failed attempt to retrieve a resource`
8
15
 
9
16
  ## [1.1.0] - 2019-05-28
10
17
  ### 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
@@ -72,6 +72,14 @@ module JsonApiToolbox
72
72
  it { expect(data).to have_key('id') }
73
73
  it { expect(data).to have_key('attributes') }
74
74
  end
75
+
76
+ RSpec.shared_examples 'a failed attempt to retrieve a resource' do
77
+ let(:errors) { JSON.parse(response.body)['errors'] }
78
+
79
+ it 'returns response with error\'s title including RecordNotFound' do
80
+ expect(errors.first['title']).to eq('ActiveRecord::RecordNotFound')
81
+ end
82
+ end
75
83
  end
76
84
  end
77
85
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonApiToolbox
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -6,12 +6,13 @@ 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
@@ -38,7 +39,7 @@ module JsonApiToolbox
38
39
  'Content-Type' => 'application/json'
39
40
  }
40
41
  headers[:params] = body if http_method == :get
41
- headers
42
+ headers.merge(extra_headers)
42
43
  end
43
44
 
44
45
  def build_body
@@ -48,21 +49,21 @@ module JsonApiToolbox
48
49
  end
49
50
 
50
51
  class << self
51
- def get(url: nil, includes: nil, query_string: nil)
52
+ def get(url: nil, includes: nil, query_string: nil, headers: {})
52
53
  body = build_query_string(includes, query_string)
53
- new(:get, url, body).execute
54
+ new(:get, url, body, headers).execute
54
55
  end
55
56
 
56
- def post(url: nil, body: nil)
57
- new(:post, url, body).execute
57
+ def post(url: nil, body: nil, headers: {})
58
+ new(:post, url, body, headers).execute
58
59
  end
59
60
 
60
- def patch(url: nil, body: nil)
61
- new(:patch, url, body).execute
61
+ def patch(url: nil, body: nil, headers: {})
62
+ new(:patch, url, body, headers).execute
62
63
  end
63
64
 
64
- def put(url: nil, body: nil)
65
- new(:put, url, body).execute
65
+ def put(url: nil, body: nil, headers: {})
66
+ new(:put, url, body, headers).execute
66
67
  end
67
68
 
68
69
  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.1.0
4
+ version: 1.2.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: 2019-05-30 00:00:00.000000000 Z
11
+ date: 2020-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack