|
| 1 | +--- |
| 2 | +id: samples-ruby |
| 3 | +title: Sample Books API |
| 4 | +sidebar_label: Ruby + Postgres |
| 5 | +description: The following sample app showcases how to use the Ruby (Sinatra) framework and the Keploy Platform. |
| 6 | +tags: |
| 7 | + - ruby |
| 8 | + - quickstart |
| 9 | + - samples |
| 10 | + - examples |
| 11 | + - tutorial |
| 12 | + - sinatra |
| 13 | + - postgresql |
| 14 | + - ruby-framework |
| 15 | +keyword: |
| 16 | + - Sinatra Framework |
| 17 | + - PostgreSQL |
| 18 | + - Ruby |
| 19 | + - API Test generator |
| 20 | + - Auto case generation |
| 21 | +--- |
| 22 | + |
| 23 | +import InstallReminder from '@site/src/components/InstallReminder'; |
| 24 | +import SectionDivider from '@site/src/components/SectionDivider'; |
| 25 | + |
| 26 | +## Introduction |
| 27 | + |
| 28 | +πͺ Dive into the world of Books CRUD API and see how seamlessly Keploy integrates with [Ruby (Sinatra)](http://sinatrarb.com/) and [PostgreSQL](https://www.postgresql.org/). Buckle up, it's gonna be a fun ride! π’ |
| 29 | + |
| 30 | +<InstallReminder /> |
| 31 | + |
| 32 | +## Prerequisites |
| 33 | + |
| 34 | +### For Local Setup: |
| 35 | +- Ruby 3.2 or higher |
| 36 | +- PostgreSQL 15 or higher |
| 37 | +- Bundler (`gem install bundler`) |
| 38 | + |
| 39 | +### For Docker Setup: |
| 40 | +- Docker (20.10 or higher) |
| 41 | +- Docker Compose (v2.0 or higher) |
| 42 | + |
| 43 | +<SectionDivider /> |
| 44 | + |
| 45 | +## Using Docker Compose π³ |
| 46 | + |
| 47 | +We will be using Docker compose to run the application as well as PostgreSQL on Docker container. |
| 48 | + |
| 49 | +### Clone the Application π§ͺ |
| 50 | + |
| 51 | +```bash |
| 52 | +git clone https://github.com/Nsanjayboruds/keploy-ruby-postgresql-quickstart.git && cd keploy-ruby-postgresql-quickstart |
| 53 | +``` |
| 54 | + |
| 55 | +### Build and Start Services |
| 56 | + |
| 57 | +```bash |
| 58 | +docker-compose up --build |
| 59 | +``` |
| 60 | + |
| 61 | +This will: |
| 62 | +- Start a PostgreSQL container |
| 63 | +- Build and start the Ruby application container |
| 64 | +- Initialize the database with sample data |
| 65 | +- Expose the API on port 8000 |
| 66 | + |
| 67 | +### Verify the Setup |
| 68 | + |
| 69 | +```bash |
| 70 | +curl http://localhost:8000/health |
| 71 | +``` |
| 72 | + |
| 73 | +Expected Response: |
| 74 | +```json |
| 75 | +{"status":"healthy","service":"Ruby Books API"} |
| 76 | +``` |
| 77 | + |
| 78 | +### Lights, Camera, Record! π₯ |
| 79 | + |
| 80 | +Capture the test-cases- |
| 81 | + |
| 82 | +```bash |
| 83 | +keploy record -c "docker-compose up" --container-name "ruby-books-app" |
| 84 | +``` |
| 85 | + |
| 86 | +π₯**Make some API calls**. Postman, Hoppscotch or even curl - take your pick! |
| 87 | + |
| 88 | +### Generate Testcases |
| 89 | + |
| 90 | +To generate testcases we just need to **make some API calls.** |
| 91 | + |
| 92 | +#### 1. Get All Books |
| 93 | + |
| 94 | +```bash |
| 95 | +curl http://localhost:8000/books |
| 96 | +``` |
| 97 | + |
| 98 | +#### 2. Get a Specific Book |
| 99 | + |
| 100 | +```bash |
| 101 | +curl http://localhost:8000/books/1 |
| 102 | +``` |
| 103 | + |
| 104 | +#### 3. Create a New Book |
| 105 | + |
| 106 | +```bash |
| 107 | +curl -X POST http://localhost:8000/books \ |
| 108 | + -H "Content-Type: application/json" \ |
| 109 | + -d '{ |
| 110 | + "title": "The Hobbit", |
| 111 | + "author": "J.R.R. Tolkien", |
| 112 | + "isbn": "9780547928227", |
| 113 | + "published_year": 1937 |
| 114 | + }' |
| 115 | +``` |
| 116 | + |
| 117 | +#### 4. Update a Book |
| 118 | + |
| 119 | +```bash |
| 120 | +curl -X PUT http://localhost:8000/books/1 \ |
| 121 | + -H "Content-Type: application/json" \ |
| 122 | + -d '{ |
| 123 | + "title": "The Great Gatsby (Updated)", |
| 124 | + "author": "F. Scott Fitzgerald", |
| 125 | + "isbn": "9780743273565", |
| 126 | + "published_year": 1925 |
| 127 | + }' |
| 128 | +``` |
| 129 | + |
| 130 | +#### 5. Delete a Book |
| 131 | + |
| 132 | +```bash |
| 133 | +curl -X DELETE http://localhost:8000/books/1 |
| 134 | +``` |
| 135 | + |
| 136 | +And 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`. |
| 137 | + |
| 138 | +Want to see if everything works as expected? |
| 139 | + |
| 140 | +### Run Tests π§ͺ |
| 141 | + |
| 142 | +Time to put things to the test π§ͺ |
| 143 | + |
| 144 | +```bash |
| 145 | +keploy test -c "docker-compose up" --container-name "ruby-books-app" --delay 10 |
| 146 | +``` |
| 147 | + |
| 148 | +> The `--delay` flag? Oh, that's just giving your app a little breather (in seconds) before the test cases come knocking. |
| 149 | +
|
| 150 | +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!β¨π©βπ»π¨βπ»β¨ |
| 151 | + |
| 152 | +<SectionDivider /> |
| 153 | + |
| 154 | +## Running App Locally on Linux/WSL π§ |
| 155 | + |
| 156 | +We'll be running our sample application locally on Linux/WSL. There are 2 ways you can run this application: |
| 157 | + |
| 158 | +### Clone the Application π§ͺ |
| 159 | + |
| 160 | +```bash |
| 161 | +git clone https://github.com/Nsanjayboruds/keploy-ruby-postgresql-quickstart.git && cd keploy-ruby-postgresql-quickstart |
| 162 | +``` |
| 163 | + |
| 164 | +### Install Dependencies |
| 165 | + |
| 166 | +```bash |
| 167 | +bundle install |
| 168 | +``` |
| 169 | + |
| 170 | +### Set up PostgreSQL Database |
| 171 | + |
| 172 | +Create the database: |
| 173 | + |
| 174 | +```bash |
| 175 | +createdb booksdb |
| 176 | +``` |
| 177 | + |
| 178 | +Initialize the database with the schema: |
| 179 | + |
| 180 | +```bash |
| 181 | +psql -d booksdb -f init.sql |
| 182 | +``` |
| 183 | + |
| 184 | +### Configure Environment Variables |
| 185 | + |
| 186 | +```bash |
| 187 | +cp .env.example .env |
| 188 | +# Edit .env if needed for your local PostgreSQL configuration |
| 189 | +``` |
| 190 | + |
| 191 | +### Start the Application |
| 192 | + |
| 193 | +```bash |
| 194 | +bundle exec ruby app.rb |
| 195 | +``` |
| 196 | + |
| 197 | +The API will be available at `http://localhost:8000` |
| 198 | + |
| 199 | +### Verify the Setup |
| 200 | + |
| 201 | +```bash |
| 202 | +curl http://localhost:8000/health |
| 203 | +``` |
| 204 | + |
| 205 | +Expected Response: |
| 206 | +```json |
| 207 | +{"status":"healthy","service":"Ruby Books API"} |
| 208 | +``` |
| 209 | + |
| 210 | +### Lights, Camera, Record! π₯ |
| 211 | + |
| 212 | +Capture the test-cases- |
| 213 | + |
| 214 | +```bash |
| 215 | +keploy record -c "bundle exec ruby app.rb" |
| 216 | +``` |
| 217 | + |
| 218 | +π₯**Make some API calls**. Postman, Hoppscotch or even curl - take your pick! |
| 219 | + |
| 220 | +### Generate Testcases |
| 221 | + |
| 222 | +To generate testcases we just need to **make some API calls.** |
| 223 | + |
| 224 | +#### 1. Get All Books |
| 225 | + |
| 226 | +```bash |
| 227 | +curl http://localhost:8000/books |
| 228 | +``` |
| 229 | + |
| 230 | +#### 2. Get a Specific Book |
| 231 | + |
| 232 | +```bash |
| 233 | +curl http://localhost:8000/books/1 |
| 234 | +``` |
| 235 | + |
| 236 | +#### 3. Create a New Book |
| 237 | + |
| 238 | +```bash |
| 239 | +curl -X POST http://localhost:8000/books \ |
| 240 | + -H "Content-Type: application/json" \ |
| 241 | + -d '{ |
| 242 | + "title": "The Hobbit", |
| 243 | + "author": "J.R.R. Tolkien", |
| 244 | + "isbn": "9780547928227", |
| 245 | + "published_year": 1937 |
| 246 | + }' |
| 247 | +``` |
| 248 | + |
| 249 | +#### 4. Update a Book |
| 250 | + |
| 251 | +```bash |
| 252 | +curl -X PUT http://localhost:8000/books/1 \ |
| 253 | + -H "Content-Type: application/json" \ |
| 254 | + -d '{ |
| 255 | + "title": "The Great Gatsby (Updated)", |
| 256 | + "author": "F. Scott Fitzgerald", |
| 257 | + "isbn": "9780743273565", |
| 258 | + "published_year": 1925 |
| 259 | + }' |
| 260 | +``` |
| 261 | + |
| 262 | +#### 5. Delete a Book |
| 263 | + |
| 264 | +```bash |
| 265 | +curl -X DELETE http://localhost:8000/books/1 |
| 266 | +``` |
| 267 | + |
| 268 | +And 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`. |
| 269 | + |
| 270 | +Want to see if everything works as expected? |
| 271 | + |
| 272 | +### Run Tests π§ͺ |
| 273 | + |
| 274 | +Time to put things to the test π§ͺ |
| 275 | + |
| 276 | +```bash |
| 277 | +keploy test -c "bundle exec ruby app.rb" --delay 10 |
| 278 | +``` |
| 279 | + |
| 280 | +> The `--delay` flag? Oh, that's just giving your app a little breather (in seconds) before the test cases come knocking. |
| 281 | +
|
| 282 | +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!β¨π©βπ»π¨βπ»β¨ |
| 283 | + |
| 284 | +Happy coding! β¨π©βπ»π¨βπ»β¨ |
0 commit comments