captive-api 0.1.0.alpha
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 +82 -0
- data/Rakefile +12 -0
- data/app/controllers/captive/api/application_controller.rb +8 -0
- data/app/controllers/concerns/api/pagination_concern.rb +28 -0
- data/app/controllers/concerns/api/render_error_concern.rb +56 -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/initializers/oj.rb +1 -0
- data/config/routes.rb +2 -0
- data/lib/captive/api/engine.rb +8 -0
- data/lib/captive/api.rb +7 -0
- data/lib/tasks/captive/api_tasks.rake +4 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 42ffdc7324be81460128d7033133e99a75d8464ef5810e7b5384faf18e2a9193
|
4
|
+
data.tar.gz: ea16ae814ef611ce847a77485631ffe2e9fcf3bb1e31952ec8ce6624d4763b63
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a482588e1e93b8951cfd4c4551fb7b41af85f285ae9f0ef0c98e284a83c714db5f67b38370f2f4859e45914ba64c9fcf7d2130bef6c69146a969e382b836fc9a
|
7
|
+
data.tar.gz: 773a3091d79e141838d561ddb414ca4fcf14b938c5d22b608e7967add493ae42422b82631fa68615ae00134a79cd0a90d4b48273496bfa18496407e26bacff1c
|
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,82 @@
|
|
1
|
+
# Captive::API
|
2
|
+
|
3
|
+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
4
|
+
[![Gem Version](https://img.shields.io/gem/v/captive-api.svg)](https://rubygems.org/gems/captive-api)
|
5
|
+
|
6
|
+
Code commun des API de Captive
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
How to use my plugin.
|
11
|
+
|
12
|
+
Implementation :
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
module API
|
16
|
+
module V1
|
17
|
+
class BaseController < Captive::API::ApplicationController
|
18
|
+
...
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
The class `Captive::API::ApplicationController` includes 2 concerns :
|
25
|
+
|
26
|
+
| Nom du concern | Description |
|
27
|
+
|:-----|:--------:|
|
28
|
+
| [pagination](https://github.com/Captive-Studio/captive-api/blob/main/app/controllers/concerns/api/pagination_concern.rb) | Logique de pagination pour les index |
|
29
|
+
| [render error](https://github.com/Captive-Studio/captive-api/blob/main/app/controllers/concerns/api/render_error_concern.rb) | Attrape certaines erreurs pour retourner une page d'erreur en json. Permet aussi d'utiliser les méthodes pour rendre des pages d'erreur json manuellement |
|
30
|
+
|
31
|
+
### PaginationConcern
|
32
|
+
|
33
|
+
#### `#pagination`
|
34
|
+
|
35
|
+
You can use the pagination method like this :
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
@tenues = @tenues.page(pagination[:page])
|
39
|
+
.per(pagination[:per_page])
|
40
|
+
```
|
41
|
+
|
42
|
+
*The method `page` and `per` come from [will_paginate](https://github.com/mislav/will_paginate)*
|
43
|
+
|
44
|
+
The pagination method return an hash like this :
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
{ page: 1, per_page: 20 }
|
48
|
+
```
|
49
|
+
|
50
|
+
⚠️ The first page is `1` and not `0` !
|
51
|
+
|
52
|
+
### RenderErrorConcern
|
53
|
+
|
54
|
+
TODO
|
55
|
+
|
56
|
+
## Installation
|
57
|
+
|
58
|
+
Add this line to your application's Gemfile:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
gem "captive-api"
|
62
|
+
```
|
63
|
+
|
64
|
+
And then execute:
|
65
|
+
|
66
|
+
```bash
|
67
|
+
bundle
|
68
|
+
```
|
69
|
+
|
70
|
+
Or install it yourself as:
|
71
|
+
|
72
|
+
```bash
|
73
|
+
gem install captive-api
|
74
|
+
```
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
Contribution directions go here.
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
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,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module API
|
4
|
+
##
|
5
|
+
# Défini le nombre max d'items par page à 100
|
6
|
+
# `MAX_ITEMS_PAR_PAGE = 100`
|
7
|
+
#
|
8
|
+
# Défini le nombre par défaut d'items par page à 20
|
9
|
+
# DEFAULT_NB_ITEMS_PAR_PAGE = 20
|
10
|
+
module PaginationConcern
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
|
13
|
+
included do |base|
|
14
|
+
base.const_set :MAX_ITEMS_PAR_PAGE, 100
|
15
|
+
base.const_set :DEFAULT_NB_ITEMS_PAR_PAGE, 20
|
16
|
+
|
17
|
+
def pagination
|
18
|
+
@pagination ||=
|
19
|
+
begin
|
20
|
+
par_page = (params[:per_page] || self.class::DEFAULT_NB_ITEMS_PAR_PAGE).to_i
|
21
|
+
par_page = self.class::MAX_ITEMS_PAR_PAGE if par_page > self.class::MAX_ITEMS_PAR_PAGE
|
22
|
+
page = (params[:page] || 1).to_i
|
23
|
+
{ page: page, per_page: par_page }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module API
|
4
|
+
##
|
5
|
+
# Permet de catcher des erreurs pour rendre une page d'erreur en json
|
6
|
+
module RenderErrorConcern
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
rescue_from ActiveRecord::RecordNotFound, with: :not_found
|
11
|
+
rescue_from ActionController::ParameterMissing, with: :handle_parameter_missing
|
12
|
+
|
13
|
+
rescue_from CanCan::AccessDenied do |exception|
|
14
|
+
render_error_from_exception(exception, status: :forbidden)
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def render_error_from_object(
|
20
|
+
object,
|
21
|
+
status: :unprocessable_entity,
|
22
|
+
message: "Erreur de validation"
|
23
|
+
)
|
24
|
+
errors = object.errors.messages
|
25
|
+
Rails.logger.error("Impossible de sauvagarder l'objet #{object.class} : #{errors}")
|
26
|
+
render_error(
|
27
|
+
message: message, errors: errors, status: status
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
def render_error_from_exception(exception, status:, message: nil)
|
32
|
+
render_error(
|
33
|
+
message: exception.message, errors: exception.message, status: status
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
def handle_parameter_missing(exception)
|
38
|
+
render_error_from_exception(exception, status: :bad_request)
|
39
|
+
end
|
40
|
+
|
41
|
+
def not_found(exception)
|
42
|
+
render_error(message: "#{exception.model} not found", status: :not_found)
|
43
|
+
end
|
44
|
+
|
45
|
+
def render_error(message:, status:, errors: [], code: nil)
|
46
|
+
code ||= status.to_s
|
47
|
+
render json: {
|
48
|
+
message: message,
|
49
|
+
errors: errors,
|
50
|
+
code: code,
|
51
|
+
},
|
52
|
+
status: status
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Oj.optimize_rails
|
data/config/routes.rb
ADDED
data/lib/captive/api.rb
ADDED
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: captive-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.alpha
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Captive
|
8
|
+
- Clément Prod'homme
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2023-10-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 7.0.0
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 7.0.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: cancancan
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 3.5.0
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 3.5.0
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: oj
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 3.15.0
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 3.15.0
|
56
|
+
description: Code commun des API de Captive
|
57
|
+
email:
|
58
|
+
- clement.prod-homme@captive.fr
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- MIT-LICENSE
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- app/controllers/captive/api/application_controller.rb
|
67
|
+
- app/controllers/concerns/api/pagination_concern.rb
|
68
|
+
- app/controllers/concerns/api/render_error_concern.rb
|
69
|
+
- app/jobs/captive/api/application_job.rb
|
70
|
+
- app/mailers/captive/api/application_mailer.rb
|
71
|
+
- app/models/captive/api/application_record.rb
|
72
|
+
- config/initializers/oj.rb
|
73
|
+
- config/routes.rb
|
74
|
+
- lib/captive/api.rb
|
75
|
+
- lib/captive/api/engine.rb
|
76
|
+
- lib/tasks/captive/api_tasks.rake
|
77
|
+
homepage: https://www.captive.fr/
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata:
|
81
|
+
homepage_uri: https://www.captive.fr/
|
82
|
+
source_code_uri: https://github.com/Captive-Studio/captive-sdk/tree/v0.1.0.alpha/captive-api
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.3.1
|
97
|
+
requirements: []
|
98
|
+
rubygems_version: 3.3.26
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Code commun des API de Captive
|
102
|
+
test_files: []
|