| id | samples-ruby | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| title | Sample Books API | ||||||||
| sidebar_label | Ruby + Postgres | ||||||||
| description | The following sample app showcases how to use the Ruby (Sinatra) framework and the Keploy Platform. | ||||||||
| tags |
|
||||||||
| keyword |
|
import InstallReminder from '@site/src/components/InstallReminder'; import SectionDivider from '@site/src/components/SectionDivider';
🪄 Dive into the world of Books CRUD API and see how seamlessly Keploy integrates with Ruby (Sinatra) and PostgreSQL. Buckle up, it's gonna be a fun ride! 🎢
- Ruby 3.2 or higher
- PostgreSQL 15 or higher
- Bundler (
gem install bundler)
- Docker (20.10 or higher)
- Docker Compose (v2.0 or higher)
We will be using Docker compose to run the application as well as PostgreSQL on Docker container.
git clone https://github.com/Nsanjayboruds/keploy-ruby-postgresql-quickstart.git && cd keploy-ruby-postgresql-quickstartdocker-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/healthExpected Response:
{"status":"healthy","service":"Ruby Books API"}Capture the test-cases-
keploy record -c "docker-compose up" --container-name "ruby-books-app"🔥Make some API calls. Postman, Hoppscotch or even curl - take your pick!
To generate testcases we just need to make some API calls.
curl http://localhost:8000/bookscurl http://localhost:8000/books/1curl -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
}'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
}'curl -X DELETE http://localhost:8000/books/1And once you are done, you can stop the recording and give yourself a pat on the back! With that simple spell, you've conjured up test cases with mocks! Explore the keploy directory and you'll discover your handiwork in the tests directory and mocks.yml.
Want to see if everything works as expected?
Time to put things to the test 🧪
keploy test -c "docker-compose up" --container-name "ruby-books-app" --delay 10The
--delayflag? Oh, that's just giving your app a little breather (in seconds) before the test cases come knocking.
Final thoughts? Dive deeper! Try different API calls, tweak the DB response in the mocks.yml, or fiddle with the request or response in test-x.yml. Run the tests again and see the magic unfold!✨👩💻👨💻✨
We'll be running our sample application locally on Linux/WSL. There are 2 ways you can run this application:
git clone https://github.com/Nsanjayboruds/keploy-ruby-postgresql-quickstart.git && cd keploy-ruby-postgresql-quickstartbundle installCreate the database:
createdb booksdbInitialize the database with the schema:
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/healthExpected Response:
{"status":"healthy","service":"Ruby Books API"}Capture the test-cases-
keploy record -c "bundle exec ruby app.rb"🔥Make some API calls. Postman, Hoppscotch or even curl - take your pick!
To generate testcases we just need to make some API calls.
curl http://localhost:8000/bookscurl http://localhost:8000/books/1curl -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
}'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
}'curl -X DELETE http://localhost:8000/books/1And once you are done, you can stop the recording and give yourself a pat on the back! With that simple spell, you've conjured up test cases with mocks! Explore the keploy directory and you'll discover your handiwork in the tests directory and mocks.yml.
Want to see if everything works as expected?
Time to put things to the test 🧪
keploy test -c "bundle exec ruby app.rb" --delay 10The
--delayflag? Oh, that's just giving your app a little breather (in seconds) before the test cases come knocking.
Final thoughts? Dive deeper! Try different API calls, tweak the DB response in the mocks.yml, or fiddle with the request or response in test-x.yml. Run the tests again and see the magic unfold!✨👩💻👨💻✨
Happy coding! ✨👩💻👨💻✨