Binary Code

A whole lot of nought thought by Chris Cummer

Hominid, Mailchimp API and NULL Characters

Recently we saw a bunch of our DelayedJob tasks fail with

undefined method 'list_subscribe' for Hominid::API

which really made no sense, the whole purpose of Hominid and the Mailchimp API is to subscribe people to mailing lists. How could that method be missing?

Much digging and testing later, it turns out some of the data fields we were sending as part of our merge vars contained encoded NULL characters, aka \u0000 and \x00. Those were tripping up the Mailchimp API which in my opinion is reporting back the wrong error code to Hominid.

In our case these submissions all appeared to be as part of a scanner filling form submissions in automatically, basically spam.

Unique Is Not Random

Just because it won’t occurr again doesn’t mean it’s random, as the folks over at Meldium share in Be careful with your random tokens. It’s definitely a worthwhile read as a cautionary tale. The ultimate take-away: if you need a random GUID, generate it yourself and use SecureRandom.hex.

Or, MetaSearch and Ransack

A little warning: if you’re using MetaSearch or Ransack in your Rails app, don’t have _or_ in the name of any of your table columns.

Their respective OR search functions concatenate field names via _or_ and then split on that, so if your field has an _or_ in it, it’ll split into two distict, and broken, queries.

Remarkably Handy Shell Script

For operating over sets of files and doing something to them. Basic, sure, but ridiculously handy.

1
2
3
for file in *.html do
  mv "$file" "${file/html/md}"
done

This Is the Obligatory First Post

Figured I’d give Jekyll a go and move Binary Code away from WordPress.

While most of it was pretty straight-forward, some of it was not. Particularly, importing from a WordPress XML export file is brutal (no surprise it looks like as of two weeks ago all import scripts have been removed from Jekyll proper and into their own gem).

First, hpricot is required but it’s not a dependeny of Jekyll so I had to add it to Jekyll’s Gemfile, then bundle install, then import.

Second, all the WordPress posts were imported as HTML, not Markdown, which blew out their formatting. Simply renaming all the files to use .md instead of .html seems to have done the trick.

Finally, rather than put them into /source/_posts where Jekyll could find them for deployment, the import script put them into /_posts. Moving them fixed that problem.

There’s still a long way to go before it’s all completely migrated, particularly setting up the 301 redirects and pointing the domain name, but overall the idea of hosting on Github and writing in vim might just make it all worth it.

Rspec, DRb::DRbConnError and Connection Refused

My Rspec specs suddenly stopped working with the following error:

1
2
Failure/Error: Unable to find matching line from backtrace
DRb::DRbConnError: druby://127.0.0.1:8989 - #<Errno::ECONNREFUSED: Connection refused - connect(2)>

Oddly, the first two tests were working, then the rest exploded. Turns out the culprit was raising an exception in the initialize() method of the class being tested, like so:

1
2
3
4
def initialize( account )
  raise if !account.kind_of?( UserAccount )
  ...
end

The first two tests tested the exception state, like so:

1
lambda { AccountDecorator.new( not_an_account ) }.should raise_exception

and so passed. But due to a refactoring, the rest were not properly instantiating the test class.

Bootstrap, Rails and Small Input Fields

Should you be using Rails and Bootstrap together and find that your input fields (text, email, password, etc.) are very small, with a fixed height of 18px, make sure you have an HTML doctype at the top of your HTML pages:

1
&lt;!DOCTYPE html&gt;

For HAML, just add this to the top of your layout:

1
!!!

Ruby: Invalid Multibyte Char Error and Rspec

When testing in rspec and the dreaded “invalid multibyte char (US-ASCII)” error shows up, it’s probably because the test file doesn’t have

1
# encoding: UTF-8

at the top of it.

Rails and “Incompatible Marshal File Format” in Development

If you suddenly start getting an “incompatible marshal file format” error when trying to insert into your database, check to see if you’re using acts_as_indexed.

If you are, it’s quite likely that your index has been corrupted. The fix for me was to go nuclear on the tmp/index directory. Deleting the entire thing worked like a charm. I supposed I could have just removed the subdirectory pertinent to the model in question, but what fun that?

Refinery CMS and Testing Custom Controllers

I thoroughly enjoy working with Refinery. As the foundation of a new Rails app, it takes care of so much of the drudgery that most Rails apps needs. Can’t recommend it enough.

Recently I ran into an issue with testing custom controllers that don’t belong to any engine. When running the specs, Refinery would see that there was no superuser and redirect everything to it’s superuser creation process, effectively causing all controller specs to fail (no routes would return as expected, as everything was redirecting).

I brought it up in this thread, where Pete Higgins ultimately provided the solution I went with. I think it’s fairly graceful:

This is what I have in a file in spec/suport in a project using a 
recent-ish version of edge refinery:
1
2
3
4
5
6
7
8
9
module Refinery 
 
  module ApplicationController 
   
    module InstanceMethods 
     
      def just_installed? 
       
        false 
     
      end 
   
    end 
 
  end
end

Et voila. Since the spec_helper file in Refinery loads everything in spec/support by default, this over-rides Refinery’s default check.