Search⌘ K
AI Features

Meal Categories

Explore how to write a Ruby program that connects to a live recipe API, retrieves meal categories, handles JSON parsing, and manages exceptions for network issues. This lesson teaches you to work with APIs, fetch and process live data, and apply error handling to ensure robust program execution.

Problem

Write a program that uses a live service for an online collection of recipes, and retrieves all meal categories listed there.

Here are the meal categories:
Beef
Breakfast
Chicken
Dessert
Goat
Lamb
Miscellaneous
Pasta
Pork
Seafood
Side
Starter
Vegan
Vegetarian
Demo retrieved meal categories

If you cannot connect to the service (for example, there is no internet connection or the server is down), the program shall exit gracefully with an error message. For example, if a service https://www.themealdb.com/ is suspended, and we try to connect to it, then we should be shown the following error message:

Unable to connect to the server, Error: 'Failed to open TCP connection
to themealdb.com/:80 (getaddrinfo: Name or service not known)'
Exiting the program by showing an error message

Purpose

  • Get content from a URL

  • Parse a ...