Skip to content

erivandosena/api-ruby

Repository files navigation

Bot logo

API Ruby on Rails

Status License


🤖 RESTful API with Ruby on Rails

Endpoint API: http://localhost:8002/api/v1 Endpoint DOC: http://localhost:8002/api/v1/api-docs/

📝 Table of Contents

🧐 About

A RESTful API ultimately lives on the Web, or on a communication network where different parties can expose and consume data streams.

🎥 Application Sample / Working

Working

💭 How it works

The entire API is written in Ruby 3.1.1

🎈 Usage

Rotas

http: //domain/api/v1

Listar todos recursos

Retorna uma lista de todos os recursos cadastrados.
GET /api/v1/pages

Obter um recurso específico

Retorna os detalhes de um recurso específico com base no ID fornecido.
GET /api/v1/pages/:id Parâmetros

  • :id (obrigatório): O ID do recurso desejado.

Criar um novo recurso

Cria um novo recurso com os dados fornecidos. POST /api/v1/pages Parâmetros da Requisição

  • page (obrigatório): Um objeto JSON contendo os detalhes do recurso a ser criado.
    Os campos obrigatórios são title, description e active.
    Exemplo de requisição
{
  "page": {
    "title": "Novo Artigo",
    "description": "Texto descritivo do novo artigo.",
    "active": true
  }
}

Atualizar uma página existente

Atualiza os detalhes de um recurso existente com base no ID fornecido.
PATCH/PUT /api/v1/pages/:id Parâmetros

  • :id (obrigatório): O ID do recurso a ser atualizado.
    Parâmetros da Requisição
  • page (obrigatório): Um objeto JSON contendo os detalhes atualizados do recurso. Os campos permitidos para atualização são title, description e active.
    Exemplo de requisição
{
  "page": {
    "title": "Atualização do artigo",
    "description": "Texto descritivo com atualização desativado.",
    "active": false
  }
}

Excluir uma página

Exclui um recurso existente com base no ID fornecido.
DELETE /api/v1/pages/:id Parâmetros

  • :id (obrigatório): O ID do recurso a ser excluído.

Erros

A API retorna os respectivos textos de erros, mensagens e validações:
Exemplo de respostas:

{
  "error": "Recurso não encontrado"
}
{
  "error": "404 Not Found: Endpoint inexistente para o recurso."
}
{
  "message": "Bem-vindo à API RESTful Ruby on Rails"
}
{
    "title": [
        "não pode ficar em branco"
    ],
    "description": [
        "não pode ficar em branco"
    ],
    "active": [
        "não está incluído na lista"
    ]
}

Install Docker

curl https://releases.rancher.com/install-docker/20.10.sh | sh

To Install Docker in WSL/2

echo 'sudo gpasswd -a $USER docker' >> ~/.bashrc
echo 'sudo chmod 666 /var/run/docker.sock' >> ~/.bashrc
echo 'sudo /etc/init.d/docker start' >> ~/.bashrc
source ~/.bashrc

Installing

A step by step series of examples that tell you how to get a development env running.

echo "# api-ruby" >> README.md
git init
git add README.md
git commit -m "Next commit"
git branch -M main
git remote add origin https://github.com/erivandosena/api-ruby.git
git push -u origin main

Send to repository

git remote add origin https://github.com/erivandosena/api-ruby.git
git branch -M main
git push -u origin main

Clean Docker Workspace local

docker stop $(docker ps -qa)
docker rm -f $(docker ps -qa)
docker rmi -f $(docker images -qa)
docker volume rm -f $(docker volume ls -q)
docker network rm $(docker network ls -q)
docker system prune -fa

Installing Docs

Gemfile
gem 'grape-swagger-rails'
gem 'rswag-api'
gem 'rswag-ui'

bundle install

Install generators/customizing

rails g rswag:api:install  
rails g rswag:ui:install  
rails g rswag:ui:custom  

User

rails g controller User email:string password_digest:string

rails g model User email:string password_digest:string

rails g model ApiKey bearer_id:integer bearer_type:string token:string

Função HMAC para chave secreta

API_KEY_HMAC_SECRET_KEY=$(bundle exec bin/rails runner "puts SecureRandom.hex(32)")

Add user

docker exec -it api-ruby bin/rails runner 'User.create!(email: "[email protected]", password: "1234")'

Create Token API

curl -v -X POST http://localhost:8002/api/v1/api-keys -u [email protected]:1234

Exemplo de resposta:

{
	"api_key": "1febeb9cd6babc872e81eefe349062a2"
}

Modelos de solicitações para API REST

curl -v -X GET http://localhost:8002/api/v1/api-keys -H 'Authorization: Bearer 1febeb9cd6babc872e81eefe349062a2'

curl -v -X GET http://localhost:8002/api/v1/users -H 'Authorization: Bearer 1febeb9cd6babc872e81eefe349062a2'

curl -v -X GET http://localhost:8002/api/v1/pages -H 'Authorization: Bearer 1febeb9cd6babc872e81eefe349062a2'

curl -v -X DELETE http://localhost:8002/api/v1/api-keys/1 -H 'Authorization: Bearer 1febeb9cd6babc872e81eefe349062a2'

Example:

!dict what is love

Definition:

Baby, dont hurt me~ Dont hurt me~ no more.

Example:

Dude1: Bruh, what is love? Dude2: Baby, dont hurt me, dont hurt me- no more! Dude1: dafuq?

Source: https://github.com/erivandosena/api-ruby


Beep boop. I am a API. If there are any issues, contact my Main

Want to make a similar reddit API? Check out: GitHub

🏁 Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

Docker
Linux Debian
Ruby on Rails
Nginx Server
Passenger
OAuth2
Devise
Doorkeeper
Rswag
Swagger
PosgreSQL
Redis

Rails version: 7.0.6
Ruby version: ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [x86_64-linux]

Installing

Build image:
docker build -f ./Dockerfile -t erivando/api-ruby:latest --build-arg VERSION=1.0.0 --build-arg COMMIT_SHA=$(git rev-parse HEAD) --no-cache .
docker tag erivando/api-ruby:latest erivando/api-ruby:v1.0.0
docker login
docker push erivando/api-ruby:v1.0.0
Compose V2 (para Compose V1 use: docker-compose ...):
docker compose build --no-cache && docker compose up -d
docker compose exec api-ruby bash
docker compose logs -f -t api-ruby
docker compose down

🚀 Deploying your API

Gerar modelo Page com os atributos title (string), description (text), active (boolean).
Também irá gerar automaticamente o controlador, as rotas e os testes correspondentes para API.

rails new api_crud -v 3.1.1 --api --skip-bundle --version=7.0.6 -d postgresql
rails g scaffold Page title:string description:text active:boolean

⛏️ Built Using

  • PRAW - Python Reddit API Wrapper

✍️ Authors

🎉 Acknowledgements

  • Hat tip to anyone whose code was used
  • Inspiration
  • References