Resolucion del antiguo challenge de alkemy para la acceleracion de Ruby on Rails, el objetivo de este challenge es desarrollar una API para explorar el mundo Disney, la cual permitira conocer y modificar los personajes que lo componen y entender en que peliculas participaron. Por otro lado, debera exponer la informacion para que cualquier frontend pueda consumirla.
- Funcionalidades
- Modelado de la Base de Datos
- Instalación/Ejecucion
- Uso de la API
- Endpoints API
- Rspec Testing
- Tecnologias
- Documentacion Postman
- Autor
- Autenticación de usuarios mediante un Json Web Token
- Usuarios (Users): Endpoints Create, Read, Update, Delete
- Tokens (Tokens): Endpoints Create
- Generos (Genre): Endpoints Index, Create, Read, Update, Delete
- Peliculas (Movies): Endpoints Index, Create, Read, Update, Delete
- Busqueda por params querys: title, id_genre, order
- Personajes (Characters): Endpoints Index, Create, Read, Update, Delete
- Busqueda por params querys: name, age, id_movie
Asegúrate de tener instalado lo siguiente en tu sistema:
- Ruby (versión 3.1.2)
- Ruby on Rails (versión 7.0.4)
- PostgreSQL
Clonar el repositorio
git clone https://github.com/Mettralla/old_alkemy_challenge_disney_api.git
Ir al directorio del proyecto
cd old_alkemy_challenge_disney_api
Instalar dependencias
bundle install
Configuración de la base de datos
Antes de ejecutar la aplicación, asegúrate de configurar la conexión a tu base de datos PostgreSQL.
# config\database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: <%= ENV["USER"] %> # ASIGNAR ENV_VARIABLE O INGRESAR USER
password: <%= ENV["DATABASE_PASSWORD"] %> # ASIGNAR ENV_VARIABLE O INGRESAR PASSWORD
Crear base de datos
rails db:create
Realizar migraciones
rails db:migrate
De forma opcional puedes poblar la base de datos con algunos registros de prueba
rails db:seed
Iniciar server
rails server
El servidor estara corriendo en localhost:3000, sin embargo este proyecto no cuenta con un frontend.
Para tener libre acceso a los endpoints necesitaras crear un usuario y loguearte para generar un token de acceso.
Crear un usuario:
Para crear el usuario necesitas enviar un json con un email y un password mediante un POST request.
request: POST
http://localhost:3000/api/v1/users
body(raw): "Content-type:application/json"
{
"user": {
"email": "toot@gmail.com",
"password": "g00d_p4$$"
}
}
Esto devolvera un status 201 junto al registro creado.
Generar token:
Para generar el token necesitas enviar un json con el email y el password del usuario creado mediante un POST request.
request: POST
http://localhost:3000/api/v1/tokens
body(raw): "Content-type:application/json"
{
"user": {
"email": "toot@gmail.com",
"password": "g00d_p4$$"
}
}
Response: 200 OK
{
"token": "jwt_token",
"email": "toot@gmail.com"
}
Adjuntando el jwt_token en los headers de tus consultas seras capaz de operar con todos los endpoints de la API:
Authorization: <jwt_token>
http://localhost:3000/api/v1/users
{
"user": {
"email": "toot@gmail.com",
"password": "g00d_p4$$"
}
}
{
"id": 1,
"email": "toot@gmail.com",
"password_digest": "$2a$12$FNsLJtUde5HGHj2.yuE9TeTXP/PhhqtECB64f/cd.LWyJOfb1Z6f.",
"created_at": "2023-06-18T18:44:58.307Z",
"updated_at": "2023-06-18T18:44:58.307Z"
}
http://localhost:3000/api/v1/users/1
Content-Type | Value |
---|---|
Authorization | jwt_token |
{
"id": 1,
"email": "toot@gmail.com"
}
http://localhost:3000/api/v1/users/1
Content-Type | Value |
---|---|
Authorization | jwt_token |
{
"user": {
"email": "example@gmail.com"
}
}
{
"email": "example@gmail.com",
"id": 1,
"password_digest": "$2a$12$FNsLJtUde5HGHj2.yuE9TeTXP/PhhqtECB64f/cd.LWyJOfb1Z6f.",
"created_at": "2023-06-18T18:44:58.307Z",
"updated_at": "2023-06-18T21:30:31.845Z"
}
http://localhost:3000/api/v1/users/1
Content-Type | Value |
---|---|
Authorization | jwt_token |
null
http://localhost:3000/api/v1/tokens
{
"user": {
"email": "toot@gmail.com",
"password": "g00d_p4$$"
}
}
{
"token": "jwt_token",
"email": "toot@gmail.com"
}
http://localhost:3000/api/v1/genres
Content-Type | Value |
---|---|
Authorization | jwt_token |
[
{
"name": "Action",
"picture": "action.jpg"
},
{
"name": "Adventure",
"picture": "adventure.jpg"
},
{
"name": "Comedy",
"picture": "comedy.jpg"
},
{
"name": "Drama",
"picture": "drama.jpg"
},
{
"name": "Horror",
"picture": "horror.jpg"
},
{
"name": "Animation",
"picture": "animation.jpg"
},
{
"name": "Family",
"picture": "family.jpg"
}
]
http://localhost:3000/api/v1/genres/14
Content-Type | Value |
---|---|
Authorization | jwt_token |
{
"id": 15,
"name": "Family",
"picture": "family.jpg"
}
http://localhost:3000/api/v1/genres
Content-Type | Value |
---|---|
Authorization | jwt_token |
{
"genre": {
"picture": "documentary.jpg",
"name": "Documentary"
}
}
{
"id": 16,
"name": "Documentary",
"picture": "documentary.jpg"
}
http://localhost:3000/api/v1/genres/16
Content-Type | Value |
---|---|
Authorization | jwt_token |
{
"genre": {
"picture": "documental.jpg",
"name": "Documental"
}
}
{
"id": 16,
"name": "Documental",
"picture": "documental.jpg"
}
http://localhost:3000/api/v1/genres/16
Content-Type | Value |
---|---|
Authorization | jwt_token |
null
http://localhost:3000/api/v1/movies?title=lion
Content-Type | Value |
---|---|
Authorization | jwt_token |
Param | value |
---|---|
title | lion |
[
{
"picture": "the_lion_king.jpg",
"title": "The Lion King",
"release_date": "15/06/1994"
},
{
"picture": "the_lion_guard.jpg",
"title": "The Lion Guard",
"release_date": "15/01/2016"
}
]
http://localhost:3000/api/v1/movies?genre_id=10
Content-Type | Value |
---|---|
Authorization | jwt_token |
Param | value |
---|---|
genre_id | 10 |
[
{
"picture": "the_lion_king.jpg",
"title": "The Lion King",
"release_date": "15/06/1994"
},
{
"picture": "aladdin.jpg",
"title": "Aladdin",
"release_date": "25/11/1992"
},
{
"picture": "frozen.jpg",
"title": "Frozen",
"release_date": "27/11/2013"
},
{
"picture": "toy_story.jpg",
"title": "Toy Story",
"release_date": "22/11/1995"
}
]
http://localhost:3000/api/v1/movies?order=ASC
Content-Type | Value |
---|---|
Authorization | jwt_token |
Param | value |
---|---|
order | ASC |
[
{
"picture": "aladdin.jpg",
"title": "Aladdin",
"release_date": "25/11/1992"
},
{
"picture": "the_lion_king.jpg",
"title": "The Lion King",
"release_date": "15/06/1994"
},
{
"picture": "toy_story.jpg",
"title": "Toy Story",
"release_date": "22/11/1995"
},
{
"picture": "finding_nemo",
"title": "Finding Nemo",
"release_date": "30/05/2003"
},
{
"picture": "the_incredibles",
"title": "The Incredibles",
"release_date": "27/10/2004"
},
{
"picture": "frozen.jpg",
"title": "Frozen",
"release_date": "27/11/2013"
},
{
"picture": "the_lion_guard.jpg",
"title": "The Lion Guard",
"release_date": "15/01/2016"
},
{
"picture": "coco.jpg",
"title": "Coco",
"release_date": "20/10/2017"
}
]
http://localhost:3000/api/v1/movies?order=DESC
Content-Type | Value |
---|---|
Authorization | jwt_token |
Param | value |
---|---|
order | DESC |
[
{
"picture": "coco.jpg",
"title": "Coco",
"release_date": "20/10/2017"
},
{
"picture": "the_lion_guard.jpg",
"title": "The Lion Guard",
"release_date": "15/01/2016"
},
{
"picture": "frozen.jpg",
"title": "Frozen",
"release_date": "27/11/2013"
},
{
"picture": "the_incredibles",
"title": "The Incredibles",
"release_date": "27/10/2004"
},
{
"picture": "finding_nemo",
"title": "Finding Nemo",
"release_date": "30/05/2003"
},
{
"picture": "toy_story.jpg",
"title": "Toy Story",
"release_date": "22/11/1995"
},
{
"picture": "the_lion_king.jpg",
"title": "The Lion King",
"release_date": "15/06/1994"
},
{
"picture": "aladdin.jpg",
"title": "Aladdin",
"release_date": "25/11/1992"
}
]
http://localhost:3000/api/v1/movies
Content-Type | Value |
---|---|
Authorization | jwt_token |
[
{
"picture": "the_lion_king.jpg",
"title": "The Lion King",
"release_date": "15/06/1994"
},
{
"picture": "aladdin.jpg",
"title": "Aladdin",
"release_date": "25/11/1992"
},
{
"picture": "frozen.jpg",
"title": "Frozen",
"release_date": "27/11/2013"
},
{
"picture": "toy_story.jpg",
"title": "Toy Story",
"release_date": "22/11/1995"
},
{
"picture": "nemo.jpg",
"title": "Finding Nemo",
"release_date": "20/05/2003"
},
{
"picture": "coco.jpg",
"title": "Coco",
"release_date": "20/10/2017"
}
]
http://localhost:3000/api/v1/movies/6
Content-Type | Value |
---|---|
Authorization | jwt_token |
{
"id": 3,
"picture": "the_lion_king.jpg",
"title": "The Lion King",
"release_date": "15/06/1994",
"raiting": 8,
"genre_id": "Adventure",
"cast": [
{
"id": 2,
"picture": "simba.jpg",
"name": "simba",
"age": 2,
"weight": 50,
"story": "Simba is the main protagonist of The Lion King",
"created_at": "2023-06-15T20:11:59.849Z",
"updated_at": "2023-06-15T20:11:59.849Z",
"movie_id": 3
},
{
"id": 4,
"picture": "pumba.jpg",
"name": "pumba",
"age": 20,
"weight": 150,
"story": "Pumba is lovable warthog and best friend of Timon in The Lion King",
"created_at": "2023-06-15T20:52:45.014Z",
"updated_at": "2023-06-15T20:52:45.014Z",
"movie_id": 3
}
]
}
http://localhost:3000/api/v1/movies
Content-Type | Value |
---|---|
Authorization | jwt_token |
{
"movie": {
"picture": "beauty_beast.jpg",
"title": "Beauty and the Beast",
"release_date": "12/11/1991",
"raiting": 8.0,
"genre_id": 14
}
}
{
"id": 10,
"picture": "finding_nemo",
"title": "Finding Nemo",
"release_date": "30/05/2003",
"raiting": 8,
"genre_id": "Family",
"cast": []
}
http://localhost:3000/api/v1/movies/11
Content-Type | Value |
---|---|
Authorization | jwt_token |
{
"movie": {
"picture": "moana.jpg"
}
}
{
"id": 11,
"picture": "moana.jpg",
"title": "Moana",
"release_date": "23/11/2016",
"raiting": 7,
"genre_id": "Family",
"cast": []
}
http://localhost:3000/api/v1/movies/11
Content-Type | Value |
---|---|
Authorization | jwt_token |
null
http://localhost:3000/api/v1/characters?name=mouse
Content-Type | Value |
---|---|
Authorization | jwt_token |
Param | value |
---|---|
name | mouse |
[
{
"picture": "mickey.jpg",
"name": "Mickey Mouse"
},
{
"picture": "minnie.jpg",
"name": "Minnie Mouse"
}
]
http://localhost:3000/api/v1/characters?age=25
Content-Type | Value |
---|---|
Authorization | jwt_token |
Param | value |
---|---|
age | 25 |
[
{
"picture": "beast.jpg",
"name": "Beast"
}
]
http://localhost:3000/api/v1/characters?movie_id=3
Content-Type | Value |
---|---|
Authorization | jwt_token |
Param | value |
---|---|
movie_id | 3 |
[
{
"picture": "simba.jpg",
"name": "simba"
},
{
"picture": "pumba.jpg",
"name": "pumba"
}
]
http://localhost:3000/api/v1/characters
Content-Type | Value |
---|---|
Authorization | jwt_token |
[
{
"picture": "simba.jpg",
"name": "simba"
},
{
"picture": "pumba.jpg",
"name": "pumba"
},
{
"picture": "aladdin_ch.jpg",
"name": "Aladdin - Prince Ali"
}
]
http://localhost:3000/api/v1/characters/2
Content-Type | Value |
---|---|
Authorization | jwt_token |
{
"id": 2,
"picture": "simba.jpg",
"name": "simba",
"age": 2,
"weight": 50,
"story": "Simba is the main protagonist of The Lion King",
"movies": "The Lion King"
}
http://localhost:3000/api/v1/characters
Content-Type | Value |
---|---|
Authorization | jwt_token |
{
"character": {
"picture": "minnie.jpg",
"name": "Minnie Mouse",
"age": 90,
"weight": 110.0,
"story": "Minnie is Mickey's loyal and loving girlfriend who often joins him on his adventures",
"movie_id": 13
}
}
{
"id": 6,
"picture": "dory.jpg",
"name": "Dory",
"age": 30,
"weight": 150,
"story": "Dory is a friendly and forgetful blue tang fish in Finding Nemo",
"movies": "Finding Nemo"
}
http://localhost:3000/api/v1/characters/3
Content-Type | Value |
---|---|
Authorization | jwt_token |
{
"character": {
"name": "Aladdin",
"picture": "aladdin.jpg"
}
}
{
"id": 3,
"picture": "aladdin.jpg",
"name": "Aladdin",
"age": 18,
"weight": 70,
"story": "Aladdin is a street urchin who becomes a prince with the help of a magical genie",
"movies": "Aladdin"
}
http://localhost:3000/api/v1/characters/7
Content-Type | Value |
---|---|
Authorization | jwt_token |
null
Para ejecutar todos los tests y ver los resultados en formato de documentación con colores, sigue estos pasos:
- Asegúrate de tener todas las dependencias y hacer las migraciones necesarias en caso de no haberlo hecho aun.
rails db:migrate RAILS_ENV=test
-
Abre una terminal y navega hasta el directorio raíz del proyecto.
-
Ejecuta el siguiente comando para ejecutar todos los tests y ver los resultados en formato de documentación con colores:
bundle exec rspec --format documentation --color
Esto ejecutará todos los tests del proyecto y mostrará los resultados detallados en la consola. Los tests están agrupados por categorías y cada uno muestra una descripción clara de lo que se está probando.
- ruby "3.1.2"
- rails "7.0.4"
- pg "1.1"
- bcrypt "3.1.7"
- rack-cors
- factory_bot_rails
- rspec-rails "6.0.0"
- jwt "2.7"
- Documentacion de la API hecha con Postman:
- Conversion de Postman to Markdown: