Archive for the ‘Ruby and Rails’ Category
RSpec Request Spec to Test Rails / Grape API Functionality
I finally got around to trying Grape – a “RESTful API microframework built to easily and quickly produce APIs for Ruby-based web applications”. This is a project still in baby stages, but has a lot of potential and worth exploring for anyone creating a Rack-based API in Ruby, not necessarily Rails!
Now, after creating a pretty basic API that used HTTP Basic Authentication, I was inclined to write some RSpec tests to make sure my API was working the way I thought it was (.. or because I am obsessed with well-tested, beautiful code..).
After some thought, I decided that the best way to test my API was with RSpec “request” specs. Now, if you are at all relatively new to RSpec (I was a Test::Unit kinda guy before), it might not be completely obvious that “request specs” are basically what I have come to know as “integration tests”, testing high-level functionality that spans multiple controllers and multiple requests – (think: a user’s interaction with the app).
My reasoning for choosing request specs is because I want to test specific API URL endpoints routed the way I expected. (Routing is handled magically by Grape with a simple mount in the config/routes.rb file). API testing just kinda makes sense to handle in request specs.
Anyways, I ran into a couple issues because in REQUEST specs, you do not have access to the @request object (haha?), as you do in controller specs. Now, in order to mock HTTP Basic Authentication, you need to mock the request object to send headers along with the GET request.
Well, solution: It turns out you can pass headers into your get() method! I only wish I had discovered that an hour ago!
Here’s a simple excerpt from my API request specs that shows how to mock the HTTP basic authentication and test your API functionality:
With NO basic auth, it’s just a simple GET request
it 'should return a 401 with no basic auth to /api/v1/rewards' do
get '/api/v1/rewards'
response.code.should == '401'
response.body.should == "Unauthorized - Please check your username and password"
end
To mock the basic auth, simply pass header hash as argument to the GET request! No need to access the request object here.
it 'should return a 200 with valid basic auth to /api/v1/rewards' do
# Uses basic_auth helper method
credentials = basic_auth('testuser','test')
get '/api/v1/rewards', nil, {'HTTP_AUTHORIZATION' => credentials }
response.code.should == '200'
response.body.should == "..."
end
# You can define this at the bottom of your spec file, or in spec_helper for convenience
def basic_auth(user, password)
ActionController::HttpAuthentication::Basic.encode_credentials user, password
end
Hope this helps someone else. Now go write some request specs!
Ruby Multi-level Nested Hash Value
Often in my Ruby code or Rails application, I will need to find a value in a nested hash. Frequently this also comes in handy when dealing with JSON and parsing JSON to a hash. For example, I might have a hash of user information that looks like this:
user_hash = {:id => 1, :name => 'John doe', :extra => {:birthday => {:month => 11, :day => 16, :year => 1951}}}
Now, when I want to find the birthday year, I have to do something messy like this:
year = user_hash[:extra] && user_hash[:extra][:birthday] && user_hash[:extra][:birthday][:year]
How inconvenient is this?! Every level of the hash I am checking for existence of the hash-key. Here’s a helper method that I use so that I can avoid these verbose statements and get the value I want in 1 line. It adds a ‘hash_val’ method to any hash, and takes in the hash-keys as arguments. If one of the nested hash keys is missing, it will simply return nil.
# I usually define this in an initializer, so it can be used all over my app:
# Eg. Place in config/initializers/hash_val.rb
class Hash
# Fetch a nested hash value
def hash_val(*attrs)
attr_count = attrs.size
current_val = self
for i in 0..(attr_count-1)
attr_name = attrs[i]
return current_val[attr_name] if i == (attr_count-1)
return nil if current_val[attr_name].nil?
current_val = current_val[attr_name]
end
return nil
end
end
Now, getting a nested hash value is so easy!
user_hash.hash_val(:extra, :birthday, :year)
=> 1951
And, if the hash-key does not exist, it simply returns nil:
user_hash.hash_val(:extra, :trouble)
=> nil
Rails 3 – How to Rename a Project
As opposed to previous versions of Rails, Rails 3 namespaces your entire project according to your project name. As an example, notice that in your config/routes.rb file, the first line is:
ProjectName::Application.routes.draw do
...
This means that changing a project name involves changing a number of files to reference the new project name as well. Here’s a quick list of the standard files to change:
Rakefile
config.ru
config/application.rb
config/database.yml
config/environment.rb
config/environments/*.rb
config/initializers/secret_token.rb
config/initializers/session_store.rb
config/routes.rb
Besides these, it’s a good idea to also check all files in config/ and config/initializers/
If you want to be thorough, run this grep command in your project root, and you will get a list of all files that contain your old project name:
grep -Ri 'oldprojectame' * | cut -f1 -d':' | sort | uniq
Facebook base64 url decode for signed_request
I ran into a problem as I was trying to decode and parse the Facebook signed_request for their new Registration plugin (http://developers.facebook.com/docs/plugins/registration).
Folowing the PHP example, I attempted to decode and read the signed_request returned by Facebook. Unfortunately, it seemed like the decoded JSON returned was malformed! It was missing the end hash character “}”. This may not happen in all cases, but the reason is due to the padding in Base64 encoding (See Base64 for URLs in Wikipedia).
To account for the padding in Base64, I used the following helper method to do the base64_url_decode. Hope it helps someone else trying to base64 decode Facebook’s signed_request in Ruby on Rails!:
def base64_url_decode(str)
str += '=' * (4 - str.length.modulo(4))
Base64.decode64(str.tr('-_','+/'))
end
Notice there’s two things that must happen before decoding the string:
- Pad the encoded string with “=”
- Replace the character ‘-’ with ‘+’, and ‘_’ with ‘/’
I wish Facebook mentioned this clearly on their API !
Setup a Git Repository for Redmine
Just installed Redmine for our project management / code tracking with Git. I must say it’s pretty nice being able to setup multiple projects easily (unlike Trac). The system is designed fairly well with a very user-friendly interface. And plus, it’s Rails !!
Anyways… too much on Redmine! This post is to describe how to setup a Git repository for use with Redmine.
The important thing to note is that the Git repository MUST BE on the same server as your Redmine setup. So, you probably need to have access to that server!
This was not ideal for me because our Git server does not have Ruby/Rails, etc. installed on it. That’s okay! You just need to clone the repository as a BARE repository on the same machine that your Redmine app is running on. Let’s see how:
Pick a place on your Redmine app server to house all your bare Git repos.
I will choose “/var/local/git_copies” for my example.
Note: Make sure that your permissions allow for your web-user to access these Git repos. My web-user is ‘build’.
# Change to build user (see above)
$ su - build
# Create the directory
$ mkdir /var/local/git_copies
Now, create your git clone as a bare repository clone.
Note: a bare repository will not have your actual files. It will just contain the standard git folders
# Goto your git_copies directory
$ cd /var/local/git_copies
# Make a bare clone of the repo
$ git clone --bare ssh://git@reposerver/usr/local/git_root/foo-project.git
Change into your project and configure remote branch tracking for your local copy.
$ cd foo-project.git
$ git remote add origin ssh://git@reposerver/usr/local/git_root/foo-project.git
Now, normally you want to sync up the repo, but you cannot do a normal git fetch && git merge into a bare repo.
Instead, do a fetch and reset the HEAD to point to the remote branch commit. You need the ‘–soft’ flag or else you will see errors!
$ git fetch origin
$ git reset --soft refs/remotes/origin/master
Now, remember you need to manually sync your new Git repo to have the changes appear on Redmine. A better idea would be to create a cronjob that does this automatically. Especially with more than 1 repository, automating this process will save much time. Here’s the basic idea:
# Add the following to your crontab
*/30 * * * * cd /var/local/git_copies/foo-project.git && git fetch origin && git reset --soft refs/remotes/origin/master > /dev/null
Instead of doing many times in your crontab, maybe it would be easier to setup a bash script to run:
#!/bin/bash
# Not tested! Use at your own risk, and change your GIT_ROOT and "*.git" to fit your setup.
GIT_ROOT=/var/local/git_copies
cd $GIT_ROOT
ls *.git | while read repo; do
cd $repo && git fetch origin && git reset --soft refs/remotes/origin/master > /dev/null && cd $GIT_ROOT
done
Then, just add this one script to your crontab and have it run every N minutes as you desire!
This site is helpful and worth checking out as well:
Reference on synchronizing 2 git repositories
That’s it, let me know how it goes!
