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

@brentmc79

@brentmc79

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.

#

UndefinedConversionError can suck it

Posted on 09/04/2014

I have a bit of code in our app that downloads binary files and writes them to Tempfiles. Some testing led to the following exception:

An Encoding::UndefinedConversionError occurred in analyses#create:

  "\x90" from ASCII-8BIT to UTF-8
  app/models/analysis/malware.rb:193:in `get_file'

Line 193 looked basically like this:

tempfile = Tempfile.new('tmp')
begin
  tempfile.write(uploader.file.read)
ensure
  tempfile.close
end

I actually didn't want it converted to UTF-8, so a quick perusal of the Tempfile docs told me that I just needed to specify the encoding when I initialized the Tempfile.

So I changed the first line to:

tempfile = Tempfile.new('tmp', :encoding => 'ascii-8bit')

And everything was kosher.

#

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>

Awesome.

#

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.