module BBS::HTTP

HTTP requests of a board to the services it shows (an encyclopedia, a feed, a catalog), bounded so a slow or huge answer cannot hold a caller’s thread or fill the memory: a deadline for the whole request, a cap on the body, a few redirects to http and https only, and by default no request to an address that is not on the public internet.

response = BBS::HTTP.get('https://en.wikipedia.org/w/api.php?action=query&format=json')
JSON.parse(response.body) if response.ok?

Every failure (no answer, a timeout, a body over the cap, a refused address, a bad redirect) raises BBS::HTTP::Error, with a message that names the address without its query string, so no key or search term lands in a log. A body over the cap raises its subclass TooLarge, as soon as the Content-Length says so.

A big download goes to an IO instead of the memory:

File.open('book.txt', 'wb') { |file| BBS::HTTP.get(url, to: file, max_bytes: 64 * 1024 * 1024) }