Jason Magee

Must have Ruby on Rails Gems

Feb 8 2015

These are some of my most regularly used Ruby Gems when I’m working with Rails.

Better Errors

As the name suggests, Better Errors takes Rack errors and makes them better. You don’t even have to make any code changes.

Image
Standard
Image
Better Errors

Sidekiq

Sidekiq is a background task processor, similar to Cron, but for Ruby. All you need to do is write the tasks method and tell Sidekiq to execute it asynchronously. Sidekiq also comes with a decent dashboard, these screenshots are from the Gamely Digests Discourse Sidekiq.

Image
Image

Capistrano

Capistrano extends the Rake DSL to provide ways to run scripts on servers. It’s main usage is for deploying apps onto servers. On jotter.io and data.gg I have it set up so running this command…

c a p p r o d u c t i o n d e p l o y

will upload the latest version to my server, run any migrations and restart the app on the server. I can also deploy a staging version or rollback to the previous version if something goes wrong.

On the server the directory structure looks like this…

/ / / / p p p p a a a a t t t t h h h h / / / / t t t t o / a a a a p p p p p p p p / / / / c r r s u e e h r l p a r e o r e a / e n s d t e ( / / s g / i ( ( t h s ( a y c r s m o e b n p t o t o h l a s i i i i n c n t g s o s l r i t y l n h ) i k e k e t l o a p s i t t d h s e c , o v u l e p o r l g y e s , l o a f e t t e r c s e . t l ) e r a e s l e e s a ) s e i n t h e r e l e a s e s f o l d e r )

Devise

Devise is user authentication for Rails. Devise will setup all the parts necessary for users to register, login, reset passwords, etc. If you know your project needs use logins, Devise is a no-brainer. You’re always going to want to modify the way the forms look so Devise provides a command to generate all the views for you to change. For everything else, Devise has a massive ‘How-to’ section on their Wiki.

Simple Form

Simple Form is a tool to help you make forms in Rails. Rails comes with its own way to do forms but I’ve never been happy with it. Simple Form is very similar for the basics.

< < % % = < < < % % % e s = = = n i d m f f f p . . . % l i i b > e n n u _ p p t f u u t o t t o r n m : : _ u p : f s a s o e s u r r s b n w m @ a o i u m r t s e d e % r % % > > > d o | f | % >

Simple Form really shines for me when it comes to associations and configuration. Associations can be done as simply as this…

< < % % = < < % % e s = = n i d m f f p . . % l i a > e n s _ p s f u o o t c r i m : a _ e t f m i o a o r i n l @ : u % a s > p e p r _ a d c o o u | n f t | % % > >

Part of the install process adds some configuration files allowing you to set the input types, internationalization, what classes to apply to what components by default, priority countries in country picker drop-downs and much more.

Gamely Digest image processing

Jan 30 2015

One of the my targets for Gamely Digest is that it is kept simple and pain-free to maintain. For the first few reviews, I had to manually create thumbnails, rename and convert 50 images in GIMP. This drove me to write some bash scripts to automate the process.

The first script loops over every file in the current directory and renames it to gamename_X.jpg (X being a number from 1 to the total count of the files in the directory). Below is an example of the scripts execution.

l s | c a t - n | w h i l e r e a d n f ; d o m v " $ f " " g a m e n a m e _ $ n . j p g " ; d o n e
B 2 2 2 e 0 0 0 f 1 1 1 o 5 5 5 r - - - e 0 0 0 1 1 1 - - - 0 0 0 1 1 1 - - - d d d r r r e e e a a a m m m f f f a a a l l l l l l . 1 2 j . . p j j g p p g g
C l o s m m | a n c d a t - n | w h i l e r e a d n f ; d o m v " $ f " " d r e a m f a l l _ $ n . j p g " ; d o n e
A d d d f r r r t e e e e a a a r m m m f f f a a a l l l l l l _ _ _ 1 2 3 . . . j j j p p p g g g

The next script uses Image Magick which is a fantastic library and toolset to create, edit, compose and convert images. Image Magick is a huge subject and outside the scope of this article but the command I’ve used below is very basic.

This one loops over every JPG file in the current directory, uses the Image Magick convert command to resize the image to 30% of its original size and appends a ‘t’ (t for thumbnail) to the end of the filename.

