Mojo Wonk Blog.

A semi-offical blog dedicated to the Mojolicious web framework

Day 12: More Than a Base Class

Golden Gate Bridge viewed and Fort Point

Through this series, you've seen the module Mojo::Base referenced several times, though briefly and mostly in passing. It shouldn't be taken lightly however, it packs a lot of punch in one import statement! Nearly every file in the Mojolicious distribution uses it, either directly or indirectly. So what is it?

First it imports several handy pragma that make your code safer and some features that are useful. Second, it can be a base class to the current package, or establish a parent class, or even define a role. Let's see how it does it.

Continue reading Day 12: More Than a Base Class...

Day 10: Give the Customer What They Want

Wood bar counter with chairs

Writing an API can be as easy as taking the results of some database query and presenting it to the user. A more advanced one can often present the data in one of multiple formats. The user can then specify which format they want.

JSON is currently the most popular format for new APIs. XML is another common one and was certainly king before JSON hit the scene. An API might choose to make an HTML format of the data available, whether in some representation of the data or to render documentation about the API itself. Of course there are many others.

Mojolicious believes in Content Negotiation, as it is called, and supports it throughout the stack. Mojolicious makes it easy and convenient for users to pick a format they would like. Similarly it makes rendering the different formats easy on the site author, as you would expect.

Continue reading Day 10: Give the Customer What They Want...

Day 9: The Best Way to Test

Woman using chemistry lab equipment

Ok so it is a bit of a click-bait headline. But that doesn't mean I don't believe it.

Test::Mojo is a test framework for websites and related technologies. While its true that there are many such tools, this one gets its power comes from combining so many of the tools that Mojolicious provides. A full non-blocking web server, including websockets, an event loop, an XML/HTML DOM parser, JSON parser and emitter, and more all come together to make incredibly detailed testing simple. Further, with the recent additions in support of roles (which will be discussed in a future post), Test::Mojo is becoming an extensible testing platform.

In this article, I'll give a quick overview of how to use Test::Mojo and some of its methods. Rest assured you'll see more of it as the series continues.

Continue reading Day 9: The Best Way to Test...

Day 8: Mocking a REST API

Two crash test dummies

One of my applications is a pure-JavaScript UI for a JSON API. This UI is an entirely different project that communicates with a public API using an OpenAPI specification.

Our public API is huge and complex: To set up the public API, I need a database, sample data, and three other private API servers that perform individual tasks as directed by the public API. Worse, I would need to set up a lot of different test scenarios with different kinds of data.

It would be a lot easier to set up a mock public API that I could use to test my UI, and it turns out that Mojolicious makes this very easy.

Continue reading Day 8: Mocking a REST API...