TL;DR

Serving interesting quotes through a simple Mojolicious::Lite application is not difficult, enjoy Quotes-O-Matic.

Our journey to transform a thread of tweets into a simple web page providing random wisdom (which we started with post Scrape a Thread of Tweets a few days ago) is coming to an end, because we’re now adding an important piece to the puzzle:

In case GitLab is giving issues, take a look at the local version.

The code pretty much explain itself. We’re relying upon an input file in JSON format (named quotes.json, line 12) which we hunt for either in the current directory, or in the program’s directory (line 45).

The internal structure of the file is the same we described before in the previous post about jq magic. Line 41 does the heavylifting of reading the whole contents of the file (via slurp) and turn that into an anonymous has (calling j on the JSON text).

Selection of a random quote is an old trick in Perl (line 34): generate a random number between 0 (inclusive) and the number of items in an array (exclusive) and use that as an index inside the array itself: it will be turned into an integer and used to extract one item.

The handler for the only supported endpoint can be understood reading through the Mojolicious tutorial.

See you soon!

Nothing more to add… apart wishing you happy hacking!