json_api_toolbox 1.3.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: af58f7d3d2dc113db2fd9a34cad4e6861aee871f
4
- data.tar.gz: 93a83be02bb1c09da703f1aa274a9005761f8b6f
2
+ SHA256:
3
+ metadata.gz: 07cee6578d3ec72b85799bb88a7b8f0fee45c63950516209dca50d65ee0d4bcb
4
+ data.tar.gz: 9e6171341031bba7809a8337256d882e05ee5b11095bedd58e2f7ff2f62000d3
5
5
  SHA512:
6
- metadata.gz: 90a1513719aa4bfe47a83dd800c1e23d0a12bb9856ca1b490d90a564b2d3d3928052fb4ca85ab6cbd9585349481c5de14a95b2f0a2e86fa5284de7eaa635983f
7
- data.tar.gz: 4365c231d6a15e8f55509270011d09203cb0e3b47c9bb5024caeee7795a3d0b7f5a9d400166ed1c568f63e852204623556991606ecd562061106b58015158dc7
6
+ metadata.gz: 24ab8d780ec3b8a21ace2100b29c8f60593bc01cef3f23dbe81a6d3993687aa7c3a93a22dd6e04c9647de17b62f693452c8e7b1a40941d191459879f89342c35
7
+ data.tar.gz: 5aa57f37c3c11b462755ab90a3d85986ade02bdbb9d1d5a78a3f6ac5df025f5ee73eeb23ba9011277ace4a3a3742626a516ef8d62573976156d30cdc50bec496
data/.gitignore CHANGED
@@ -1,6 +1,5 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
- /Gemfile.lock
4
3
  /_yardoc/
5
4
  /coverage/
6
5
  /doc/
@@ -10,3 +9,5 @@
10
9
 
11
10
  # rspec failure tracking
12
11
  .rspec_status
12
+
13
+ .rubocop.base.yml
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ inherit_from:
2
+ - '.rubocop.base.yml'
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.6
6
+ SuggestExtensions: false
data/.version ADDED
@@ -0,0 +1 @@
1
+ 2.0.1
data/CHANGELOG.md CHANGED
@@ -5,9 +5,45 @@ 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
+
9
+ ## [2.0.1] - 2021-08-13
10
+ ### Fixed
11
+ - Fixed a constant loading problem on rails
12
+
13
+ ## [2.0.0] - 2021-06-23
8
14
  ### Added
15
+ - New README file describing the gem and it's uses
16
+ - Added support for default rubocop config
17
+ - New setup script
18
+ - Added docker to run rubocop and rspec
19
+
20
+ ### Changed
21
+ - New 2.0 branch! With better support for semver and ruby updates
22
+ - Moved the rspec examples file for easier manual import
23
+ - Updated all dependencies
24
+ - Moved some dependencies into development dependencies
25
+
26
+ ### Removed
27
+ - Removed support for ruby 2.5 series
28
+ - Removed unneeded files (old CI files, ruby version lock, etc…)
29
+ - Removed auto import for rspec examples. It now need to be added manually on `rails_helper.rb`
9
30
 
10
31
  ## [Released]
32
+
33
+ ## [1.5.1] - 2021-05-26
34
+ ### Fixed
35
+ - Fixed a pagination bug that happened when building the resource_path from the controller name
36
+
37
+ ## [1.5.0] - 2021-02-04
38
+ ### Added
39
+ - Require `will_paginate`
40
+ - Remove limit of version to `log_toolbox` dependence
41
+ - Update ruby of 2.4.x to 2.5.x
42
+
43
+ ## [1.4.0] - 2020‑11‑16
44
+ ### Added
45
+ - Adding per_page limit
46
+
11
47
  ## [1.3.0] - 2020-02-19
12
48
  ### Added
13
49
  - Remove gem rescue responsibility