f o r f i n . j p g ; d o c o n v e r t " $ f " - r e s i z e 3 0 % " $ { f % % . j p g } t . j p g " ; d o n e
B d d d e r r r f e e e o a a a r m m m e f f f a a a l l l l l l _ _ _ 1 2 3 . . . j j j p p p g g g
C f o o m r m a f n d i n . j p g ; d o c o n v e r t " $ f " - r e s i z e 3 0 % " $ { f % % . j p g } t . j p g " ; d o n e
A d d d d d d f r r r r r r t e e e e e e e a a a a a a r m m m m m m f f f f f f a a a a a a l l l l l l l l l l l l _ _ _ _ _ _ 1 1 2 2 3 3 . t . t . t j . j . j . p j p j p j g p g p g p g g g

My next job to tackle is reviewers submitting different size images causing reviews not to line up.

Image

The expanded images can continue to have a different size but thumbnails need to be consistent. I plan on tackling this by using ImageMagick to remove pixels from the edges so we end up with a consistent thumbnail size. I’ll post my solution to this at a later late.

Getting data out of image PDFs without losing your mind

Jan 30 2015

Recently I’ve been gathering data from gov.gg census reports as far back as 1971 for data.gg . The earlier census reports are scanned and the later ones have been created electronically but don’t copy and paste correctly. By the end of manually copying the data from the first table I knew I’d have to find a better way. After a bit of searching I found Tesseract , an OCR (Optical Character Recognition) tool. OCR tools take an image and attempt to convert any text it finds in the image into usable text. Google has even had a hand in Tesseract.

Tesseract is extremely easy to use. All you need to do is give it the image and an output file.

t e s s e r a c t m y s c a n . p n g o u t

There are plenty of other options available to optimise for your particular situation and even multiple languages supported. For me Tesseract worked perfectly except for the occasional 3 and 8 getting mixed up. Here is an example I created earlier…

Input

Image

Output

H T e h l e l r o e

Gamely Digest and Jekyll

Jan 21 2015

We started Gamely Digest in 2013 to make a more digestible way to follow video games. The older I get the less time I have to play games and deciding what is worth playing is time consuming in itself.

When I was younger I noticed something similar with hardware. Once I took up software development I had less and less time to keep up with hardware components. Nowadays, I still build computers from components but do research at the time of purchase. I am extremely grateful for hardware sites that do guides on what to buy, they have save me so much time and effort. I want to do something similar for video games.

Before I’d even written a line of code, I knew Gamely Digest needed to be simple to maintain, static (no databases) and fast. Having used Jekyll previously, I used that. Jekyll is a static site generator. Instead of using a database, you create content using Markdown and Liquid templates. Jekyll generates the site into a folder, ready to be uploaded straight to your web server.

For example, if I was to use HTML to generate the reviews page, it would look something like this:

< P < P < P < P h o h o h o h o 1 s 1 s 1 s 1 s > t > t > t > t P P P P o # o # o # o # s 1 s 2 s 3 s 4 t t t t t t t t # e # e # e # e 1 x 2 x 3 x 4 x < t < t < t < t / , / , / , / , h h h h 1 b 1 b 1 b 1 b > l > l > l > l a a a a h h h h , , , , b b b b l l l l a a a a h h h h , , , , e e e e t t t t c c c c . . . .

Using Jekyll, I just write this:

{ < { { % h { % 1 r > p e a { o n w { s d t f % p . o } o s r { s u % t m % . m } f t a { o i r % r t y l e p e } n o } d s } r t } a < w i / n h % 1 } s > i t e . p o s t s % }

Behind the scenes, I have a folder called ‘_posts’ and Jekyll knows to find the posts for the loop in there. Each post has things associated with it such as title, date, layout, author, etc. Once the site has been built using Jekyll, you get the same HTML as above.

Reviews have improved a great deal since the release. Initially I had some of the layout, repeated inline image code and adsense code in there. This was becoming too time consuming to maintain. Now, I’ve extracted the adsense and inline image code into the Jekyll ‘_includes’ folder. Includes can also have parameters, allowing me to pass the filenames of inline images.

{ % i n c l u d e i m a g e _ i n l i n e . h t m l i m g 1 = f i r s t _ i m a g e i m g 2 = s e c o n d _ i m a g e % }

The difficultly was the sidebar images, I needed to be able to pick the images that go inline in the review and the order they appear in the sidebar. In the end I have come up with a quite elegant solution to minimize repetition. Integers are the naming convention for images on Gamely Digest. Reviews only ever use JPG images so the extension is assumed.

_posts/2015-01-01-my-review.html:

l s i < { P { P a i m p % a % a y d g > r r o e - P i a i a u b l a n g n g t a o r c r c r : r o a l a l a - k g u p u p r i u r d h d h e m p a e e v a : p 2 3 i g h i a e e " m d w - 1 a s _ d < g e l r i / e n a d r p _ s y e / > i e o r t n . u : o l h t / i t [ r n m 3 e e l , v . 4 i h % , e t } 5 w m , / l 6 i , m i 7 a m ] g g e 1 s = " 1 i m g 2 = 2 % }

In the front matter I am creating an array containing the names of the images in the order they should appear in the sidebar. Front matter is an the area between the two triple-dashed lines in which you can set predefined variables or create and set custom variables. I use this area for things like twitter slugs, authors, excerpts, dates, titles, etc. ‘layout’ is a predefined variable telling Jekyll what layout file to use for the page.

_layouts/review_layout.html:

< d / i < < d v d / d / i i { d i { d v c v % i v { i > l < v v a c i e > c c > s l m n l o s a g d a n = s f s t " s s o s e r = r r = n o " c " t w c = % c " o " } o } > l { l } - { - 4 8 " p " > a > { g % e . f i o m r g - i l t o e o m k u i p n } p } a { g { e . i s t i e d m e b } a } r . - j i p m g a " g e - o r d e r % }

The review layout is used by all reviews and uses the ‘sidebar-image-order’ variable to loop and select the images for the sidebar.

Using this system I can copy and paste the reviews from our shared storage directly into the post file leaving me to set the sidebar image order, place an adsense advert in an appropriate place and place inline images in the article.

Sometimes you do still need a databases but for simple things like personal sites, blogs, business sites and product homepages Jekyll is an excellent choice. Once upon a time you would use WordPress for your blog because of its facility to comment on posts but with products like Disqus and Discourse , you don’t always have to use WordPress. Of course Jekyll does have some downsides, it requires Ruby to be installed and for non-technical users, it is not as simple as logging in and using a What You See Is What You Get (WYSIWYG) editor to create content.

Jan 1 0001

A while ago now, Digimap gave data.gg a SHP file containing elevation data for Guernsey, Sark, Alderney and Herm. Using this data, I’m going to place a WebGL widget on data.gg allowing users to fly around the islands. There’s two ways to approach this:

  • Create a height-map using the SHP data and generate game engine terrain using it. This would be done using geographic software to rasterize the data into a normal image (like a PNG) with white pixels representing high points and black pixels low (e.g. sea level).
  • Triangulate the XYZ points in the SHP file to create 3D meshes. This is done using an algorithm to join all the points together to form edges and faces.

This post is going to be about the latter method which is to create a mesh. I’ll do a separate post about the other method in the future. Both approaches have different pros and cons—the mesh approach may be more detailed, while game engines (which is ultimately what the WebGL widget will be) will handle terrain better performance-wise.

To create the mesh I’m going to be using Blender , an awesome open-source 3D graphics editor. You’ll also want to grab an add-on called BlenderGIS , which is a “bridge between Blender and geographic data.”

Import

Using the menu, import your SHP file. Note that the ability to import SHP files comes from the BlenderGIS add-on.

Image

These options will appear. The field holding the elevation in my case is called ZVALUE.

Image

Point Cloud

Depending on your data size, this could take a while, but eventually the add-on will do its job and create vertices for all the XYZ coordinates.

Image

What you have here is known as a point cloud. There are loads of tools and algorithms to triangulate point clouds into meshes, but BlenderGIS comes with one called ‘Delaunay,’ which worked perfectly for me. It’s in the tools area (press T) under the ‘GIS’ tab.

Image

Mesh

Again—this could take a while depending on your data size, but eventually you’ll be presented with a mesh containing edges and faces. Due to my data set containing rock formations around the islands, it has created some planes joining the rocks to the island and other rocks. Select one of the planes and press Ctrl-Shift-Alt-F to select all linked flat faces as in the screenshot below. Use X to delete these faces.

Image

There we have it. One mesh.

Image

I have a large data set, so my mesh has 433,775 faces, which is too many to be usable. There are a couple of ways to simplify a mesh, but I found using the ‘Decimate’ modifier with a collapse ratio of 0.03 produced good results. Below I’ve reduced the mesh to 5,987 faces.

Image