grape-utils 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +16 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +20 -0
- data/Gemfile.lock +123 -0
- data/LICENSE.txt +21 -0
- data/README.md +124 -0
- data/Rakefile +12 -0
- data/grape-utils.gemspec +41 -0
- data/lib/grape-utils.rb +3 -0
- data/lib/grape_utils/helpers.rb +11 -0
- data/lib/grape_utils/templates/create.rb +25 -0
- data/lib/grape_utils/templates/crud.rb +30 -0
- data/lib/grape_utils/templates/delete.rb +24 -0
- data/lib/grape_utils/templates/index.rb +27 -0
- data/lib/grape_utils/templates/show.rb +22 -0
- data/lib/grape_utils/templates/update.rb +27 -0
- data/lib/grape_utils/version.rb +7 -0
- data/lib/grape_utils.rb +10 -0
- data/sig/grape/utils.rbs +6 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2b06a9ea4aa2398f2825175fe9992e39c5353e44c6d80838ab2458975d8d3ce1
|
4
|
+
data.tar.gz: b0a71606237e35d2c923571fb7ffb1f0b1a9afdb7fdae7d111a93ded902e4bb4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c33be00c3a8b029b9d03d278f9cba246056f243cfe7fe70386aa0b8bacd64a9cc0c5d9c1efbfc5a55101f1d99fba2edf224ad9b648b00523dcc65176b0db7402
|
7
|
+
data.tar.gz: 0d7b08fd62151141aeb3a438b95d932fbec51132b81e10736afc85b341e9a6873d170f16b76ebe4105b5d8dc647bef03525e4deb063d294edef144e69eb07a02
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
8
|
+
Style/StringLiteralsInInterpolation:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Style/Documentation:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Layout/LineLength:
|
16
|
+
Max: 120
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at acprieto41@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in grape-utils.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rack-test"
|
11
|
+
gem "rspec", "~> 3.0"
|
12
|
+
|
13
|
+
gem "rubocop", "~> 1.21"
|
14
|
+
|
15
|
+
# API
|
16
|
+
gem "grape"
|
17
|
+
gem "grape-entity"
|
18
|
+
|
19
|
+
# API Pagination
|
20
|
+
gem "grape-kaminari"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
grape-utils (0.1.0)
|
5
|
+
grape
|
6
|
+
grape-entity
|
7
|
+
grape-kaminari
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activesupport (7.0.8)
|
13
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
+
i18n (>= 1.6, < 2)
|
15
|
+
minitest (>= 5.1)
|
16
|
+
tzinfo (~> 2.0)
|
17
|
+
ast (2.4.2)
|
18
|
+
builder (3.2.4)
|
19
|
+
concurrent-ruby (1.2.2)
|
20
|
+
diff-lcs (1.5.0)
|
21
|
+
dry-core (1.0.1)
|
22
|
+
concurrent-ruby (~> 1.0)
|
23
|
+
zeitwerk (~> 2.6)
|
24
|
+
dry-inflector (1.0.0)
|
25
|
+
dry-logic (1.5.0)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
dry-core (~> 1.0, < 2)
|
28
|
+
zeitwerk (~> 2.6)
|
29
|
+
dry-types (1.7.1)
|
30
|
+
concurrent-ruby (~> 1.0)
|
31
|
+
dry-core (~> 1.0)
|
32
|
+
dry-inflector (~> 1.0)
|
33
|
+
dry-logic (~> 1.4)
|
34
|
+
zeitwerk (~> 2.6)
|
35
|
+
grape (1.8.0)
|
36
|
+
activesupport (>= 5)
|
37
|
+
builder
|
38
|
+
dry-types (>= 1.1)
|
39
|
+
mustermann-grape (~> 1.0.0)
|
40
|
+
rack (>= 1.3.0)
|
41
|
+
rack-accept
|
42
|
+
grape-entity (1.0.0)
|
43
|
+
activesupport (>= 3.0.0)
|
44
|
+
multi_json (>= 1.3.2)
|
45
|
+
grape-kaminari (0.4.5)
|
46
|
+
grape (>= 1.6.1)
|
47
|
+
kaminari-grape
|
48
|
+
i18n (1.14.1)
|
49
|
+
concurrent-ruby (~> 1.0)
|
50
|
+
json (2.6.3)
|
51
|
+
kaminari-core (1.2.2)
|
52
|
+
kaminari-grape (1.0.1)
|
53
|
+
grape
|
54
|
+
kaminari-core (~> 1.0)
|
55
|
+
language_server-protocol (3.17.0.3)
|
56
|
+
minitest (5.20.0)
|
57
|
+
multi_json (1.15.0)
|
58
|
+
mustermann (3.0.0)
|
59
|
+
ruby2_keywords (~> 0.0.1)
|
60
|
+
mustermann-grape (1.0.2)
|
61
|
+
mustermann (>= 1.0.0)
|
62
|
+
parallel (1.23.0)
|
63
|
+
parser (3.2.2.4)
|
64
|
+
ast (~> 2.4.1)
|
65
|
+
racc
|
66
|
+
racc (1.7.1)
|
67
|
+
rack (2.2.8)
|
68
|
+
rack-accept (0.4.5)
|
69
|
+
rack (>= 0.4)
|
70
|
+
rack-test (1.1.0)
|
71
|
+
rack (>= 1.0, < 3)
|
72
|
+
rainbow (3.1.1)
|
73
|
+
rake (13.1.0)
|
74
|
+
regexp_parser (2.8.2)
|
75
|
+
rexml (3.2.6)
|
76
|
+
rspec (3.12.0)
|
77
|
+
rspec-core (~> 3.12.0)
|
78
|
+
rspec-expectations (~> 3.12.0)
|
79
|
+
rspec-mocks (~> 3.12.0)
|
80
|
+
rspec-core (3.12.2)
|
81
|
+
rspec-support (~> 3.12.0)
|
82
|
+
rspec-expectations (3.12.3)
|
83
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
84
|
+
rspec-support (~> 3.12.0)
|
85
|
+
rspec-mocks (3.12.5)
|
86
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
87
|
+
rspec-support (~> 3.12.0)
|
88
|
+
rspec-support (3.12.1)
|
89
|
+
rubocop (1.57.2)
|
90
|
+
json (~> 2.3)
|
91
|
+
language_server-protocol (>= 3.17.0)
|
92
|
+
parallel (~> 1.10)
|
93
|
+
parser (>= 3.2.2.4)
|
94
|
+
rainbow (>= 2.2.2, < 4.0)
|
95
|
+
regexp_parser (>= 1.8, < 3.0)
|
96
|
+
rexml (>= 3.2.5, < 4.0)
|
97
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
98
|
+
ruby-progressbar (~> 1.7)
|
99
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
100
|
+
rubocop-ast (1.30.0)
|
101
|
+
parser (>= 3.2.1.0)
|
102
|
+
ruby-progressbar (1.13.0)
|
103
|
+
ruby2_keywords (0.0.5)
|
104
|
+
tzinfo (2.0.6)
|
105
|
+
concurrent-ruby (~> 1.0)
|
106
|
+
unicode-display_width (2.5.0)
|
107
|
+
zeitwerk (2.6.12)
|
108
|
+
|
109
|
+
PLATFORMS
|
110
|
+
x86_64-linux
|
111
|
+
|
112
|
+
DEPENDENCIES
|
113
|
+
grape
|
114
|
+
grape-entity
|
115
|
+
grape-kaminari
|
116
|
+
grape-utils!
|
117
|
+
rack-test
|
118
|
+
rake (~> 13.0)
|
119
|
+
rspec (~> 3.0)
|
120
|
+
rubocop (~> 1.21)
|
121
|
+
|
122
|
+
BUNDLED WITH
|
123
|
+
2.4.10
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Nix41
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# Grape::Utils
|
2
|
+
|
3
|
+
This gem offers some templates for basic CRUD endpoints using grape, grape-entity and grape-kaminari
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add grape-utils
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install grape-utils
|
14
|
+
|
15
|
+
## Basic Usage
|
16
|
+
|
17
|
+
Given you already have a Grape API set up, and a model `Foo`, with a `Entities:Foo`, you can Use `::Grape::Utils::Templates::Crud` as follows:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
module AwesomeAPI
|
21
|
+
module Resources
|
22
|
+
class Foo < Base
|
23
|
+
|
24
|
+
resource :foos do
|
25
|
+
mount ::Grape::Utils::Templates::Crud, with: {
|
26
|
+
# The collection that will be used for the index endpoint
|
27
|
+
index: Foo.all,
|
28
|
+
# The ActiveModel instance that will be used to create, update and find_by!
|
29
|
+
model: Foo,
|
30
|
+
# The entity that will be used as a return type
|
31
|
+
entity: Entities::Foo,
|
32
|
+
# The required params for the Create endpoint
|
33
|
+
required_params: [:name]
|
34
|
+
}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
This will mount 5 endpoints in your API: Index, Show, Create, Update and Delete
|
42
|
+
|
43
|
+
Each of this templates can also be accessed individually if needed:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
module AwesomeAPI
|
47
|
+
module Resources
|
48
|
+
class Foo < Base
|
49
|
+
|
50
|
+
resource :foos do
|
51
|
+
mount Grape::Utils::Templates::Index, with: { index: Foo.all, entity: Entities::Foo }
|
52
|
+
mount Grape::Utils::Templates::Show, with: { model: Foo, entity: Entities::Foo }
|
53
|
+
mount Grape::Utils::Templates::Create, with: { model: Foo, entity: Entities::Foo}
|
54
|
+
mount Grape::Utils::Templates::Update, with: { model: Foo.all, entity: Entities::Foo }
|
55
|
+
mount Grape::Utils::Templates::Delete, with: { model: Foo, entity: Entities::Foo }
|
56
|
+
|
57
|
+
route_param :id, type: Integer do
|
58
|
+
get 'nested_foo' do
|
59
|
+
#Any other relevant logic
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
This way you can customize one of the methods to fit your situation while still using the rest as standard
|
69
|
+
|
70
|
+
|
71
|
+
## Entities and Params
|
72
|
+
|
73
|
+
The Entities Definition is used in 2 essential ways: first as a return type, and second as the param definition for the POST and PATCH endpoints
|
74
|
+
|
75
|
+
From the Entities exposed fields, only those with a `documentation` property will be used as a param on the POST/PATCH endpoints
|
76
|
+
|
77
|
+
So given:
|
78
|
+
```ruby
|
79
|
+
module AwesomeAPI
|
80
|
+
module Entities
|
81
|
+
class Foo < Grape::Entity
|
82
|
+
expose :id
|
83
|
+
expose :name, documentation: { type: String }
|
84
|
+
expose :configuration, documentation: { type: JSON }
|
85
|
+
expose :created_at
|
86
|
+
expose :updated_at
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
Only `name` and `configuration` will be accepted as params in POST and PATCH endpoints, with it's corresponding types
|
93
|
+
|
94
|
+
### Required Params
|
95
|
+
|
96
|
+
You can define which of the documented exposed fields of the Entity are required for the creation of the resource
|
97
|
+
by mounting `::Grape::Utils::Templates::Crud` or `::Grape::Utils::Templates::Create` with `required_params: String[]`
|
98
|
+
|
99
|
+
### ID Column
|
100
|
+
|
101
|
+
By default, it is assumed that the resources will be identified by the column `id`
|
102
|
+
This will be used as the route parameter of the Show, Update and Delete endpoints, and by means of which, the resource will be located
|
103
|
+
If you want to change the name of the column to use as id, you can
|
104
|
+
mount the relevant templates `with{... column_id: 'field_key' }`
|
105
|
+
|
106
|
+
## Development
|
107
|
+
|
108
|
+
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.
|
109
|
+
|
110
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
111
|
+
|
112
|
+
## Contributing
|
113
|
+
|
114
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Nix41/grape-utils. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/Nix41/grape-utils/blob/master/CODE_OF_CONDUCT.md).
|
115
|
+
|
116
|
+
Particularly I am still working on getting the gem 100% covered
|
117
|
+
|
118
|
+
## License
|
119
|
+
|
120
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
121
|
+
|
122
|
+
## Code of Conduct
|
123
|
+
|
124
|
+
Everyone interacting in the Grape::Utils project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/grape-utils/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/grape-utils.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift File.expand_path("lib", __dir__)
|
4
|
+
require_relative "lib/grape_utils/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "grape-utils"
|
8
|
+
spec.version = Grape::Utils::VERSION
|
9
|
+
spec.authors = ["Nix41"]
|
10
|
+
spec.email = ["acprieto41@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Grape Utils for building APIs"
|
13
|
+
# spec.description = "TODO: Write a longer description or delete this line."
|
14
|
+
spec.homepage = "https://github.com/Nix41/grape-utils"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.required_ruby_version = ">= 2.6.0"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = "https://github.com/Nix41/grape-utils"
|
20
|
+
spec.metadata["changelog_uri"] = "https://github.com/Nix41/grape-utils/blob/main/CHANGELOG.md"
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(__dir__) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_dependency "grape"
|
34
|
+
spec.add_dependency "grape-entity"
|
35
|
+
spec.add_dependency "grape-kaminari"
|
36
|
+
spec.add_development_dependency "rspec"
|
37
|
+
spec.add_development_dependency "rubocop"
|
38
|
+
|
39
|
+
# For more information and examples about making a new gem, check out our
|
40
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
41
|
+
end
|
data/lib/grape-utils.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Grape
|
4
|
+
module Utils
|
5
|
+
module Templates
|
6
|
+
# Adds POST method to create a new instance of the resource
|
7
|
+
class Create < ::Grape::API
|
8
|
+
helpers Grape::Utils::Helpers
|
9
|
+
|
10
|
+
mounted do
|
11
|
+
desc "Creates an instance of #{configuration[:model].class_name}"
|
12
|
+
params do
|
13
|
+
requires :none, except: configuration[:required_params],
|
14
|
+
using: configuration[:entity].documentation
|
15
|
+
end
|
16
|
+
post do
|
17
|
+
resource = configuration[:model].create!(permitted_params)
|
18
|
+
|
19
|
+
present resource, with: configuration[:entity]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Grape
|
4
|
+
module Utils
|
5
|
+
module Templates
|
6
|
+
# Adds all requests necessary to CRUD the resource
|
7
|
+
class Crud < ::Grape::API
|
8
|
+
mounted do
|
9
|
+
mount Templates::Index, with: { index: configuration[:index], entity: configuration[:entity] }
|
10
|
+
|
11
|
+
mount Templates::Create, with: { model: configuration[:model],
|
12
|
+
entity: configuration[:entity],
|
13
|
+
required_params: configuration[:required_params] }
|
14
|
+
|
15
|
+
mount Templates::Show, with: { model: configuration[:model],
|
16
|
+
entity: configuration[:entity],
|
17
|
+
column_id: configuration[:column_id] }
|
18
|
+
|
19
|
+
mount Templates::Update, with: { model: configuration[:model],
|
20
|
+
entity: configuration[:entity],
|
21
|
+
column_id: configuration[:column_id] }
|
22
|
+
|
23
|
+
mount Templates::Delete, with: { model: configuration[:model],
|
24
|
+
entity: configuration[:entity],
|
25
|
+
column_id: configuration[:column_id] }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Grape
|
4
|
+
module Utils
|
5
|
+
module Templates
|
6
|
+
# Adds DELETE method to delete an instance of the resource
|
7
|
+
class Delete < ::Grape::API
|
8
|
+
helpers Grape::Utils::Helpers
|
9
|
+
|
10
|
+
mounted do
|
11
|
+
route_param :id, type: String do
|
12
|
+
desc "Deletes an instances of #{configuration[:model].class_name}"
|
13
|
+
delete do
|
14
|
+
resource = configuration[:model].find_by!((configuration[:column_id] || :id) => params[:id])
|
15
|
+
resource.destroy!
|
16
|
+
|
17
|
+
status(204)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "grape-kaminari"
|
4
|
+
|
5
|
+
module Grape
|
6
|
+
module Utils
|
7
|
+
module Templates
|
8
|
+
# Adds a GET method to index all the scope of the resource
|
9
|
+
class Index < ::Grape::API
|
10
|
+
include ::Grape::Kaminari
|
11
|
+
helpers Grape::Utils::Helpers
|
12
|
+
|
13
|
+
mounted do
|
14
|
+
desc "Lists all #{configuration[:model].class_name}"
|
15
|
+
params do
|
16
|
+
use :pagination, per_page: 10, max_per_page: 30
|
17
|
+
end
|
18
|
+
get do
|
19
|
+
list = configuration[:index]
|
20
|
+
|
21
|
+
present list, with: configuration[:entity]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Grape
|
4
|
+
module Utils
|
5
|
+
module Templates
|
6
|
+
# Adds a GET method to show details of an instance eof the resource
|
7
|
+
class Show < ::Grape::API
|
8
|
+
helpers Grape::Utils::Helpers
|
9
|
+
|
10
|
+
mounted do
|
11
|
+
route_param :id, type: String do
|
12
|
+
desc "Details of a #{configuration[:model].class_name}"
|
13
|
+
get do
|
14
|
+
resource = configuration[:model].find_by!((configuration[:column_id] || :id) => params[:id])
|
15
|
+
present resource, with: configuration[:entity]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Grape
|
4
|
+
module Utils
|
5
|
+
module Templates
|
6
|
+
# Adds a PATCH method to update an instance of the resource
|
7
|
+
class Update < ::Grape::API
|
8
|
+
helpers Grape::Utils::Helpers
|
9
|
+
|
10
|
+
mounted do
|
11
|
+
route_param :id, type: String do
|
12
|
+
desc "Updates a #{configuration[:model].class_name}"
|
13
|
+
params do
|
14
|
+
requires :none, using: configuration[:entity].documentation
|
15
|
+
end
|
16
|
+
patch do
|
17
|
+
resource = configuration[:model].find_by!((configuration[:column_id] || :id) => params[:id])
|
18
|
+
resource.update!(permitted_params.except(:id))
|
19
|
+
|
20
|
+
present resource, with: configuration[:entity]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/grape_utils.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "grape_utils/version"
|
4
|
+
require_relative "grape_utils/helpers"
|
5
|
+
require_relative "grape_utils/templates/create"
|
6
|
+
require_relative "grape_utils/templates/crud"
|
7
|
+
require_relative "grape_utils/templates/delete"
|
8
|
+
require_relative "grape_utils/templates/index"
|
9
|
+
require_relative "grape_utils/templates/update"
|
10
|
+
require_relative "grape_utils/templates/show"
|
data/sig/grape/utils.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grape-utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nix41
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-01-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: grape
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: grape-entity
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: grape-kaminari
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- acprieto41@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".rspec"
|
91
|
+
- ".rubocop.yml"
|
92
|
+
- CHANGELOG.md
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- grape-utils.gemspec
|
100
|
+
- lib/grape-utils.rb
|
101
|
+
- lib/grape_utils.rb
|
102
|
+
- lib/grape_utils/helpers.rb
|
103
|
+
- lib/grape_utils/templates/create.rb
|
104
|
+
- lib/grape_utils/templates/crud.rb
|
105
|
+
- lib/grape_utils/templates/delete.rb
|
106
|
+
- lib/grape_utils/templates/index.rb
|
107
|
+
- lib/grape_utils/templates/show.rb
|
108
|
+
- lib/grape_utils/templates/update.rb
|
109
|
+
- lib/grape_utils/version.rb
|
110
|
+
- sig/grape/utils.rbs
|
111
|
+
homepage: https://github.com/Nix41/grape-utils
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
metadata:
|
115
|
+
homepage_uri: https://github.com/Nix41/grape-utils
|
116
|
+
source_code_uri: https://github.com/Nix41/grape-utils
|
117
|
+
changelog_uri: https://github.com/Nix41/grape-utils/blob/main/CHANGELOG.md
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: 2.6.0
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubygems_version: 3.4.10
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: Grape Utils for building APIs
|
137
|
+
test_files: []
|