Posterous theme by Cory Watilo

Heroku Rails App Detection

Recently while deploying an app to Heroku I noticed that the Sinatra app I was deploying was somehow being detected as a Rails app. Whenever I ran heroku run console. I would get the following error:

Running console attached to terminal... up, run.1
bundler: command not found: script/console
Install missing gem executables with `bundle install`

For some reason Heroku was looking for the Rails console. I thought about how one might detect the differences between these two types of apps and what the heuristics of a Rails app may be. Then I realized I had used a Rails convention in my app. I used config/environment.rb as a way to manage multiple envs for my app. I quickly changed this files name to config/env.rb and re-deployed. This immediately fixed the problem and Heroku now detects the Ruby/Rack application.

Lesson learned.

Vim, Janus and Solarized on Ubuntu done right

MacVim, Janus and Solarized look great but how do you bring that to your SSH sessions?

alt vim

First install Vim with ruby support.

# install Vim with ruby support
sudo apt-get install vim-nox

Then let Janus take over…

# install Janus
curl https://raw.github.com/carlhuda/janus/master/bootstrap.sh -o - | sh

Solarized is packaged with Janus but the color settings are all wrong. Here’s how to get it right:

" ~/.vimrc.local
let g:solarized_termcolors=256
set t_Co=16
set background=dark
colorscheme solarized

Install it all:

# install Vim with ruby support
sudo apt-get install vim-nox

# install Janus
curl https://raw.github.com/carlhuda/janus/master/bootstrap.sh -o - |sh

# setup the solarized theme
curl https://raw.github.com/posterous/vim/master/vimrc.local > ~/.vimrc.local

Validating JSON with RSpec and pathy

While building out our API I have at times needed to validate the JSON that we are returning from endpoints or as object representations in the to_json method of our models. Typically I would just parse the returned JSON string and test the resulting value like so.

# contrived rspec but you get the idea
before do
  @post         = Post.new(:title => "Yup, I'm a post")
  @parsed_json  = JSON.parse(@post.to_json)
end

it "should include :title in it's JSON representation" do
  @parsed_json['title'].should == @post.title
end

That works fine and all but once you start validating a model this becomes terribly repetitive and a chore to maintain. After adding a quick patch to Object and a bit of yack shaving, I decided to give it gem cred and publish it.

Enter Pathy

Pathy adds a few convenience methods that make validating the JSON version of an object stupid easy.

Example

require 'pathy'

Object.pathy! # add the methods to all Objects

@json = %[ 
  {
    "string"  : "barr",
    "number"  : 1,
    "array"   : [1,2,3],
    "hash"    : {"one":{"two" : 2}}
  }
]

@obj = JSON.parse(@json)

puts @obj.at_json_path("number")
=> 1
puts @obj.at_json_path("array.1")
=> 2
puts @obj.at_json_path("hash.one.two")
=> 2

Rspec Matcher

it "should work as rspec matcher" do
  @obj.should have_json_path "hash.one"
end

Installation

gem install pathy

Source

https://github.com/twoism/pathy