A simple CRUD REST API built with Ruby (Sinatra) and PostgreSQL demonstrating Keploy integration for automated API testing.
- β Complete CRUD operations for books management
- β PostgreSQL database integration
- β Docker Compose setup (app + database)
- β Keploy integration for record and replay
- β RESTful API design
- β JSON request/response handling
- β Error handling and validation
- Ruby 3.2 or higher
- PostgreSQL 15 or higher
- Bundler (
gem install bundler)
- Docker (20.10 or higher)
- Docker Compose (v2.0 or higher)
- Keploy CLI (Installation Guide)
- Linux/WSL2 environment (Keploy requirement)
# Install Ruby dependencies
bundle install# Create database
createdb booksdb
# Run initialization script
psql -d booksdb -f init.sqlcp .env.example .env
# Edit .env if needed for your local PostgreSQL configurationbundle exec ruby app.rbThe API will be available at http://localhost:8000
curl http://localhost:8000/health
# Expected: {"status":"healthy","service":"Ruby Books API"}docker-compose up --buildThis will:
- Start a PostgreSQL container
- Build and start the Ruby application container
- Initialize the database with sample data
- Expose the API on port 8000
curl http://localhost:8000/health
# Expected: {"status":"healthy","service":"Ruby Books API"}docker-compose downTo remove volumes as well:
docker-compose down -vIf you haven't installed Keploy yet:
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp
sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy versionKeploy will capture all API calls and database interactions:
keploy record -c "bundle exec ruby app.rb"keploy record -c "docker-compose up" --container-name "ruby-books-app"Open a new terminal and make some API requests:
# Get all books
curl http://localhost:8000/books
# Get a specific book
curl http://localhost:8000/books/1
# Create a new book
curl -X POST http://localhost:8000/books \
-H "Content-Type: application/json" \
-d '{
"title": "The Hobbit",
"author": "J.R.R. Tolkien",
"isbn": "9780547928227",
"published_year": 1937
}'
# Update a book
curl -X PUT http://localhost:8000/books/1 \
-H "Content-Type: application/json" \
-d '{
"title": "The Great Gatsby (Updated)",
"author": "F. Scott Fitzgerald",
"isbn": "9780743273565",
"published_year": 1925
}'
# Delete a book
curl -X DELETE http://localhost:8000/books/1Keploy will automatically record these interactions as test cases.
Run the recorded test cases:
keploy test -c "bundle exec ruby app.rb" --delay 10keploy test -c "docker-compose up" --container-name "ruby-books-app" --delay 10Keploy will replay all recorded requests and verify the responses match the recorded ones.
GET /health
Response:
{
"status": "healthy",
"service": "Ruby Books API"
}GET /books
Response:
{
"books": [
{
"id": 1,
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"isbn": "9780743273565",
"published_year": 1925
}
],
"count": 1
}GET /books/:id
Response:
{
"id": 1,
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"isbn": "9780743273565",
"published_year": 1925
}POST /books
Content-Type: application/json
{
"title": "Book Title",
"author": "Author Name",
"isbn": "1234567890",
"published_year": 2024
}
Response:
{
"message": "Book created successfully",
"book": {
"id": 6,
"title": "Book Title",
"author": "Author Name",
"isbn": "1234567890",
"published_year": 2024
}
}PUT /books/:id
Content-Type: application/json
{
"title": "Updated Title",
"author": "Updated Author",
"isbn": "1234567890",
"published_year": 2024
}
Response:
{
"message": "Book updated successfully",
"book": {
"id": 1,
"title": "Updated Title",
"author": "Updated Author",
"isbn": "1234567890",
"published_year": 2024
}
}DELETE /books/:id
Response:
{
"message": "Book deleted successfully"
} KEPLOY RECORD MODE
βββββββββββββββββββββββββββββββββββββββββββββββ
π Recording API calls...
β
Test case recorded: test-1
β
Test case recorded: test-2
β
Test case recorded: test-3
β
Test case recorded: test-4
β
Test case recorded: test-5
Test cases saved to: ./keploy/tests
KEPLOY TEST MODE
βββββββββββββββββββββββββββββββββββββββββββββββ
π§ͺ Running test cases...
Test Case 1: PASSED β
Test Case 2: PASSED β
Test Case 3: PASSED β
Test Case 4: PASSED β
Test Case 5: PASSED β
ββββββββββββββββββββββββββββββββββββ
Test Summary:
Total: 5
Passed: 5
Failed: 0
ββββββββββββββββββββββββββββββββββββ
.
βββ app.rb # Main application file
βββ config.ru # Rack configuration
βββ Gemfile # Ruby dependencies
βββ Dockerfile # Docker image configuration
βββ docker-compose.yml # Docker Compose setup
βββ init.sql # Database initialization script
βββ README.md # This file
If you see database connection errors:
-
Local setup: Ensure PostgreSQL is running
sudo service postgresql status sudo service postgresql start
-
Docker setup: Check if containers are healthy
docker-compose ps docker-compose logs postgres
If port 8000 is already in use:
- Change the port in
.env(local setup) - Or modify
docker-compose.ymlports section (Docker setup)
- Ensure you're running on Linux/WSL2
- Check Keploy logs for detailed error messages
- Try increasing the delay:
--delay 15
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.
- Built with Keploy for automated API testing
- Uses Sinatra web framework
- Database: PostgreSQL
Happy Testing! π