mortymer 0.0.2
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Guardfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +167 -0
- data/Rakefile +12 -0
- data/config.ru +5 -0
- data/docs/.vitepress/config.mts +73 -0
- data/docs/.vitepress/theme/Layout.vue +21 -0
- data/docs/.vitepress/theme/index.ts +5 -0
- data/docs/.vitepress/theme/style.css +5 -0
- data/docs/advanced/dependency-injection.md +21 -0
- data/docs/advanced/openapi.md +16 -0
- data/docs/guide/api-metadata.md +142 -0
- data/docs/guide/introduction.md +31 -0
- data/docs/guide/models.md +16 -0
- data/docs/guide/quick-start.md +190 -0
- data/docs/index.md +26 -0
- data/lib/mortymer/api_metadata.rb +79 -0
- data/lib/mortymer/container.rb +122 -0
- data/lib/mortymer/dependencies_dsl.rb +65 -0
- data/lib/mortymer/dry_swagger.rb +10 -0
- data/lib/mortymer/endpoint.rb +139 -0
- data/lib/mortymer/endpoint_registry.rb +16 -0
- data/lib/mortymer/model.rb +8 -0
- data/lib/mortymer/openapi_generator.rb +76 -0
- data/lib/mortymer/rails/configuration.rb +16 -0
- data/lib/mortymer/rails/endpoint_wrapper_controller.rb +44 -0
- data/lib/mortymer/rails/routes.rb +62 -0
- data/lib/mortymer/rails.rb +19 -0
- data/lib/mortymer/railtie.rb +14 -0
- data/lib/mortymer/utils/string_transformations.rb +28 -0
- data/lib/mortymer/version.rb +5 -0
- data/lib/mortymer.rb +15 -0
- data/lib/mortymermer.rb +15 -0
- data/mortymer.gemspec +45 -0
- data/package-lock.json +2429 -0
- data/package.json +13 -0
- metadata +172 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ecb9a7f658430de14fe07898f71288a63923707680c8a032de19ba6ede91c4a6
|
4
|
+
data.tar.gz: 034032b9168e5225d1c2a2fca5049c4436637aa8748e7e43376d0637fc4b3891
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6bae0bfe67e11f94216b6f04794c38bba579804780eeab91044e279d2da2a7beea076c2b42c7e644d639f0a2fb33b6ee5e64d84695301a6c74b6167980e8b5df
|
7
|
+
data.tar.gz: e34d25b038d008e2ec61f6bb949811d7f18b5d6d65bc379d45a9256e3436e17534faeaabddb2af97ea4f05ee533e56896d1818f06ac05a9bba370e0e6fedd10e
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
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 adriangonzalezsanchez1996@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/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Adrian Gonzalez
|
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,167 @@
|
|
1
|
+
# Morty
|
2
|
+
|
3
|
+
```markdown
|
4
|
+
███╗ ███╗ ██████╗ ██████╗ ████████╗██╗ ██╗
|
5
|
+
████╗ ████║██╔═══██╗██╔══██╗╚══██╔══╝╚██╗ ██╔╝
|
6
|
+
██╔████╔██║██║ ██║██████╔╝ ██║ ╚████╔╝
|
7
|
+
██║╚██╔╝██║██║ ██║██╔══██╗ ██║ ╚██╔╝
|
8
|
+
██║ ╚═╝ ██║╚██████╔╝██║ ██║ ██║ ██║
|
9
|
+
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝
|
10
|
+
```
|
11
|
+
|
12
|
+
Morty is a Ruby gem that simplifies API endpoint management and documentation for Ruby on Rails applications. It provides a clean DSL for defining API endpoints with input/output contracts and automatically generates OpenAPI documentation.
|
13
|
+
|
14
|
+
[](https://badge.fury.io/rb/morty)
|
15
|
+
[](https://opensource.org/licenses/MIT)
|
16
|
+
|
17
|
+
## Features
|
18
|
+
|
19
|
+
- 🚀 Simple DSL for defining API endpoints
|
20
|
+
- 📝 Automatic OpenAPI documentation generation
|
21
|
+
- ✨ Input/Output contract validation
|
22
|
+
- 🎯 Dependency injection support
|
23
|
+
- 🛣️ Seamless Rails integration
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'morty'
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
```bash
|
36
|
+
bundle install
|
37
|
+
```
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
|
41
|
+
### Basic Example
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class UserProfileEndpoint < ApplicationController
|
45
|
+
include Morty::ApiMetadata
|
46
|
+
|
47
|
+
# Define an endpoint with input/output contracts
|
48
|
+
get input: UserProfileInput, output: UserProfileOutput, path: '/api/v1/users/:id/profile'
|
49
|
+
def call(input)
|
50
|
+
user = User.find(input.id)
|
51
|
+
UserProfileOutput.new(
|
52
|
+
id: user.id,
|
53
|
+
name: user.name,
|
54
|
+
email: user.email
|
55
|
+
)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
### Input/Output Contracts
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class UserProfileInput < Morty::Model
|
64
|
+
attribute :id, Types::Integer
|
65
|
+
attribute :include_details, Types::Bool.optional.default(false)
|
66
|
+
end
|
67
|
+
|
68
|
+
class UserProfileOutput < Morty::Model
|
69
|
+
attribute :id, Types::Integer
|
70
|
+
attribute :name, Types::String
|
71
|
+
attribute :email, Types::String
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
### Dependency Injection
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
class UserService
|
79
|
+
include Morty::DependenciesDsl
|
80
|
+
|
81
|
+
# Inject dependencies
|
82
|
+
inject UserRepository
|
83
|
+
inject EmailService, as: :mailer
|
84
|
+
|
85
|
+
def process_user(id)
|
86
|
+
user = @user_repository.find_user(id)
|
87
|
+
@email_service.send_email(user.email, "Welcome!")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
### Rails Integration
|
93
|
+
|
94
|
+
In your `config/routes.rb`:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
Rails.application.routes.draw do
|
98
|
+
Morty::Rails::Routes.new(self).mount_controllers
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
## OpenAPI Documentation
|
103
|
+
|
104
|
+
Morty automatically generates OpenAPI documentation for your API endpoints. The documentation includes:
|
105
|
+
|
106
|
+
- Endpoint paths and HTTP methods
|
107
|
+
- Request/response schemas
|
108
|
+
- Input validation rules
|
109
|
+
- Error responses
|
110
|
+
|
111
|
+
To generate the OpenAPI documentation:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
generator = Morty::OpenapiGenerator.new(
|
115
|
+
prefix: "/api",
|
116
|
+
title: "My API",
|
117
|
+
version: "v1"
|
118
|
+
)
|
119
|
+
generator.generate
|
120
|
+
```
|
121
|
+
|
122
|
+
## Contributing
|
123
|
+
|
124
|
+
We love your input! We want to make contributing to Morty as easy and transparent as possible, whether it's:
|
125
|
+
|
126
|
+
- Reporting a bug
|
127
|
+
- Discussing the current state of the code
|
128
|
+
- Submitting a fix
|
129
|
+
- Proposing new features
|
130
|
+
- Becoming a maintainer
|
131
|
+
|
132
|
+
### Development Process
|
133
|
+
|
134
|
+
1. Fork the repo and create your branch from `master`
|
135
|
+
2. If you've added code, please, add some tests to contribute to gem health
|
136
|
+
3. If you've changed APIs, update the documentation accordingly
|
137
|
+
4. Ensure the test suite passes
|
138
|
+
5. Make sure your code lints
|
139
|
+
6. Issue that pull request!
|
140
|
+
|
141
|
+
### Running Tests
|
142
|
+
|
143
|
+
```bash
|
144
|
+
bundle install
|
145
|
+
bundle exec rspec
|
146
|
+
```
|
147
|
+
|
148
|
+
## License
|
149
|
+
|
150
|
+
MIT License. See [LICENSE](LICENSE) for details.
|
151
|
+
|
152
|
+
## Code of Conduct
|
153
|
+
|
154
|
+
This project follows the [Contributor Covenant](https://www.contributor-covenant.org/version/2/0/code_of_conduct/) Code of Conduct.
|
155
|
+
|
156
|
+
## Support
|
157
|
+
|
158
|
+
If you have any questions or need help with Morty:
|
159
|
+
|
160
|
+
- Open an [issue](https://github.com/yourusername/morty/issues)
|
161
|
+
- Join our [Discord community](#) (coming soon)
|
162
|
+
|
163
|
+
## Credits
|
164
|
+
|
165
|
+
Morty is maintained by [Adrian Gonzalez] and was inspired by the need for a simple,
|
166
|
+
yet powerful API management solution in the Ruby ecosystem, that integrates well
|
167
|
+
with existing frameworks. We deserve a FastAPI experience within the ruby side.
|
data/Rakefile
ADDED
data/config.ru
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
import { defineConfig } from "vitepress";
|
2
|
+
|
3
|
+
// https://vitepress.dev/reference/site-config
|
4
|
+
export default defineConfig({
|
5
|
+
title: "Morty",
|
6
|
+
description:
|
7
|
+
"Standalone API definition for ruby frameworks based on dry.rb. Rails compatible from day 0 with a full Ruby flavored dependency injection engine.",
|
8
|
+
markdown: {
|
9
|
+
theme: "catppuccin-mocha",
|
10
|
+
},
|
11
|
+
themeConfig: {
|
12
|
+
nav: [
|
13
|
+
{ text: "Guide", link: "/guide/introduction" },
|
14
|
+
{ text: "API", link: "/api/" },
|
15
|
+
{
|
16
|
+
text: "GitHub",
|
17
|
+
link: "https://github.com/adriangs1996/morty",
|
18
|
+
},
|
19
|
+
],
|
20
|
+
|
21
|
+
sidebar: {
|
22
|
+
"/guide/": [
|
23
|
+
{
|
24
|
+
text: "Getting Started",
|
25
|
+
items: [
|
26
|
+
{ text: "Introduction", link: "/guide/introduction" },
|
27
|
+
{ text: "Quick Start", link: "/guide/quick-start" },
|
28
|
+
{ text: "Installation", link: "/guide/installation" },
|
29
|
+
],
|
30
|
+
},
|
31
|
+
{
|
32
|
+
text: "Core Concepts",
|
33
|
+
items: [
|
34
|
+
{ text: "API Metadata", link: "/guide/api-metadata" },
|
35
|
+
{ text: "Type System", link: "/guide/type-system" },
|
36
|
+
{
|
37
|
+
text: "Dependency Injection",
|
38
|
+
link: "/guide/dependency-injection",
|
39
|
+
},
|
40
|
+
{ text: "Error Handling", link: "/guide/error-handling" },
|
41
|
+
],
|
42
|
+
},
|
43
|
+
{
|
44
|
+
text: "Rails Integration",
|
45
|
+
items: [
|
46
|
+
{ text: "Setup", link: "/guide/rails-setup" },
|
47
|
+
{ text: "Routing", link: "/guide/routing" },
|
48
|
+
{ text: "Controllers", link: "/guide/controllers" },
|
49
|
+
{ text: "Testing", link: "/guide/testing" },
|
50
|
+
],
|
51
|
+
},
|
52
|
+
{
|
53
|
+
text: "Advanced",
|
54
|
+
items: [
|
55
|
+
{ text: "OpenAPI Generation", link: "/guide/openapi" },
|
56
|
+
{ text: "Custom Types", link: "/guide/custom-types" },
|
57
|
+
{ text: "Middleware", link: "/guide/middleware" },
|
58
|
+
{ text: "Configuration", link: "/guide/configuration" },
|
59
|
+
],
|
60
|
+
},
|
61
|
+
],
|
62
|
+
},
|
63
|
+
|
64
|
+
socialLinks: [
|
65
|
+
{ icon: "github", link: "https://github.com/yourusername/morty" },
|
66
|
+
],
|
67
|
+
|
68
|
+
footer: {
|
69
|
+
message: "Released under the MIT License.",
|
70
|
+
copyright: "Copyright © 2024-present",
|
71
|
+
},
|
72
|
+
},
|
73
|
+
});
|
@@ -0,0 +1,21 @@
|
|
1
|
+
<script setup lang="ts">
|
2
|
+
import { useData } from 'vitepress'
|
3
|
+
|
4
|
+
// https://vitepress.dev/reference/runtime-api#usedata
|
5
|
+
const { site, frontmatter } = useData()
|
6
|
+
</script>
|
7
|
+
|
8
|
+
<template>
|
9
|
+
<div v-if="frontmatter.home">
|
10
|
+
<h1>{{ site.title }}</h1>
|
11
|
+
<p>{{ site.description }}</p>
|
12
|
+
<ul>
|
13
|
+
<li><a href="/markdown-examples.html">Markdown Examples</a></li>
|
14
|
+
<li><a href="/api-examples.html">API Examples</a></li>
|
15
|
+
</ul>
|
16
|
+
</div>
|
17
|
+
<div v-else>
|
18
|
+
<a href="/">Home</a>
|
19
|
+
<Content />
|
20
|
+
</div>
|
21
|
+
</template>
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Dependency Injection
|
2
|
+
|
3
|
+
Morty provides a powerful dependency injection system
|
4
|
+
that helps you write clean, testable code.
|
5
|
+
|
6
|
+
## Basic Usage
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
class UserService
|
10
|
+
include Morty::DependenciesDsl
|
11
|
+
|
12
|
+
inject EmailService
|
13
|
+
inject UserRepository
|
14
|
+
|
15
|
+
def process_user(id)
|
16
|
+
user = user_repository.find(id)
|
17
|
+
email_service.send_welcome_email(user)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
```
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# OpenAPI Documentation
|
2
|
+
|
3
|
+
Morty automatically generates OpenAPI (Swagger)
|
4
|
+
documentation from your endpoints and models.
|
5
|
+
|
6
|
+
## Basic Configuration
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
# config/initializers/morty.rb
|
10
|
+
Rails.application.config.morty.configure do |config|
|
11
|
+
config.openapi.title = "My API"
|
12
|
+
config.openapi.version = "v1"
|
13
|
+
config.openapi.description = "API documentation for My Application"
|
14
|
+
end
|
15
|
+
|
16
|
+
```
|
@@ -0,0 +1,142 @@
|
|
1
|
+
# API Metadata
|
2
|
+
|
3
|
+
The `ApiMetadata` module is a core component of Morty that allows you to define and configure API endpoints in your Ruby classes. It provides a clean DSL for declaring HTTP endpoints with their input/output types and automatically handles endpoint registration and Rails integration.
|
4
|
+
|
5
|
+
## Basic Usage
|
6
|
+
|
7
|
+
Include the `ApiMetadata` module in your endpoint classes to use the DSL:
|
8
|
+
|
9
|
+
````ruby
|
10
|
+
class UserEndpoint
|
11
|
+
include Morty::ApiMetadata
|
12
|
+
|
13
|
+
# Define a GET endpoint
|
14
|
+
get input: UserSearchInput,
|
15
|
+
output: UserSearchOutput
|
16
|
+
|
17
|
+
def call(input)
|
18
|
+
# Your endpoint logic here
|
19
|
+
UserSearchOutput.new(...)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
# API Metadata
|
23
|
+
|
24
|
+
The `ApiMetadata` module is a core component of Morty that allows you to define and configure API endpoints in your Ruby classes. It provides a clean DSL for declaring HTTP endpoints with their input/output types and automatically handles endpoint registration and Rails integration.
|
25
|
+
|
26
|
+
## Basic Usage
|
27
|
+
|
28
|
+
Include the `ApiMetadata` module in your endpoint classes to use the DSL:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class UserEndpoint
|
32
|
+
include Morty::ApiMetadata
|
33
|
+
|
34
|
+
# Define a GET endpoint
|
35
|
+
get input: UserSearchInput,
|
36
|
+
output: UserSearchOutput
|
37
|
+
|
38
|
+
def call(input)
|
39
|
+
# Your endpoint logic here
|
40
|
+
UserSearchOutput.new(...)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
````
|
44
|
+
|
45
|
+
## HTTP Method DSL
|
46
|
+
|
47
|
+
The module provides methods for all common HTTP verbs:
|
48
|
+
|
49
|
+
- `get(input:, output:, path: nil)`
|
50
|
+
- `post(input:, output:, path: nil)`
|
51
|
+
- `put(input:, output:, path: nil)`
|
52
|
+
- `delete(input:, output:, path: nil)`
|
53
|
+
|
54
|
+
Each method accepts:
|
55
|
+
|
56
|
+
- `input`: The class that defines the input schema/parameters
|
57
|
+
- `output`: The class that defines the response schema
|
58
|
+
- `path`: Optional custom path override (defaults to convention-based routing)
|
59
|
+
|
60
|
+
## Method Names
|
61
|
+
|
62
|
+
By default, Morty looks for either a `call` or `execute` method as the endpoint handler. You can use other method names, but the endpoint name will include the method name in its registration:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class UserEndpoint
|
66
|
+
include Morty::ApiMetadata
|
67
|
+
|
68
|
+
# Registers as "UserEndpoint"
|
69
|
+
get input: SearchInput, output: SearchOutput
|
70
|
+
def call(input)
|
71
|
+
# ...
|
72
|
+
end
|
73
|
+
|
74
|
+
# Registers as "UserEndpoint#search"
|
75
|
+
get input: SearchInput, output: SearchOutput
|
76
|
+
def search(input)
|
77
|
+
# ...
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
## Rails Integration
|
83
|
+
|
84
|
+
When used in a Rails application with `config.morty.wrap_methods` enabled, the module automatically:
|
85
|
+
|
86
|
+
1. Wraps endpoint methods to handle parameter conversion
|
87
|
+
2. Transforms Rails params into your input class
|
88
|
+
3. Renders JSON responses with proper status codes
|
89
|
+
|
90
|
+
This means your endpoint methods can focus purely on business logic while Morty handles the HTTP/Rails integration.
|
91
|
+
|
92
|
+
## OpenAPI Generation
|
93
|
+
|
94
|
+
Endpoints defined with ApiMetadata automatically participate in OpenAPI documentation generation. The input and output classes are used to generate request/response schemas, and the HTTP method and path information is used to generate path documentation.
|
95
|
+
|
96
|
+
See the [OpenAPI Documentation](../advanced/openapi.md) section for more details on how Morty generates API documentation.
|
97
|
+
|
98
|
+
## Best Practices
|
99
|
+
|
100
|
+
1. Keep endpoint classes focused on a single resource or concern
|
101
|
+
2. Use meaningful input/output class names that reflect their purpose
|
102
|
+
3. Consider using namespaces to organize related endpoints
|
103
|
+
4. Let Morty handle the Rails integration rather than mixing Rails-specific code in your endpoints
|
104
|
+
|
105
|
+
## Example
|
106
|
+
|
107
|
+
Here's a complete example showing various features:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
module API
|
111
|
+
module V1
|
112
|
+
class UsersEndpoint
|
113
|
+
include Morty::ApiMetadata
|
114
|
+
|
115
|
+
# GET /api/v1/users
|
116
|
+
get input: Users::ListInput, output: Users::ListOutput
|
117
|
+
def list(input)
|
118
|
+
users = User.limit(input.limit).offset(input.offset)
|
119
|
+
Users::ListOutput.new(users: users)
|
120
|
+
end
|
121
|
+
|
122
|
+
# POST /api/v1/users
|
123
|
+
post input: Users::CreateInput, output: Users::CreateOutput
|
124
|
+
def create(input)
|
125
|
+
user = User.create!(input.to_h)
|
126
|
+
Users::CreateOutput.new(user: user)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Custom path example
|
130
|
+
# PUT /api/v1/users/:id/activate
|
131
|
+
put input: Users::ActivateInput,
|
132
|
+
output: Users::ActivateOutput,
|
133
|
+
path: '/api/v1/users/:id/activate'
|
134
|
+
def activate(input)
|
135
|
+
user = User.find(input.id)
|
136
|
+
user.activate!
|
137
|
+
Users::ActivateOutput.new(user: user)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
```
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Introduction
|
2
|
+
|
3
|
+
Morty is a modern Ruby gem designed to enhance API development with features
|
4
|
+
present in most modern API-first frameworks, like FastAPI and bringing some goodies
|
5
|
+
to the ruby ecosystem. It brings type safety, automatic documentation,
|
6
|
+
and dependency injection to your APIs, while beign web framework agnostic and
|
7
|
+
mantaining a good balance between explicitness and development experience.
|
8
|
+
It provides a seamless integration with Ruby on Rails, but it could be used along side
|
9
|
+
other frameworks like Sinatra or Grape.
|
10
|
+
|
11
|
+
## Features
|
12
|
+
|
13
|
+
- **Type-Safe APIs**: Define input and output types for your endpoints
|
14
|
+
- **Automatic Documentation**: OpenAPI/Swagger documentation generation from your defined input and outputs.
|
15
|
+
- **DI**: Dependency injection and clear separation of concerns through a simple DI Engine.
|
16
|
+
- **Rails Integration**: Seamless integration with Ruby on Rails
|
17
|
+
|
18
|
+
## Why Morty?
|
19
|
+
|
20
|
+
Morty brings the best practices of modern API development to the Ruby ecosystem. Topics
|
21
|
+
like API documentation is really painful, at least from my experience, in the Ruby world. There
|
22
|
+
are a lot of alternatives, from general ones like Rswag to Grape's approach, but none of them
|
23
|
+
feels intuitive or comfortable to use for me. In Rails' I personally hate the strong parameters
|
24
|
+
features, I strongly believe that if you are able to define the shape of your enpoint, i.e, your contract,
|
25
|
+
you should be able to use a fully Parsed object from your params, much like FastAPI's approach.
|
26
|
+
Im tire of beign jelous of FastAPI and want to use Ruby in a similar way, that's when Morty was born.
|
27
|
+
|
28
|
+
The name Morty comes from the popular series Rick and Morty, where Morty is like an assistant, a
|
29
|
+
faithful companion. And that is what this gem aims to be, not a replacement for any existent framework,
|
30
|
+
but a companion that allows the real heroes (Rails, Sinatra, Grape, etc) shine with their cool features,
|
31
|
+
and not get bored with the API handling stuff.
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Models
|
2
|
+
|
3
|
+
Morty models provide a type-safe way to define your API's data structures.
|
4
|
+
They're used for both input validation and output serialization.
|
5
|
+
|
6
|
+
## Basic Model
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
class UserModel < Morty::Model
|
10
|
+
attribute :id, Types::Integer
|
11
|
+
attribute :name, Types::String
|
12
|
+
attribute :email, Types::String
|
13
|
+
attribute :age, Types::Integer.optional
|
14
|
+
end
|
15
|
+
|
16
|
+
```
|