Solution: Meal Categories
Go over the implementation of the meal categories retrieval problem.
We'll cover the following...
We'll cover the following...
Solution
# This program download
require 'net/http'
require 'uri'
def get_url_content(url)
puts "Conntecting to server..."
begin
url_content = Net::HTTP.get(URI.parse(url))
puts "Conntected!"
return url_content
rescue => e
puts "Unable to connect to the server, Error: '#{e}'"
exit(-1)
end
end
def retrieve_meal_categories
require 'json'
json_str = get_url_content("https://www.themealdb.com/api/json/v1/{{FREE_API_KEY}}/list.php?c=list")
begin
json_obj = JSON.parse(json_str)
return json_obj["meals"]
rescue => e
puts "Unable to parse JSON object, Error: #{e}"
exit(-1)
end
end
array = retrieve_meal_categories
puts "Here are the meal categories: "
array.each do |hash|
puts hash["strCategory"]
end
Retrieving meal categories from a live service
Explanation
Lines 5–15: The
get_url_contentmethod connects to the internet and handles any exception ...