Photo-1 Photo-2 Photo-3 Photo-4 Photo-5 Photo-6



Full-time web developer. Part-time smart ass.

I'm Brent Collier.

After a year and a half as an engineer on Twitter's Trust & Safety team, I'm looking for my next gig. Contact me if you know of something interesting.


Formatting JSON in Vim

Posted on 01/21/2014

Typically when I'm dealing with JSON data, it's in the browser and I can use the handy JsonView Chrome extension, but in this particular instance it was a text file containing a string of json.

A quick google search led me to this blog post.

To format a file as json, run this command in Vim:

:%!python -m json.tool

Or, add this to your .vimrc, then select the bit of json and hit <mapleader>j.

map <Leader>j !python -m json.tool<CR>



Rails 3 asset compilation problem

Posted on 12/30/2013

Is your Rails 3.2 app showing errors like this in the log file?

ActionController::RoutingError (No route matches [GET] "/assets/bootstrap-responsive.css"):
  actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.8) lib/rails/engine.rb:479:in `call'
  railties (3.2.8) lib/rails/application.rb:223:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /Users/brent/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /Users/brent/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /Users/brent/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

Or like this in the browser console:

$ is not defined

If you've got all the necessary manifest files included in your layout, and those manifest files all have the correct import statements, then you may just need to update your Rails gem version.

My 3.2.8 Rails app was showing these symptoms and a quick bump to 3.2.14 in the Gemfile fixed everything.


Net::HTTP "End of file reached"

Posted on 03/25/2013

While writing some code to consume Twitter's v1.1 api, I was continually running into this error:

EOFError: end of file reached

I began adding and removing bits of code around the http request, until I eventually discovered it was the gzip encoding that was causing the problem. Removing this header declaration seemed to fix everything up:

request["Accept-Encoding"] = "gzip"


I ran into the same issue again, this time with a different solution. I was including an HMAC signature in the header, but wasn't encoding it properly.

I changed it from

request['X-Hmac-signature"] = generate_signature


request['X-Hmac-signature'] = URI.encode(generate_signature)

and the EOFerror went away.


Configuring Redis Store for Rails 2.3.x

Posted on 05/23/2012

I wanted to use Redis as the cache store for a Rails 2.3.8 app. After a little shopping around, I settled on RedisStore as the cache adapter. I following the configuration directions on the RedisStore website and restarted the app, expecting everything to be hunky-dory. Instead, I was kicked in the face with this:

uninitialized constant ActiveSupport::Cache::RedisStore

My redis-store configuration looked like this:

redis_config = YAML.load_file(RAILS_ROOT + '/config/redis.yml')[RAILS_ENV]
config.cache_store = :redis_store, redis_config

I took another look at the config instructions and realized I was using the Rails 3.x setup. Following the 2.x instructions, I updated the configuration to look like this:

redis_config = YAML.load_file(RAILS_ROOT + '/config/redis.yml')[RAILS_ENV]
config.gem 'redis-store'
config.cache_store = :redis_store, redis_config

After restarting the app, I was still seeing the same uninitialized constant error. Finally, a bit more googling revealed that v1.1.0 of redis-store didn't jive with Rails 2.3.8. Evidently what I needed was v1.0.0.1. I'm not sure why it's v1.0.0.1 and not v1.0.1, but I checked and saw that v1.0.0 was yanked for some reason.

alt text

Anyway, so I updated my Gemfile to specify version v1.0.0.1 of redis-store and bam! The app starts up without yelling at me. Good stuff. Or so I thought.

Everything was hunky-dory in my local development environment, but when I started testing in our staging environment, something was up. Our staging environment consists of two app servers, one of which has the Redis instance running on it.

Attempts to connect to Redis repeated failed with an "unable to connect" error on one web server, but not the other. Examining the redis connection, it was clear that the app was trying to access Redis via localhost, the default when no valid connection options were supplied. This explained why it worked on one app server and not the other, since Redis was in fact running locally on one of them.

A little console debugging demonstrated that the redis config expected a hash with symbol keys, whereas the config hash loaded from the yaml file had string keys.

config.cache_store = :redis_store, redis_config.symbolize_keys

Updating the redis-store config to symbolize the keys allowed the connection to be properly created, and everything was working as intended.