captive-api 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +40 -0
- data/Rakefile +8 -0
- data/app/controllers/captive/api/application_controller.rb +8 -0
- data/app/controllers/concerns/api/pagination_concern.rb +19 -0
- data/app/controllers/concerns/api/render_error_concern.rb +48 -0
- data/app/jobs/captive/api/application_job.rb +6 -0
- data/app/mailers/captive/api/application_mailer.rb +8 -0
- data/app/models/captive/api/application_record.rb +7 -0
- data/config/routes.rb +2 -0
- data/lib/captive/api/engine.rb +8 -0
- data/lib/captive/api/version.rb +5 -0
- data/lib/captive/api.rb +8 -0
- data/lib/tasks/captive/api_tasks.rake +4 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aa071cf3e2b74fa646d99645daa968329f02a82df71344a1dc8bf6f0959b7cab
|
4
|
+
data.tar.gz: 8dec67a5234fcacdcf177922403bffa3dc3ed71b61ff66c49e0013eab5516294
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a35b99b5b24cca392656620b2cb1f3df13e678f00639a9688ddbf43864ddadfcc8cd7bba7bc3c8acac19b44aee89533923f6eff3efd4e382afda746110f6234c
|
7
|
+
data.tar.gz: 7a878b264965496688cc8916f644ab1dfa52ff5ad84e11ce06f77f68fd57cc2da5436ba61d11c8f8b3a93c812f94a2d5096e80fc5227dda75d0b51c3d9b779e1
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2023 Clément Prod'homme
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Captive::Api
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
Implementation :
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
module Api
|
11
|
+
module V1
|
12
|
+
class BaseController < Captive::Api::ApplicationController
|
13
|
+
...
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem "captive-api"
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
```bash
|
28
|
+
$ bundle
|
29
|
+
```
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
```bash
|
33
|
+
$ gem install captive-api
|
34
|
+
```
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
Contribution directions go here.
|
38
|
+
|
39
|
+
## License
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Api
|
4
|
+
module PaginationConcern
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
MAX_ITEMS_PAR_PAGE = 100
|
9
|
+
DEFAULT_NB_ITEMS_PAR_PAGE = 10
|
10
|
+
|
11
|
+
def pagination
|
12
|
+
par_page = (params[:per_page] || DEFAULT_NB_ITEMS_PAR_PAGE).to_i
|
13
|
+
par_page = MAX_ITEMS_PAR_PAGE if par_page > MAX_ITEMS_PAR_PAGE
|
14
|
+
page = (params[:page] || 1).to_i
|
15
|
+
@pagination ||= { page: page, per_page: par_page }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Api
|
4
|
+
module RenderErrorConcern
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
rescue_from ActiveRecord::RecordNotFound, with: :not_found
|
9
|
+
rescue_from CanCan::AccessDenied do |exception|
|
10
|
+
render_error_from_exception(exception, status: :forbidden)
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
|
15
|
+
def render_error_from_object(
|
16
|
+
object,
|
17
|
+
status: :unprocessable_entity,
|
18
|
+
message: 'Erreur de validation'
|
19
|
+
)
|
20
|
+
errors = object.errors.messages
|
21
|
+
Rails.logger.error("Impossible de sauvagarder l'objet #{object.class} : #{errors}")
|
22
|
+
render_error(
|
23
|
+
message: message, errors: errors, status: :unprocessable_entity
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
def render_error_from_exception(exception, status:, message: nil)
|
28
|
+
render_error(
|
29
|
+
message: exception.message, errors: exception.message, status: status
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
def not_found
|
34
|
+
render_error(message: 'Erreur not found', status: :not_found)
|
35
|
+
end
|
36
|
+
|
37
|
+
def render_error(message:, status:, errors: [], code: nil)
|
38
|
+
code ||= status.to_s
|
39
|
+
render json: {
|
40
|
+
message: message,
|
41
|
+
errors: errors,
|
42
|
+
code: code,
|
43
|
+
},
|
44
|
+
status: status
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/config/routes.rb
ADDED
data/lib/captive/api.rb
ADDED
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: captive-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Clément Prod'homme
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-06-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 7.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 7.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cancancan
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.5.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.5.0
|
41
|
+
description: Code commun des API de Captive
|
42
|
+
email:
|
43
|
+
- clement.prod-homme@captive.fr
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- app/controllers/captive/api/application_controller.rb
|
52
|
+
- app/controllers/concerns/api/pagination_concern.rb
|
53
|
+
- app/controllers/concerns/api/render_error_concern.rb
|
54
|
+
- app/jobs/captive/api/application_job.rb
|
55
|
+
- app/mailers/captive/api/application_mailer.rb
|
56
|
+
- app/models/captive/api/application_record.rb
|
57
|
+
- config/routes.rb
|
58
|
+
- lib/captive/api.rb
|
59
|
+
- lib/captive/api/engine.rb
|
60
|
+
- lib/captive/api/version.rb
|
61
|
+
- lib/tasks/captive/api_tasks.rake
|
62
|
+
homepage: https://www.captive.fr/
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata:
|
66
|
+
allowed_push_host: https://rubygems.org/
|
67
|
+
homepage_uri: https://www.captive.fr/
|
68
|
+
source_code_uri: https://github.com/Captive-Studio/captive-api
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubygems_version: 3.3.26
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: Code commun des API de Captive
|
88
|
+
test_files: []
|