https://github.com/jonathan-foucher/ruby-api-example
An example of Ruby API with Sinatra
https://github.com/jonathan-foucher/ruby-api-example
ruby sinatra
Last synced: 3 months ago
JSON representation
An example of Ruby API with Sinatra
- Host: GitHub
- URL: https://github.com/jonathan-foucher/ruby-api-example
- Owner: jonathan-foucher
- Created: 2024-11-11T13:50:19.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-11-17T13:19:02.000Z (over 1 year ago)
- Last Synced: 2025-03-21T06:15:05.425Z (over 1 year ago)
- Topics: ruby, sinatra
- Language: Ruby
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Introduction
This project is an example of a Ruby API using Sinatra and a postgres database.
## Run the project
### Database
Install postgres locally or run it through docker with :
```
docker run -p 5432:5432 -e POSTGRES_DB=my_database -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres postgres
```
You will need to run the this request to create the movie table :
```
create table movie (
id integer primary key,
title varchar(50) not null,
release_date date not null
);
```
### Install the Gems
You can use Bundler to install the required Gems
```
bundle install
```
### Application
Once the postgres database is started, the movie table created and the Gems installed, you can start the Ruby project and try it out.
Start the application
```
ruby main.rb -p 3000
```
Get all the movies
```
curl --location 'http://localhost:3000/api/movies'
```
Save a movie
```
curl --request POST \
--url http://localhost:3000/api/movies \
--header 'Content-Type: application/json' \
--data '{
"id": 26,
"title": "Some movie title",
"release_date": "2022-02-26"
}'
```
Delete a movie
```
curl --request DELETE \
--url http://localhost:3000/api/movies/26
```