Skip to content

Nsanjayboruds/keploy-ruby-postgresql-quickstart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Ruby Books API - Keploy Quickstart

A simple CRUD REST API built with Ruby (Sinatra) and PostgreSQL demonstrating Keploy integration for automated API testing.

πŸš€ Features

  • βœ… 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

πŸ“‹ Prerequisites

For Local Setup:

  • Ruby 3.2 or higher
  • PostgreSQL 15 or higher
  • Bundler (gem install bundler)

For Docker Setup:

  • Docker (20.10 or higher)
  • Docker Compose (v2.0 or higher)

For Keploy:

πŸ› οΈ Installation & Setup

Option 1: Local Setup (Without Docker)

1. Clone the repository and install dependencies

# Install Ruby dependencies
bundle install

2. Set up PostgreSQL database

# Create database
createdb booksdb

# Run initialization script
psql -d booksdb -f init.sql

3. Configure environment variables

cp .env.example .env
# Edit .env if needed for your local PostgreSQL configuration

4. Start the application

bundle exec ruby app.rb

The API will be available at http://localhost:8000

5. Verify the setup

curl http://localhost:8000/health
# Expected: {"status":"healthy","service":"Ruby Books API"}

Option 2: Docker Compose Setup (Recommended)

1. Build and start services

docker-compose up --build

This will:

  • Start a PostgreSQL container
  • Build and start the Ruby application container
  • Initialize the database with sample data
  • Expose the API on port 8000

2. Verify the setup

curl http://localhost:8000/health
# Expected: {"status":"healthy","service":"Ruby Books API"}

3. Stop services

docker-compose down

To remove volumes as well:

docker-compose down -v

πŸ§ͺ Testing with Keploy

Installation

If 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 version

Record Mode

Keploy will capture all API calls and database interactions:

For Local Setup:

keploy record -c "bundle exec ruby app.rb"

For Docker Setup:

keploy record -c "docker-compose up" --container-name "ruby-books-app"

Make API Calls

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/1

Keploy will automatically record these interactions as test cases.

Replay Mode

Run the recorded test cases:

For Local Setup:

keploy test -c "bundle exec ruby app.rb" --delay 10

For Docker Setup:

keploy test -c "docker-compose up" --container-name "ruby-books-app" --delay 10

Keploy will replay all recorded requests and verify the responses match the recorded ones.


πŸ“– API Endpoints

Health Check

GET /health

Response:

{
  "status": "healthy",
  "service": "Ruby Books API"
}

Get All Books

GET /books

Response:

{
  "books": [
    {
      "id": 1,
      "title": "The Great Gatsby",
      "author": "F. Scott Fitzgerald",
      "isbn": "9780743273565",
      "published_year": 1925
    }
  ],
  "count": 1
}

Get Book by ID

GET /books/:id

Response:

{
  "id": 1,
  "title": "The Great Gatsby",
  "author": "F. Scott Fitzgerald",
  "isbn": "9780743273565",
  "published_year": 1925
}

Create Book

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
  }
}

Update Book

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 Book

DELETE /books/:id

Response:

{
  "message": "Book deleted successfully"
}

πŸ” Expected Outputs

Keploy Record Output

 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 Replay Output

 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
 ────────────────────────────────────

πŸ—οΈ Project Structure

.
β”œβ”€β”€ 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

πŸ› Troubleshooting

Database Connection Issues

If you see database connection errors:

  1. Local setup: Ensure PostgreSQL is running

    sudo service postgresql status
    sudo service postgresql start
  2. Docker setup: Check if containers are healthy

    docker-compose ps
    docker-compose logs postgres

Port Already in Use

If port 8000 is already in use:

  1. Change the port in .env (local setup)
  2. Or modify docker-compose.yml ports section (Docker setup)

Keploy Issues

  1. Ensure you're running on Linux/WSL2
  2. Check Keploy logs for detailed error messages
  3. Try increasing the delay: --delay 15

πŸ“š Learn More


🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.


πŸ“„ License

This project is open source and available under the MIT License.


✨ Acknowledgments


Happy Testing! πŸŽ‰

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published