Mojo Wonk Blog.

A semi-offical blog dedicated to the Mojolicious web framework

Day 22: A RESTful API with OpenAPI

A hammock on a beach

The OpenAPI Specification (formerly named Swagger) is an API description format for REST APIs. An API specification written using the the rules set by the Open API Initiative can be used to describe:

  • All the available endpoints. An endpoint is a unique resource that can access or modify a given object.
  • Input parameters, such as headers, query parameters and/or body parameters.
  • The structure of the response, including headers status codes and the body - if any.
  • Authentication methods
  • Contact information, license, terms of use and other information

This post look into how to write an API specification and how to use it together with Mojolicious::Plugin::OpenAPI and OpenAPI::Client.

Continue reading Day 22: A RESTful API with OpenAPI...

Day 21: Virtual(ly a) Lumberjack

Woman wearing VR goggles outside

What do you do when you want to split up a stream of data in real-time while giving the user instructions?

This is just what I wanted to do to aid in reverse-engineering the USB protocol of Virtual Reality devices known as Head Mounted Displays (HMD), for the OpenHMD project.

HMDs are used to create virtual reality environments. When worn, two slightly different images are drawn to each side of the screen, with each side visible to only one eye. This imitates binocular vision and creates an image with a feeling of depth. By tracking the rotation of the unit, the user can then look around this environment.

The recent resurgence of Virtual Reality devices can be attributed to the Rift DK1, released by Oculus in March 2013.

By logging the packets generated during each movement, we can compare the content of each log to identify which bytes are related to which action. Such movements include roll (tilting head side-to-side), pitch (looking up and down) and yaw (turning left/right). Though position isn't tracked, we also look for sway (left-right translation), surge (back and forth) and heave (up and down) information as this is used in combination with the other values for accurate tracking of rotation.

Mojo is an amazing toolkit for web development, as shown in previous calendar entries, but using components of it can also solve problems in other non-web spaces like these. Why use Mojo for this? Because it makes it easy.

Continue reading Day 21: Virtual(ly a) Lumberjack...

Day 19: Make Your App Installable

Container ship loading at the dock

Thus far we have always run our applications from the local directory. That is usually the project root directory and/or the repository checkout. But did you know that with only a few changes you can make your application installable like other Perl modules?

While, you must do this if you want to upload your application to CPAN, even if you don't intend to do that, it still has benefits. You can install the application on your personal computer, especially if you want to be able to run the script while in other directories. Having an installable module also means that you can use a so-called "DarkPAN" and related tools to build yourself a local "CPAN". If you have multiple Perl modules at your company (or as part of any project) using a DarkPAN can ease integration and deployment immensely! N.B. there is even a DarkPAN tool written using Mojolicious called opan.

And hey if you needed even more more reason, it cleans up your project root directory somewhat too!

Continue reading Day 19: Make Your App Installable...

Day 18: The Wishlist Model

Still Life: Vase with Pink Roses by Vincent van Gogh

Yesterday we discussed templates features like layouts, partial templates, and content buffers. We motivated the discussion by introducing a fully functioning example application that tracked user's Christmas wishlists. That application did lack sophistication in the area of data storage, using DBM::Deep for quickly getting a basic persistence layer. While that worked well enough to demonstrate template functionality it was no triumph of the model layer. Indeed some very hack techniques are used, especially in manipulating wishlist items, since there was no unique record ids.

Well as promised I have created a repository for the application on Github. I have also added several tags. While development on the application may continue, those tags will remain for future readers.

The initial form of the application (as seen in yesterday's post) is tagged blog_post/dbm_deep. You are then invited to step through the commits from that one to blog_post/full_app to follow along as I port it from a Lite to a Full app; a practical demonstration of what we saw on Day 4.

This article will briefly discuss the application as it exists in the next tag, blog_post/sqlite_model. At this point I have replaced DBM::Deep with Mojo::SQLite, written a rudimentary model layer for it, and connected the two with the application via helpers. Let's see how that improves the application and in the meantime, get a look at idiomatic database access in Mojolicious!

Continue reading Day 18: The Wishlist Model...