data/Dockerfile ADDED
@@ -0,0 +1,18 @@
1
+ FROM ruby:2.6-alpine
2
+
3
+ RUN apk update \
4
+ && apk upgrade \
5
+ && apk add --update \
6
+ git \
7
+ openssh \
8
+ build-base \
9
+ && rm -rf /var/cache/apk/*
10
+
11
+ RUN gem update --system
12
+
13
+ WORKDIR /gem
14
+ COPY . .
15
+
16
+ ENTRYPOINT ["sh", "docker-entrypoint.sh"]
17
+
18
+ CMD ["rake", "lint-and-test"]
data/Gemfile.lock ADDED
@@ -0,0 +1,239 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ json_api_toolbox (2.0.1)
5
+ activerecord (>= 5.1.0)
6
+ activesupport (>= 5.1.0)
7
+ json-api-vanilla
8
+ jsonapi-serializable
9
+ log_toolbox
10
+ request_store
11
+ rest-client
12
+ will_paginate
13
+
14
+ GEM
15
+ remote: https://rubygems.org/
16
+ specs:
17
+ actioncable (6.1.4)
18
+ actionpack (= 6.1.4)
19
+ activesupport (= 6.1.4)
20
+ nio4r (~> 2.0)
21
+ websocket-driver (>= 0.6.1)
22
+ actionmailbox (6.1.4)
23
+ actionpack (= 6.1.4)
24
+ activejob (= 6.1.4)
25
+ activerecord (= 6.1.4)
26
+ activestorage (= 6.1.4)
27
+ activesupport (= 6.1.4)
28
+ mail (>= 2.7.1)
29
+ actionmailer (6.1.4)
30
+ actionpack (= 6.1.4)
31
+ actionview (= 6.1.4)
32
+ activejob (= 6.1.4)
33
+ activesupport (= 6.1.4)
34
+ mail (~> 2.5, >= 2.5.4)
35
+ rails-dom-testing (~> 2.0)
36
+ actionpack (6.1.4)
37
+ actionview (= 6.1.4)
38
+ activesupport (= 6.1.4)
39
+ rack (~> 2.0, >= 2.0.9)
40
+ rack-test (>= 0.6.3)
41
+ rails-dom-testing (~> 2.0)
42
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
43
+ actiontext (6.1.4)
44
+ actionpack (= 6.1.4)
45
+ activerecord (= 6.1.4)
46
+ activestorage (= 6.1.4)
47
+ activesupport (= 6.1.4)
48
+ nokogiri (>= 1.8.5)
49
+ actionview (6.1.4)
50
+ activesupport (= 6.1.4)
51
+ builder (~> 3.1)
52
+ erubi (~> 1.4)
53
+ rails-dom-testing (~> 2.0)
54
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
55
+ activejob (6.1.4)
56
+ activesupport (= 6.1.4)
57
+ globalid (>= 0.3.6)
58
+ activemodel (6.1.4)
59
+ activesupport (= 6.1.4)
60
+ activerecord (6.1.4)
61
+ activemodel (= 6.1.4)
62
+ activesupport (= 6.1.4)
63
+ activestorage (6.1.4)
64
+ actionpack (= 6.1.4)
65
+ activejob (= 6.1.4)
66
+ activerecord (= 6.1.4)
67
+ activesupport (= 6.1.4)
68
+ marcel (~> 1.0.0)
69
+ mini_mime (>= 1.1.0)
70
+ activesupport (6.1.4)
71
+ concurrent-ruby (~> 1.0, >= 1.0.2)
72
+ i18n (>= 1.6, < 2)
73
+ minitest (>= 5.1)
74
+ tzinfo (~> 2.0)
75
+ zeitwerk (~> 2.3)
76
+ ast (2.4.2)
77
+ builder (3.2.4)
78
+ byebug (11.1.3)
79
+ coderay (1.1.3)
80
+ concurrent-ruby (1.1.9)
81
+ crass (1.0.6)
82
+ diff-lcs (1.4.4)
83
+ domain_name (0.5.20190701)
84
+ unf (>= 0.0.5, < 1.0.0)
85
+ erubi (1.10.0)
86
+ globalid (0.5.2)
87
+ activesupport (>= 5.0)
88
+ http-accept (1.7.0)
89
+ http-cookie (1.0.4)
90
+ domain_name (~> 0.5)
91
+ i18n (1.8.10)
92
+ concurrent-ruby (~> 1.0)
93
+ json-api-vanilla (1.0.2)
94
+ jsonapi-renderer (0.2.2)
95
+ jsonapi-serializable (0.3.1)
96
+ jsonapi-renderer (~> 0.2.0)
97
+ log_toolbox (1.3.11)
98
+ lograge (>= 0.11.1)
99
+ logstash-event (>= 1.2.02)
100
+ logstash-logger (>= 0.26.1)
101
+ rails (>= 5.0)
102
+ lograge (0.11.2)
103
+ actionpack (>= 4)
104
+ activesupport (>= 4)
105
+ railties (>= 4)
106
+ request_store (~> 1.0)
107
+ logstash-event (1.2.02)
108
+ logstash-logger (0.26.1)
109
+ logstash-event (~> 1.2)
110
+ loofah (2.12.0)
111
+ crass (~> 1.0.2)
112
+ nokogiri (>= 1.5.9)
113
+ mail (2.7.1)
114
+ mini_mime (>= 0.1.1)
115
+ marcel (1.0.1)
116
+ method_source (1.0.0)
117
+ mime-types (3.3.1)
118
+ mime-types-data (~> 3.2015)
119
+ mime-types-data (3.2021.0704)
120
+ mini_mime (1.1.0)
121
+ minitest (5.14.4)
122
+ netrc (0.11.0)
123
+ nio4r (2.5.8)
124
+ nokogiri (1.12.3-x86_64-darwin)
125
+ racc (~> 1.4)
126
+ nokogiri (1.12.3-x86_64-linux)
127
+ racc (~> 1.4)
128
+ parallel (1.20.1)
129
+ parser (3.0.2.0)
130
+ ast (~> 2.4.1)
131
+ pry (0.13.1)
132
+ coderay (~> 1.1)
133
+ method_source (~> 1.0)
134
+ pry-byebug (3.9.0)
135
+ byebug (~> 11.0)
136
+ pry (~> 0.13.0)
137
+ racc (1.5.2)
138
+ rack (2.2.3)
139
+ rack-test (1.1.0)
140
+ rack (>= 1.0, < 3)
141
+ rails (6.1.4)
142
+ actioncable (= 6.1.4)
143
+ actionmailbox (= 6.1.4)
144
+ actionmailer (= 6.1.4)
145
+ actionpack (= 6.1.4)
146
+ actiontext (= 6.1.4)
147
+ actionview (= 6.1.4)
148
+ activejob (= 6.1.4)
149
+ activemodel (= 6.1.4)
150
+ activerecord (= 6.1.4)
151
+ activestorage (= 6.1.4)
152
+ activesupport (= 6.1.4)
153
+ bundler (>= 1.15.0)
154
+ railties (= 6.1.4)
155
+ sprockets-rails (>= 2.0.0)
156
+ rails-dom-testing (2.0.3)
157
+ activesupport (>= 4.2.0)
158
+ nokogiri (>= 1.6)
159
+ rails-html-sanitizer (1.3.0)
160
+ loofah (~> 2.3)
161
+ railties (6.1.4)
162
+ actionpack (= 6.1.4)
163
+ activesupport (= 6.1.4)
164
+ method_source
165
+ rake (>= 0.13)
166
+ thor (~> 1.0)
167
+ rainbow (3.0.0)
168
+ rake (13.0.6)
169
+ regexp_parser (2.1.1)
170
+ request_store (1.5.0)
171
+ rack (>= 1.4)
172
+ rest-client (2.1.0)
173
+ http-accept (>= 1.7.0, < 2.0)
174
+ http-cookie (>= 1.0.2, < 2.0)
175
+ mime-types (>= 1.16, < 4.0)
176
+ netrc (~> 0.8)
177
+ rexml (3.2.5)
178
+ rspec (3.10.0)
179
+ rspec-core (~> 3.10.0)
180
+ rspec-expectations (~> 3.10.0)
181
+ rspec-mocks (~> 3.10.0)
182
+ rspec-core (3.10.1)
183
+ rspec-support (~> 3.10.0)
184
+ rspec-expectations (3.10.1)
185
+ diff-lcs (>= 1.2.0, < 2.0)
186
+ rspec-support (~> 3.10.0)
187
+ rspec-mocks (3.10.2)
188
+ diff-lcs (>= 1.2.0, < 2.0)
189
+ rspec-support (~> 3.10.0)
190
+ rspec-support (3.10.2)
191
+ rubocop (1.19.0)
192
+ parallel (~> 1.10)
193
+ parser (>= 3.0.0.0)
194
+ rainbow (>= 2.2.2, < 4.0)
195
+ regexp_parser (>= 1.8, < 3.0)
196
+ rexml
197
+ rubocop-ast (>= 1.9.1, < 2.0)
198
+ ruby-progressbar (~> 1.7)
199
+ unicode-display_width (>= 1.4.0, < 3.0)
200
+ rubocop-ast (1.10.0)
201
+ parser (>= 3.0.1.1)
202
+ ruby-progressbar (1.11.0)
203
+ shoulda-matchers (5.0.0)
204
+ activesupport (>= 5.2.0)
205
+ sprockets (4.0.2)
206
+ concurrent-ruby (~> 1.0)
207
+ rack (> 1, < 3)
208
+ sprockets-rails (3.2.2)
209
+ actionpack (>= 4.0)
210
+ activesupport (>= 4.0)
211
+ sprockets (>= 3.0.0)
212
+ thor (1.1.0)
213
+ tzinfo (2.0.4)
214
+ concurrent-ruby (~> 1.0)
215
+ unf (0.1.4)
216
+ unf_ext
217
+ unf_ext (0.0.7.7)
218
+ unicode-display_width (2.0.0)
219
+ websocket-driver (0.7.5)
220
+ websocket-extensions (>= 0.1.0)
221
+ websocket-extensions (0.1.5)
222
+ will_paginate (3.3.1)
223
+ zeitwerk (2.4.2)
224
+
225
+ PLATFORMS
226
+ x86_64-darwin-20
227
+ x86_64-linux
228
+
229
+ DEPENDENCIES
230
+ bundler
231
+ json_api_toolbox!
232
+ pry-byebug
233
+ rake
234
+ rspec
235
+ rubocop
236
+ shoulda-matchers
237
+
238
+ BUNDLED WITH
239
+ 2.2.25
data/README.md CHANGED
@@ -1,42 +1,32 @@
1
1
  # JsonApiToolbox
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/json_api_toolbox`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This toolbox serve as a JSON API helper within Guide TI specs
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ The current features are:
6
+ - Integrates the gems `jsonapi-serializable`, `json-api-vanilla` and `log_toolbox`
7
+ - Rspec support through a series of shares examples
8
+ - Suport for JSON API services
6
9
 
7
10
  ## Installation
8
11
 
9
- Add this line to your application's Gemfile:
12
+ Add this line in your `Gemfile`:
10
13
 
11
- ```ruby
12
- gem 'json_api_toolbox'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install json_api_toolbox
14
+ gem 'json_api_toolbox'
22
15
 
23
- ## Usage
16
+ then execute:
24
17
 
25
- TODO: Write usage instructions here
18
+ $ bundle install
26
19
 
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
20
+ For rspec support, add this line on your `rails_helper.rb`:
30
21
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
22
+ require 'json_api_toolbox/rspec'
32
23
 
33
- ## Contributing
24
+ ## Usage Examples
34
25
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/abacha/json_api_toolbox.
26
+ You can check some gem uses within the examples below:
36
27
 
37
- ## Examples
28
+ ### Service
38
29
 
39
- #Service
40
30
  ```ruby
41
31
  class MyService < JsonApiToolbox::Service
42
32
  BASE_URL = 'http://localhost:3000/my_model'
@@ -61,9 +51,10 @@ class MyService < JsonApiToolbox::Service
61
51
  end
62
52
  ```
63
53
 
64
- #Renderizable
54
+ ### Renderizable
55
+
56
+ A simple render:
65
57
 
66
- Render common
67
58
  ```ruby
68
59
  render_object(Quota.all)
69
60
  ```
@@ -76,7 +67,7 @@ new Hash, the key must be 'Hash' and the value must be your Serializable.
76
67
  render_object(QuotaGap.all, class: { Hash: SerializableQuotaGap })
77
68
  ```
78
69
 
79
- #Paginable
70
+ ### Paginable
80
71
 
81
72
  To render a collection with a json api pagination, you will need to send a Hash
82
73
  that contains two keys called 'links' and 'meta', and its values must be Hashs.
@@ -96,35 +87,33 @@ class MyController
96
87
  end
97
88
  ```
98
89
 
99
- ##ShareExamplesForControllers
90
+ ### Rspec Shared Examples
100
91
 
101
- it_behaves_like 'a http method'
102
- validate if your response have http status 200
92
+ `it_behaves_like 'a http method'`
93
+ - validate if your response have http status 200
103
94
 
104
- it_behaves_like 'a get method'
105
- validate if your response have http status 200
106
- validate if your response responde to body method
95
+ `it_behaves_like 'a get method'`
96
+ - validate if your response have http status 200
97
+ - validate if your response responde to body method
107
98
 
108
- it_behaves_like 'a json api response with included node'
109
- validate if yor response.body has included node
99
+ `it_behaves_like 'a json api response with included node'`
100
+ - validate if yor response.body has included node
110
101
 
111
- it_behaves_like 'a get with jsonapi with default value of', SomeModel
112
- validate if all attributes are present on your data
113
- obs:
114
- 1 - for enums who have '_cd' in the end of attribute name, '_cd' is removed.
115
- ex: enum_cd => enum
116
- 2 - id attributes are removed from validation
117
- 3 - for attributes who have _id in the end of attribute name, they are
118
- removed from validation
102
+ `it_behaves_like 'a get with jsonapi with default value of', SomeModel`
103
+ - validate if all attributes are present on your data based on the current criteria:
104
+ 1. for enums who have `_cd` in the end of attribute name, `_cd` is removed.
119
105
 
120
- it_behaves_like 'a json api response with all relations of', SomeModel
121
- validate if all relations are included on your data
106
+ ex: `enum_cd` => `enum`
107
+ 1. id attributes are removed from validation
108
+ 1. for attributes who have `_id` in the end of attribute name, they are removed from validation
122
109
 
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.
110
+ `it_behaves_like 'a json api response with all relations of', SomeModel`
111
+ - validate if all relations are included on your data
125
112
 
126
- ## Example Tests
113
+ `it_behaves_like 'a failed attempt to retrieve a resource'`
114
+ - checks if the api response will include an error when `find` raise a exception retrieving a resource.
127
115
 
116
+ ## Test example
128
117
 
129
118
  ```ruby
130
119
  # frozen_string_literal: true
@@ -221,3 +210,26 @@ RSpec.describe ManagersController, type: :controller do
221
210
  end
222
211
  end
223
212
  ```
213
+
214
+ ## Development
215
+
216
+ To change the gem is simple, just clone this repository and run the `bin/setup` script
217
+
218
+ Then you can make your changes and check them with `rspec` or `rubocop .`.
219
+
220
+
221
+ ### On docker
222
+
223
+ #### Building
224
+ ```bash
225
+ docker build . -t json-api-toolbox-dev
226
+ ```
227
+
228
+ #### Running tests
229
+ ```bash
230
+ docker run --rm -it -v "$(echo $HOME)/.ssh:/root/.ssh" -v "$(pwd):/gem" json-api-toolbox-dev
231
+ ```
232
+
233
+ ## Contributing
234
+
235
+ > To be written
data/Rakefile CHANGED
@@ -1,6 +1,11 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
+ require "rubocop/rake_task"
3
4
 
4
5
  RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new(:lint)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
9
+
10
+ desc 'Execute both RuboCop and RSpec'
11
+ task "lint-and-test" => [:lint, :spec]
data/bin/docker ADDED
@@ -0,0 +1,61 @@
1
+ #! /usr/bin/env sh
2
+
3
+ NAMETAG="json-api-toolbox:dev"
4
+ COMMAND=${1:-help}
5
+ shift
6
+
7
+ command_help() {
8
+ echo "
9
+ Usage: docker COMMAND [code]
10
+
11
+ A simple script to control the project container
12
+
13
+ Commands:
14
+ run Run [code] on a new container
15
+ exec Run [code] on the current active container
16
+ build Build the project image
17
+ help Show this help message
18
+ "
19
+ }
20
+
21
+ command_build() {
22
+ docker build -t $NAMETAG --ssh=default .
23
+ }
24
+
25
+ config_ssh_agent() {
26
+ if [ uname == "Darwin" ]; then
27
+ AUTH_PATH="/run/host-services/ssh-auth.sock"
28
+ else
29
+ AUTH_PATH=$(echo $SSH_AUTH_SOCK)
30
+ fi
31
+
32
+ echo "-v $(echo $HOME)/.ssh:/root/.ssh \
33
+ -v $(echo $AUTH_PATH):/ssh-agent \
34
+ -e SSH_AUTH_SOCK=/ssh-agent \
35
+ "
36
+ }
37
+
38
+ command_run() {
39
+ [[ $1 == "--build" ]] && { command_build; shift; }
40
+
41
+ docker volume create bundler_cache > /dev/null
42
+ docker run -it \
43
+ -v bundler_cache:/usr/local/bundle \
44
+ -v $(pwd):/gem \
45
+ $(config_ssh_agent) \
46
+ $NAMETAG
47
+ $@
48
+ }
49
+
50
+ command_exec() {
51
+ [[ $1 == "--build" ]] && { command_build; shift; }
52
+
53
+ docker exec $NAMETAG $@
54
+ }
55
+
56
+ case $COMMAND in
57
+ build) command_build "$@" ;;
58
+ exec) command_exec "$@" ;;
59
+ run) command_run "$@" ;;
60
+ *) command_help "$@" ;;
61
+ esac
data/bin/setup.sh ADDED
@@ -0,0 +1,57 @@
1
+ #! /bin/sh
2
+
3
+ DEFAULT_REPO=git@bitbucket.org:guideinvestimentos/rails_defaults.git
4
+ STRICT=${1:-no}
5
+ ONE_WEEK_AGO=$(( $(date +%s) - 604800 ))
6
+
7
+ if [ -f .rubocop.base.yml ]; then
8
+ FILE_TIME=$(date -r .rubocop.base.yml +%s)
9
+ fi
10
+
11
+ info(){
12
+ printf "\033[34;1m$1\033[0m\n"
13
+ }
14
+
15
+ step(){
16
+ printf " - $1: \0337\n"
17
+ }
18
+
19
+ success(){
20
+ printf "\0338\033[1A\033[32;1mOK!\033[0m\n\033[2K"
21
+ }
22
+
23
+ skipped(){
24
+ printf "\0338\033[1A\033[33;1mSKIPPED ($1)\033[0m\n\033[2K"
25
+ }
26
+
27
+ error() {
28
+ printf "\0338\033[1A\033[31;1mERRO!\033[0m\n\033[2K"
29
+ printf "\033[31m $1\033[0m\n"
30
+ [ $STRICT -eq "--strict" ] && exit $2
31
+ }
32
+
33
+ info "Configurando o projeto\n"
34
+
35
+ step "Atualizando configuração base do Rubocop"
36
+
37
+ if [ ${FILE_TIME:-0} -lt $ONE_WEEK_AGO ]; then
38
+ RUBOCOP_FILE=$(git archive --remote=$DEFAULT_REPO HEAD .rubocop.yml 2> /dev/null | tar xO 2> /dev/null)
39
+
40
+ if [ -z "$RUBOCOP_FILE" ]; then
41
+ error "Não foi possível baixar o arquivo de configuração" 1
42
+ else
43
+ echo "$RUBOCOP_FILE" > .rubocop.base.yml
44
+ success
45
+ fi
46
+ else
47
+ skipped "Arquivo recente"
48
+ fi
49
+
50
+ step "Instalando dependências"
51
+
52
+ bundle check 2>&1 > /dev/null && skipped "up-to date" || {
53
+ RESULT=$(bundle install 2>&1 > /dev/null)
54
+ [ $? -eq "0" ] && success || error "$RESULT" 2
55
+ }
56
+
57
+ info "\nConfiguração executada com sucesso"
@@ -1,20 +1,56 @@
1
- image: ruby:2.4.1
1
+ image: ruby:2.6
2
2
 
3
- pipelines:
4
- default:
5
- - step:
3
+ definitions:
4
+ caches:
5
+ bundler-26: vendor/bundle
6
+ bundler-27: vendor/bundle
7
+ bundler-30: vendor/bundle
8
+
9
+ steps:
10
+ - step: &test
11
+ name: Testing
12
+ script:
13
+ - apt-get install git
14
+ - gem update --system
15
+ - bundle config set --local path "vendor/bundle"
16
+ - bin/setup.sh --strict
17
+ - bundle exec rake lint-and-test
18
+ - step: &release
19
+ name: Release gem
6
20
  script:
7
- - git archive --remote=git@bitbucket.org:guideinvestimentos/rails_defaults.git HEAD .rubocop.yml | tar -x
21
+ - apt-get install git
22
+ - gem update --system
23
+ - bundle config set --local path "vendor/bundle"
8
24
  - bundle install
9
- - gem install rubocop
10
- - rspec -fdoc
11
- - rubocop .
25
+ - mkdir -p ~/.gem/
26
+ - curl -sSL -u $RUBYGEMS_USERNAME:$RUBYGEMS_PASSWORD https://rubygems.org/api/v1/api_key.yaml -o ~/.gem/credentials
27
+ - chmod 0600 ~/.gem/credentials
28
+ - bundle exec rake release --trace
12
29
 
30
+ pipelines:
31
+ pull-requests:
32
+ '**':
33
+ - parallel:
34
+ - step:
35
+ <<: *test
36
+ name: Ruby 2.6
37
+ image: ruby:2.6
38
+ caches:
39
+ - bundler-26
40
+ - step:
41
+ <<: *test
42
+ name: Ruby 2.7
43
+ image: ruby:2.7
44
+ caches:
45
+ - bundler-27
46
+ - step:
47
+ <<: *test
48
+ name: Ruby 3.0
49
+ image: ruby:3.0
50
+ caches:
51
+ - bundler-30
13
52
  branches:
14
53
  master:
15
- - step:
16
- script:
17
- - gem build json_api_toolbox.gemspec
18
- - curl -u $RUBYGEMS_USERNAME:$RUBYGEMS_PASSWORD https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials
19
- - chmod 0600 ~/.gem/credentials
20
- - gem push $(ls *.gem)
54
+ - step: *release
55
+ 'branch-1.x':
56
+ - step: *release
@@ -0,0 +1,3 @@
1
+ #! /bin/sh
2
+
3
+ . bin/setup.sh && bundle exec "$@"
@@ -1,41 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Metrics/BlockLength
4
3
  lib = File.expand_path('lib', __dir__)
5
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
5
  require 'json_api_toolbox/version'
7
6
 
8
7
  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']
8
+ spec.required_ruby_version = '>= 2.6.0'
9
+ spec.name = 'json_api_toolbox'
10
+ spec.version = JsonApiToolbox::VERSION
13
11
 
14
- spec.summary = 'Json API usefull tools'
15
- spec.homepage = 'http://bitbucket.org/guideinvestimentos/json_api_toolbox'
12
+ spec.authors = ['Adriano Bacha']
13
+ spec.email = ['abacha@gmail.com']
16
14
 
17
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ spec.summary = 'Json API usefull tools'
16
+ spec.homepage = 'http://bitbucket.org/guideinvestimentos/json_api_toolbox'
17
+
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']
21
22
 
22
- spec.add_dependency 'actionpack', '>= 4.0.0'
23
- spec.add_dependency 'activerecord', '>= 4.0.0'
24
- spec.add_dependency 'activesupport', '>= 4.0.0'
25
- spec.add_dependency 'json-api-vanilla', '~> 1.0.1'
26
- spec.add_dependency 'jsonapi-serializable', '>= 0.3.0'
27
- spec.add_dependency 'log_toolbox', '>= 1.0.0'
28
- spec.add_dependency 'request_store', '~> 1.3.2'
29
- spec.add_dependency 'rest-client', '>= 2.0.1'
30
- spec.add_dependency 'rspec', '>= 3.0.0'
31
- spec.add_dependency 'will_paginate', '>= 3.1.0'
23
+ spec.add_dependency 'activerecord', '>= 5.1.0'
24
+ spec.add_dependency 'activesupport', '>= 5.1.0'
25
+ spec.add_dependency 'jsonapi-serializable'
26
+ spec.add_dependency 'json-api-vanilla'
27
+ spec.add_dependency 'log_toolbox'
28
+ spec.add_dependency 'request_store'
29
+ spec.add_dependency 'rest-client'
30
+ spec.add_dependency 'will_paginate'
32
31
 
33
32
  spec.add_development_dependency 'bundler'
34
- spec.add_development_dependency 'json'
35
- spec.add_development_dependency 'pry'
36
33
  spec.add_development_dependency 'pry-byebug'
37
34
  spec.add_development_dependency 'rake'
35
+ spec.add_development_dependency 'rspec'
38
36
  spec.add_development_dependency 'rubocop'
39
- spec.add_development_dependency 'shoulda-matchers', '~> 3.1'
37
+ spec.add_development_dependency 'shoulda-matchers'
40
38
  end
41
- # rubocop:enable Metrics/BlockLength
@@ -8,8 +8,9 @@ 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
- require_relative 'json_api_toolbox/spec_support/shared_examples_for_controllers'
13
14
  require_relative 'json_api_toolbox/serializables/serializable_enum_option'
14
15
  require_relative 'json_api_toolbox/serializables/serializable_job'
15
16
  require_relative 'json_api_toolbox/serializables/serializable_version'
@@ -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 = "#{controller_name.pluralize.underscore}_path"
52
+ path_method = "#{controller_path.parameterize.pluralize.underscore}_path"
41
53
  public_send(path_method, options)
42
54
  end
43
55
  end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonApiToolbox
4
+ module Rspec
5
+ RSpec.shared_examples 'a http method' do
6
+ it 'returns http success' do
7
+ expect(response).to have_http_status(:success)
8
+ end
9
+ end
10
+
11
+ RSpec.shared_examples 'a post with status 422 Unprocessable Entity' do
12
+ it 'returns http code 422 Unprocessable Entity' do
13
+ expect(response).to have_http_status(422)
14
+ end
15
+ end
16
+
17
+ RSpec.shared_examples 'a get method' do
18
+ it_behaves_like 'a http method'
19
+
20
+ it 'response with JSON body' do
21
+ expect { response.body }.not_to raise_exception
22
+ end
23
+ end
24
+
25
+ RSpec.shared_examples 'a json api response with included node' do
26
+ it 'returns with included node' do
27
+ expect(JSON.parse(response.body)['included'].count).not_to eql(0)
28
+ end
29
+ end
30
+
31
+ RSpec.shared_examples 'a get with jsonapi with default value of' do |model|
32
+ let(:attributes) { data['attributes'].keys }
33
+ let(:model_keys) do
34
+ enums = []
35
+ model.new.attributes.keys.map do |attr|
36
+ case attr
37
+ when /_cd$/
38
+ enums << attr
39
+ attr.gsub(/_cd/, '')
40
+ when /_id$|^id$/
41
+ nil
42
+ else
43
+ attr
44
+ end
45
+ end.compact + enums
46
+ end
47
+
48
+ it 'returns response with default values' do
49
+ expect((model_keys & attributes).size).to eq(model_keys.size)
50
+ end
51
+ end
52
+
53
+ RSpec.shared_examples 'a json api response with all relations of' do |model|
54
+ let(:attributes) { data['relationships'].keys }
55
+ let(:relation_keys) { model.reflections.keys }
56
+
57
+ it 'returns response with default relationships' do
58
+ expect(attributes).to match_array(relation_keys)
59
+ end
60
+ end
61
+
62
+ RSpec.shared_examples 'a json api response' do
63
+ let(:body) { JSON.parse(response.body) }
64
+ let(:data) { body['data'].first }
65
+
66
+ it { expect(body).to have_key('data') }
67
+ it { expect(data).to have_key('type') }
68
+ it { expect(data).to have_key('id') }
69
+ it { expect(data).to have_key('attributes') }
70
+ end
71
+
72
+ RSpec.shared_examples 'a failed attempt to retrieve a resource' do
73
+ let(:errors) { JSON.parse(response.body)['errors'] }
74
+
75
+ it 'returns response with error\'s title including RecordNotFound' do
76
+ expect(errors.first['title']).to eq('ActiveRecord::RecordNotFound')
77
+ end
78
+ end
79
+ end
80
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonApiToolbox
4
- VERSION = '1.3.0'
4
+ VERSION = '2.0.1'
5
5
  end
data/lib/postable.rb CHANGED
@@ -7,11 +7,11 @@ module JsonApiToolbox
7
7
  associations = model.reflect_on_all_associations.map(&:name)
8
8
  normalized_hash = hash.map do |key, value|
9
9
  child_model = association_class(model, key) if value.is_a? Hash
10
- key = associations.include?(key.to_sym) ? "#{key}_attributes" : key
10
+ key = "#{key}_attributes" if associations.include?(key.to_sym)
11
11
  hash_value = check_child_association(value, key, model, child_model)
12
12
  [key, hash_value]
13
13
  end
14
- Hash[normalized_hash]
14
+ normalized_hash.to_h
15
15
  end
16
16
 
17
17
  private
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.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
@@ -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
- options = options.merge(status: :unprocessable_entity)
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
metadata CHANGED
@@ -1,155 +1,127 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.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: 2020-02-19 00:00:00.000000000 Z
11
+ date: 2021-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: actionpack
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.0.0
19
+ version: 5.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.0.0
26
+ version: 5.1.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: activerecord
28
+ name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 4.0.0
33
+ version: 5.1.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 4.0.0
40
+ version: 5.1.0
41
41
  - !ruby/object:Gem::Dependency
42
- name: activesupport
42
+ name: jsonapi-serializable
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 4.0.0
47
+ version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 4.0.0
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: json-api-vanilla
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: 1.0.1
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: 1.0.1
69
- - !ruby/object:Gem::Dependency
70
- name: jsonapi-serializable
71
57
  requirement: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - ">="
74
60
  - !ruby/object:Gem::Version
75
- version: 0.3.0
61
+ version: '0'
76
62
  type: :runtime
77
63
  prerelease: false
78
64
  version_requirements: !ruby/object:Gem::Requirement
79
65
  requirements:
80
66
  - - ">="
81
67
  - !ruby/object:Gem::Version
82
- version: 0.3.0
68
+ version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: log_toolbox
85
71
  requirement: !ruby/object:Gem::Requirement
86
72
  requirements:
87
73
  - - ">="
88
74
  - !ruby/object:Gem::Version
89
- version: 1.0.0
75
+ version: '0'
90
76
  type: :runtime
91
77
  prerelease: false
92
78
  version_requirements: !ruby/object:Gem::Requirement
93
79
  requirements:
94
80
  - - ">="
95
81
  - !ruby/object:Gem::Version
96
- version: 1.0.0
82
+ version: '0'
97
83
  - !ruby/object:Gem::Dependency
98
84
  name: request_store
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: 1.3.2
104
- type: :runtime
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: 1.3.2
111
- - !ruby/object:Gem::Dependency
112
- name: rest-client
113
85
  requirement: !ruby/object:Gem::Requirement
114
86
  requirements:
115
87
  - - ">="
116
88
  - !ruby/object:Gem::Version
117
- version: 2.0.1
89
+ version: '0'
118
90
  type: :runtime
119
91
  prerelease: false
120
92
  version_requirements: !ruby/object:Gem::Requirement
121
93
  requirements:
122
94
  - - ">="
123
95
  - !ruby/object:Gem::Version
124
- version: 2.0.1
96
+ version: '0'
125
97
  - !ruby/object:Gem::Dependency
126
- name: rspec
98
+ name: rest-client
127
99
  requirement: !ruby/object:Gem::Requirement
128
100
  requirements:
129
101
  - - ">="
130
102
  - !ruby/object:Gem::Version
131
- version: 3.0.0
103
+ version: '0'
132
104
  type: :runtime
133
105
  prerelease: false
134
106
  version_requirements: !ruby/object:Gem::Requirement
135
107
  requirements:
136
108
  - - ">="
137
109
  - !ruby/object:Gem::Version
138
- version: 3.0.0
110
+ version: '0'
139
111
  - !ruby/object:Gem::Dependency
140
112
  name: will_paginate
141
113
  requirement: !ruby/object:Gem::Requirement
142
114
  requirements:
143
115
  - - ">="
144
116
  - !ruby/object:Gem::Version
145
- version: 3.1.0
117
+ version: '0'
146
118
  type: :runtime
147
119
  prerelease: false
148
120
  version_requirements: !ruby/object:Gem::Requirement
149
121
  requirements:
150
122
  - - ">="
151
123
  - !ruby/object:Gem::Version
152
- version: 3.1.0
124
+ version: '0'
153
125
  - !ruby/object:Gem::Dependency
154
126
  name: bundler
155
127
  requirement: !ruby/object:Gem::Requirement
@@ -165,7 +137,7 @@ dependencies:
165
137
  - !ruby/object:Gem::Version
166
138
  version: '0'
167
139
  - !ruby/object:Gem::Dependency
168
- name: json
140
+ name: pry-byebug
169
141
  requirement: !ruby/object:Gem::Requirement
170
142
  requirements:
171
143
  - - ">="
@@ -179,7 +151,7 @@ dependencies:
179
151
  - !ruby/object:Gem::Version
180
152
  version: '0'
181
153
  - !ruby/object:Gem::Dependency
182
- name: pry
154
+ name: rake
183
155
  requirement: !ruby/object:Gem::Requirement
184
156
  requirements:
185
157
  - - ">="
@@ -193,7 +165,7 @@ dependencies:
193
165
  - !ruby/object:Gem::Version
194
166
  version: '0'
195
167
  - !ruby/object:Gem::Dependency
196
- name: pry-byebug
168
+ name: rspec
197
169
  requirement: !ruby/object:Gem::Requirement
198
170
  requirements:
199
171
  - - ">="
@@ -207,7 +179,7 @@ dependencies:
207
179
  - !ruby/object:Gem::Version
208
180
  version: '0'
209
181
  - !ruby/object:Gem::Dependency
210
- name: rake
182
+ name: rubocop
211
183
  requirement: !ruby/object:Gem::Requirement
212
184
  requirements:
213
185
  - - ">="
@@ -221,7 +193,7 @@ dependencies:
221
193
  - !ruby/object:Gem::Version
222
194
  version: '0'
223
195
  - !ruby/object:Gem::Dependency
224
- name: rubocop
196
+ name: shoulda-matchers
225
197
  requirement: !ruby/object:Gem::Requirement
226
198
  requirements:
227
199
  - - ">="
@@ -234,20 +206,6 @@ dependencies:
234
206
  - - ">="
235
207
  - !ruby/object:Gem::Version
236
208
  version: '0'
237
- - !ruby/object:Gem::Dependency
238
- name: shoulda-matchers
239
- requirement: !ruby/object:Gem::Requirement
240
- requirements:
241
- - - "~>"
242
- - !ruby/object:Gem::Version
243
- version: '3.1'
244
- type: :development
245
- prerelease: false
246
- version_requirements: !ruby/object:Gem::Requirement
247
- requirements:
248
- - - "~>"
249
- - !ruby/object:Gem::Version
250
- version: '3.1'
251
209
  description:
252
210
  email:
253
211
  - abacha@gmail.com
@@ -257,22 +215,27 @@ extra_rdoc_files: []
257
215
  files:
258
216
  - ".gitignore"
259
217
  - ".rspec"
260
- - ".ruby-version"
261
- - ".travis.yml"
218
+ - ".rubocop.yml"
219
+ - ".version"
262
220
  - CHANGELOG.md
221
+ - Dockerfile
263
222
  - Gemfile
223
+ - Gemfile.lock
264
224
  - README.md
265
225
  - Rakefile
226
+ - bin/docker
227
+ - bin/setup.sh
266
228
  - bitbucket-pipelines.yml
229
+ - docker-entrypoint.sh
267
230
  - json_api_toolbox.gemspec
268
231
  - lib/enums.rb
269
232
  - lib/json_api_toolbox.rb
270
233
  - lib/json_api_toolbox/paginable.rb
234
+ - lib/json_api_toolbox/rspec.rb
271
235
  - lib/json_api_toolbox/serializables/serializable_enum_option.rb
272
236
  - lib/json_api_toolbox/serializables/serializable_exception.rb
273
237
  - lib/json_api_toolbox/serializables/serializable_job.rb
274
238
  - lib/json_api_toolbox/serializables/serializable_version.rb
275
- - lib/json_api_toolbox/spec_support/shared_examples_for_controllers.rb
276
239
  - lib/json_api_toolbox/version.rb
277
240
  - lib/postable.rb
278
241
  - lib/renderizable.rb
@@ -289,15 +252,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
289
252
  requirements:
290
253
  - - ">="
291
254
  - !ruby/object:Gem::Version
292
- version: '0'
255
+ version: 2.6.0
293
256
  required_rubygems_version: !ruby/object:Gem::Requirement
294
257
  requirements:
295
258
  - - ">="
296
259
  - !ruby/object:Gem::Version
297
260
  version: '0'
298
261
  requirements: []
299
- rubyforge_project:
300
- rubygems_version: 2.6.13
262
+ rubygems_version: 3.2.25
301
263
  signing_key:
302
264
  specification_version: 4
303
265
  summary: Json API usefull tools
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- ruby-2.5.0
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.2.5
5
- before_install: gem install bundler -v 1.15.1
@@ -1,85 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rspec'
4
-
5
- module JsonApiToolbox
6
- module SpecSupport
7
- module SharedExamplesForControllers
8
- RSpec.shared_examples 'a http method' do
9
- it 'returns http success' do
10
- expect(response).to have_http_status(:success)
11
- end
12
- end
13
-
14
- RSpec.shared_examples 'a post with status 422 Unprocessable Entity' do
15
- it 'returns http code 422 Unprocessable Entity' do
16
- expect(response).to have_http_status(422)
17
- end
18
- end
19
-
20
- RSpec.shared_examples 'a get method' do
21
- it_behaves_like 'a http method'
22
-
23
- it 'response with JSON body' do
24
- expect { response.body }.not_to raise_exception
25
- end
26
- end
27
-
28
- RSpec.shared_examples 'a json api response with included node' do
29
- it 'returns with included node' do
30
- expect(JSON.parse(response.body)['included'].count).not_to eql(0)
31
- end
32
- end
33
-
34
- RSpec.
35
- shared_examples 'a get with jsonapi with default value of' do |model|
36
- let(:attributes) { data['attributes'].keys }
37
- let(:model_keys) do
38
- enums = []
39
- model.new.attributes.keys.map do |attr|
40
- if attr.match?(/_cd$/)
41
- enums << attr
42
- attr.gsub(/_cd/, '')
43
- elsif attr.match?(/_id$|^id$/)
44
- nil
45
- else
46
- attr
47
- end
48
- end.compact + enums
49
- end
50
-
51
- it 'returns response with default values' do
52
- expect((model_keys & attributes).size).to eq(model_keys.size)
53
- end
54
- end
55
-
56
- RSpec.
57
- shared_examples 'a json api response with all relations of' do |model|
58
- let(:attributes) { data['relationships'].keys }
59
- let(:relation_keys) { model.reflections.keys }
60
-
61
- it 'returns response with default relationships' do
62
- expect(attributes).to match_array(relation_keys)
63
- end
64
- end
65
-
66
- RSpec.shared_examples 'a json api response' do
67
- let(:body) { JSON.parse(response.body) }
68
- let(:data) { body['data'].first }
69
-
70
- it { expect(body).to have_key('data') }
71
- it { expect(data).to have_key('type') }
72
- it { expect(data).to have_key('id') }
73
- it { expect(data).to have_key('attributes') }
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
83
- end
84
- end
85
- end