Aschenblog: Thoughts on Code and Fabrication

Rails Engine API Endpoint Authentication Using Rack Middleware

At work, we recently discussed how best to authenticate an API endpoint that we needed from a rails engine. In the host application, we require that clients use tokens for API authentication. In a parent class before filter, we call a remote web service to validate these tokens. We all agreed that it was a bad idea to copy this code over from the host into the engine.

There were a couple of reasons for this. Not only would the code be duplicated, but we felt like it would be a better separation of concerns to keep authentication out of the engine. Another argument is that we would not want to impose an authentication system from the gem on the host application. Ryan Bigg expands on this in a blog post on Forem.

jRuby Test Drive

After hearing about jRuby on several Ruby Rogues podcasts and reading posts about it online, I wanted to give it a shot.

Installation is super easy if you use rvm:

1
rvm install jruby

This will install several dependencies (like the patch, gawk, g++ and the JDK if you do not already have them installed). The installation and setup took about seven minutes on a clean Ubuntu 12.04 Vagrant box I spun up for testing purposes.

Let’s say hello in irb:

1
2
> java.lang.System.out.println 'Hello World!'
Hello World!

Specifying the package is a little cumbersome. Importing the java.lang.System makes things a little easier:

1
2
3
> import java.lang.System
> System.out.println 'Hello World!'
Hello World!

Simple Isometric Tiling

In this post I share a few simple javascript examples of isometric tiling commonly used to create the illusion of 3D graphics in games.

For years, side-scrolling video games use a projection where the camera is aligned along one of the axes. Many original game titles like Tetris, Zelda, Super Mario Bros place the camera either above the player looking down along a vertical axis or looking directly from the side. Basic techniques like shading and parallax scrolling (where foreground images scroll faster than background images) help to provide a sense of depth.

An isometric projection is a popular way of visualizing 3D objects on a 2D screen. This involves rotating the camera 45 degrees to one side and then angling down roughly 30 degrees. This approach is used in several role playing and strategy games (Sim City 2000 pictured below). Q*bert, released in 1982, was perhaps one of the first games that used isometric graphics.

Writing a Small DSL Using Rack

In this post, I write a small domain specific language to handle HTTP requests (inspired by Sinatra) using Rack. We start by taking a quick look at a few small rack applications and then write a small DSL.

Rack

Rack provides an abstraction layer between multiple web servers (unicorn, webrick, thin, etc) and your ruby application. This allows developers to focus on the application layer instead of working on handling the low level details associated with dealing with a HTTP request.

Let’s take a look at the “hello world” example provided by Rack:

1
2
3
4
5
6
7
require 'rack'

app = Proc.new do |env|
  ['200', {'Content-Type' => 'application/json'}, ['A barebones rack app.']]
end

Rack::Handler::WEBrick.run app

To use rack, one must provide an object (class, proc or lambda) that responds to call and returns an array with elements. The first element is a stringified HTTP status code. The second is a hash of response headers. The last element is the response body, which must respond to the each method.

Let’s run the rack app in one terminal and issue a HTTP request against it in a different one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ curl -v localhost:8080
> GET / HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Server: WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)
< Date: Fri, 20 Feb 2015 06:19:57 GMT
< Content-Length: 18
< Connection: Keep-Alive
<
Barebones rack app

Freebase Astronomy Gems

With 47 million topics and 2.7 billion facts, Freebase has an imposing amount of community curated data. Unfortunately, they are closing up shop by mid 2015. Their plan is to help support the up and coming Wikimedia Foundation’s project Wikidata by assisting in transferring their data to this project. Given the short amount of time they are online, I wanted to do a small project using their data sets.

I am interested in astronomy, but found the Freebase JSON data a little unwieldy. I decided to write a pair gems:

  • The astronomy gem facilitates browsing and search of astronomical phenomena. Each topic includes a name, description, an array of image URLs and a link to the original detailed Freebase data via Google APIs.
  • The astronomy_engine wraps the astronomy gem and has a web interface and can easily be included in a Rails project:

Building Better Worlds With Terragen 3

For years I have been interested in modeling terrain. I wrote 2D and 3D fractal terrain generators for fun when I was in school. To help inspire a side project I am working on, I utilized an excellent rendering engine for natural environments and terrain called Terragen 3 from Planetside Software. Here are a couple of screen shots from their image gallery:

Game Time

My team laughed quite a bit recently while playing Pictionary at lunchtime over the past several weeks. We have had a few HR (not safe for work) fails in addition to several entertaining missed phrases (including Pizza Gazebo instead of Pizza Hut). We use tablets as our drawing surfaces to save paper, which introduces challenges when trying to draw rapidly. To find words, we used the Game Gal’s word generator.

I love words and decided to look at the word generator implementation to see if I could see if I could use their API to get categorized word lists. I used Chrome’s POSTman tool to issue GET requests to the following URL:

Pet Cemetery Headstones

This year we decided to take on a small project to help decorate the front yard for Halloween. We were sad to lose our dear orange tabby cat Frank this July. To memorialize Frank and our pets from years past, we decided to create a small faux pet cemetary in the front yard.

Here are the raw materials we used:

  • 1” x 48” x 96” Owens Corning FOAMULAR insulation (about $20, Home Depot)
  • Grey and black spray primer (about $6 each)
  • 5x Paint mixing sticks (free, Home Depot)
  • 10x 1 ¼” long wood screws

Cutting the foam

Time Lapse at 8,600 Feet

While on vacation near Devil’s Thumb Ranch I decided to try some time lapse photography. I wrote a bash script that took pictures on a laptop a few years ago. I ran this script using cron, but was looking for something with finer grained control. I found a Tenderlovemaking post that shows how to take web cam photos using Ruby with the AVCapture framework. Note that this appears to only work on OSX.

I modified the script from Tenderlove’s av_capture gem’s github page:

Building a Gem for RubyGems.org

We are going through some large code refactors at work and are building rails engines. This requires creating apps within our main app. Each sub-app resides in its own gem and is typically mounted or consumed in some way by the host application. While I work on several gems for different projects at work, I had never developed one from scratch and submitted it to RubyGems. I took some previous work on a SMS Scrabble cheater app to help motivate the project.

Developing the skeleton for a gem is extremely easy: