Ryan Alyn Porter

Web and mobile software developer

Need an FTP Server? Here’s How to Get There With Vagrant, EC2 and Vsftp

An example of using Vagrant’s vagrant-aws plugin to launch an FTP server on EC2 using vsftp.

When I was building Vault, the data warehouse for the Hakkasan Group, I had to accommodate data sources that post data files to an FTP server. I didn’t want to compromise the security of the data warehouse by running an FTP daemon on an existing server in Vault’s secure production environment, so I spun up a dedicated FTP server outside of that environment in the same Amazon EC2 data center.

No self-respecting DevOps practitioner would set up a server like that manually. I used Chef to configure the cloud instance. But I also wanted to automate creating the instance, not just configuring it. One way to do that is with the Knife tool in the Chef suite of tools. Knife is powerful, but using it is not simple, especially without a Chef server. I wanted to automate as much of the process as possible, so that ideally creating the server in the cloud is as simple as pressing a button and then sitting back and watching it happen.

To accomplish that, I turned to Vagrant, a tool originally intended for creating development environments. Now it’s capable of a lot more. Vagrant’s multi- provider technology makes it easy to use the vagrant-aws plugin to create cloud instances on Amazon EC2, or vagrant-rackspace to use Rackspace, or vagrant-google for the Google cloud, or others, in addition to local development environments.

Super-minimal, pure-Javascript Ad Banner Rotator

A simple system for displaying weighted random ad banners, for when you don’t really care about impression or click tracking.

I still maintain a web forum that I set up over five years ago for the Miami nightlife community, but I can’t afford to spend a lot of time dealing with it. Recently, the PHP-based ad management package that I used (and never had time to update) was used as an attack vector, and so it had to be removed. I needed a replacement but I just didn’t have the time to deal with setting up a new ad package, and I don’t really want to deal with any PHP software packages other than the forum software itself, phpBB 3. I don’t want the liability of needing to stay current with security updates for yet another piece of mission-critical software.

Then I realized: I don’t care about tracking the impressions or clicks. The site is fairly popular in its niche, and sometimes the ad space is worth money. But in nightlife, generally advertisers only care about the length of the ad campaign (a week or two) and nothing else. Nightlife advertisers are generally not sophisticated enough to even care about clicks or impression counts or ROI.

So why not just do it all in Javascript? All of the work could be done by the client’s browser, and there would be no possibility of a security breach through an outdated ad management system. And it would work with PHP or Rails or even a static HTML site. And so the Super Minimal Ads project was born. About an hour later, I was done.

Introducing the Link-checker Ruby Gem

Quickly and painlessly check your web links with Ruby, Nokogiri and Anemone

When I started using Octopress to build web sites, I was a little surprised to find that there aren’t a lot of pure Ruby tools for checking links. I want to check the links in new pages before I publish them, and I want to be able to check the links on the entire site so that I can fix dead links as time goes on. And I want to do it with Ruby on my machine, not against a live server with a Mac app or a Windows app or something. That’s just the Octopress way.

It didn’t take long to make a Ruby script to find the HTML files in the ‘public’ folder of my Octopress site, find the external links in each file using Nokogiri, and check each link. I set it up to display passing results in green, errors from bad links in red, and warnings about redirects in yellow. I set it up to spawn a thread for each file and check them simultaneously, instead of (slowly) checking each HTML file in sequence. That’s pretty much all that I needed for my Octopress sites.

But this seems like something that other people might want to do, so I took the extra time to make the tool more generalized so that it can be used with any web site generated by Jekyll or any other kind of static HTML generator, not just Octopress.

And maybe you might want to check the links on a live web server, not static HTML files on your development machine. So I added a web crawling feature using Anemone so that you can target the URL of a live site instead of a file path. I want these features to be solid and reliable over time, so I built all of it with RSpec testing and confirmed 100% test coverage using SimpleCov. And you might not agree with my idea of sensible defaults, so I added a couple of command-line parameters for controlling warnings and errors. The link-checker gem is pure Ruby and you can get it from RubyGems. The code is available on GitHub.

Simple Redis Caching in Ruby

A utility method for caching the result of any block of Ruby code.

Redis is a simple and very fast key-value store, that can be used for all kinds of things. Resque, for example, is a system built on Redis for processing background jobs or even scheduled jobs. Redis can be used for all kinds of different things, and so it has a very generalized API that doesn’t make any assumptions about how you’re going to use it. The Redis API includes simple methods like get and set and expire. And the Ruby gem for Redis is a thin layer over the standard Redis API.

Caching expensive operations with Redis

But when most people use Redis, they tend to use it for caching values in a web application, like you would use memcached. And if you use Redis for caching, then you might find yourself writing the same sort of code structure over and over:

1
2
3
4
5
6
7
8
9
10
11
12
if (something = redis.get(redis_key = "the cache key")).nil?
  redis.set(
    redis_key,
    (something =
      # Some really slow calculation or data access happens here.
    )
  )
  redis.expire redis_key, 300 # seconds = 5 minutes.
end

# Use the "something" value that was calculated (or cached) above.
use(something)

It’s great that Redis#get and Redis#set and Redis#expire are all so simple. But if you’re going to wrap expensive operations in Redis caching frequently, then what you really need is a Redis#cache method.

Resizing Thumbnails on Demand With Paperclip and Rails

How to generate Paperclip attachment thumbnails on demand instead of in advance.

If you have a Rails app that supports image uploads, then you probably use Paperclip. Paperclip codifies an assumption about attachment handling: that you know in advance all of the thumbnail sizes that your web site will need for your image attachments, and that you want to generate those thumbnails when attachments are uploaded.

But what if you need to be able to display thumbnails at any size, specified by request parameters? What if you want to upload image attachments without generating thumbnails, so that you can dynamically resize the images just-in-time (JIT) at any size specified by a user-editable view template?

Resizing thumbnails just in time

Fortunately, Paperclip is flexible enough to handle that kind of scenario. This article demonstrates how to set up dynamic, just-in-time image resizing for Paperclip attachments in this example Rails 3.2.5 app.

Rapid Web Site Creation and Deployment With Appleseed

Appleseed

Appleseed provides a generator for creating a new web site project with Rails 3, adding things like HAML and Compass to the new project, creating a Git repository for the new project, pushing that repository to GitHub, and then deploying the site to Heroku. All with one command.

This utility is designed to assist graphic designers in rapidly creating new web sites, for high-volume web shops. A graphic designer can create a site, push the code to GitHub, and deploy the site to Heroku without the assistance of a developer. Then a developer can easily join the project to build out the back-end and assist the graphic designer with the front-end.

Authorizing Users Between Web Sites With Open Sesame

Use a cryptographic hash to pass authenticated traffic between two web sites.

When I was building the Tiesto.com web site and Tiesto’s fan club web site, InTheBooth.com, I needed a way to send authorized traffic from the members-only InTheBooth.com web site to ticket sale pages at Venue Driver (ticketdriver.com) to members-only pre-sale ticket-sale pages that the general public could not access. But I needed the Tiesto.com web site to be completely independent from the VenueDriver.com web site. I needed some way to authorize users on the VenueDriver.com web site from the InTheBooth.com web site, even though the two sites needed to run in different cloud environments, with different databases.

Open Sesame was the solution that I came up with. It generates an authorization token by packaging a time stamp with a cryptographic hash of that time stamp plus a secret phrase. The receiving end can take the time stamp and the secret that it also knows about, and generate a cryptographic hash of its own. The it can compare that hash to the hash included in the token. If the two are the same, then the token is verified.

I also needed a way to pass parameters from one site to another, and so I added an OpenSesame::Message class, for protecting the integrity of a string without encrypting it.

Generating Rails ERB Templates From Dreamweaver With Ginsu

Bring graphic designers into your agile process by generating Rails templates automatially.

Rails applications are not always born as Rails applications. Sometimes graphic designers create web designs using tools like Dreamweaver and then pass them off to software developers for implementation as web applications

But Rails has a different model for a web site than a graphic designer’s authoring tool like BBEdit. Rails thinks in terms of routes that lead to actions that render using templates that can use layouts, but web authoring tools like Dreamweaver and Rapidweaver think in terms of pages. Every page includes the whole layout.

Ginsu plugs your graphic designers into the agile software development process, by automating the creation of ERB templates and Rails layout files from HTML files. Ginsu can create hybrid web sites with some sections served as static content and some sections powered by dynamic Rails actions, or you can convert every page into an action, and every Dreamweaver layout into a Rails layout. It’s not specific to Dreamweaver because it slices out content based on HTML CSS selectors, so you can use it with any HTML authoring tool.