Rails 3 / Cucumber error | undefined method `parameters' for #<Mail::UnstructuredField:0x2eceecc>

I was banging my head against the wall when trying to test the sending of an email in a Rails 3 app with Sinatra / Email Spec and getting the following error message:

    undefined method `parameters' for #<Mail::UnstructuredField:0x2eceecc>

I was using the latest versions of all the gems in my GemFile and the error did not occur outside of Cucumber. I finally tracked the issue down to the fact that the command line cucumber in system gems was an old version. This was causing the issue even though the latest cucumber was declared in my GemFile.

I couldn't find anything through Google for the error message so though I would post here incase anyone else runs into the same problem.

Posted by Matt Thu, 25 Mar 2010 14:06:00 GMT

Playing with Node.js

Today I decided to have a look at Node.js. I have heard quite a lot about it but have avoided looking at it for a while. Node.js provides a way of building server side network programs, written in Javascript. I took a look at a tutorial over at http://simonwillison.net/2009/Nov/23/node/ for installing Node and building a simple Hello World web service. I ran an apache benchmark test and was suprised with the results. I managed to get 6075 requests per second on my Macbook Pro!

To compare it with something that I'm used to, I wrote the same Hello World script service Sinatra and served it up using the thin webserver, which gave me 1606 requests per second which is quite a difference.

The 2 scripts and apache benchmark outputs are below:

Posted by Matt Fri, 19 Feb 2010 22:47:00 GMT

Installing Prey on snow leapord

Today I decided to install Prey on my mac book. Prey is basically a bash script that runs from a cron job every 10 minutes or so. It contacts a remote server to ask if the computer is stolen or not and if so can send a screenshot of the desktop and also a photo from the webcam as well as providing the IP address of the thief.

Prey does have the ability to have the data sent to their website but I decided to handle this myself and have the script email the data if a webpage I have set up on my domain gets deleted. I ran into a few problems getting the script to email me, it uses a perl script called sendEmail. I banged my head against a wall getting errors from Google like “Username and Password not accepted.” even though everything was correct. The solution was do download the latest version of SendEmail from http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz, extract and replace the file called sendEmail in /usr/share/prey/lib/ with the new one.

Posted by Matt Thu, 05 Nov 2009 21:48:00 GMT

Use a PHPBB forum to handle user authentication in your rails app

Im currently in the process of rebuilding www.realalehunter.co.uk in rails. Its currently uses PHPBB to handle logging in and authentication so to make the rebuild simpler I thought keep it working in the same way instead of trying to migrate all the users/passwords to a separate system.

So I wrote a rails gem / plugin to make this a bit easier. It currently works on Rails > 2.3.3. I decided that I still wanted to have a users table in my applications database to make relationships easier but I would just store the stuff that’s important to the app itself (not passwords etc). So if you visit the rails site and are logged in to the forum a user will also be created in your rails app database if one does not exist.

So here is how to get started:

# install the gem
gem install mattfawcett-phpbb-auth

Tell rails that you want to use the gem it by adding the following to your environment.rb

config.gem "mattfawcett-phpbb-auth", :lib => "phpbb_auth", :source => "http://gems.github.com"

Add an entries to database.yml for phpbb_database_development, phpbb_database_production and phpbb_database_development.

In your config directory create a file called phpbb_auth_settings.rb, copy whats below into the file and change to match your setup.

PHPBB_AUTH_COOKIE_NAME = 'phpbb3_7uah4'; 
PHPBB_AUTH_LOCAL_USER_MODEL_NAME ='User' #this is the name of the model that you will use to store information about  users in your rails app 
PHPBB_AUTH_REMOTE_REMOTE_IDENTIFIER = 'user_email' #I use email to identify somebody, you may want to use the username  instead 
PHPBB_AUTH_REMOTE_LOCAL_IDENTIFIER = 'email' #this is the name of a field on your local user model that will be used to  lookup the value of remote identifier.
PHPBB_AUTH_COLUMNS_TO_DUPLICATE = {:user_website => :website} #specify any additional fields that you would like copying  to your local user model 

In application_controller.rb, include the module

include PhpbbAuth 

How you handle the rest is up to you but here’s what I did. Create a method called set_current_user.

def set_current_user   
  @current_user = current_user 

Add a before filter to the controller to run the method.

before_filter :set_current_user 

You will then have access to the @current_user variable in your controllers and views. This will be either nil if the user is not logged in, or an instance of your local User.

The source is up on Github.

Posted by Matt Mon, 31 Aug 2009 12:12:00 GMT

Monit Aggregator - A Sinatra interface for Monit

I use Monit to keep an eye on server processes and restart them if they go down. It comes with a useful web interface that gives you an overview of your servers health, and what processes are up/down. If you have a lot of servers then it can be a bit of a pain because you have to access each web Interface with a different URL.

This weekened I wrote a Sinatra app that aggregates the data from each web interface and displays it all on one page. The source is on Github.

It currently does not support HTTPS, and it only currently displays the overall system and individual processes.


To get going, install the dependant gems

gem install sinatra nokogiri haml


Now clone the repo

git clone git://github.com/mattfawcett/monit-aggregator.git


Then edit the monit_installations.yml file to include the details of each of your Monit web servers. To start the app running just type

ruby app.rb


Open your browser to http://yourdomain.com:4567

Alternatively you could run with Passenger.

There is also a free hosted version available on my server monitoring site Runs Like Clockwork

Posted by Matt Sun, 05 Jul 2009 16:41:00 GMT

s3ify gem - put the assets for a HTML email online

I often have to deal with setting up email sends, which usually involves getting a zip file from a designer containing a html file and some images. I usually unzip, convert all the image references to point to an S3 path, upload the whole folder to S3 using S3fox, and change the permissions to be publicly readable.

This is a bit repetitive and manual for my liking so I wrote a small ruby gem to make it a bit simpler. It uses Nokogiri to parse the HTML (so you will need a recent version of libxml) and the aws-s3 gem to upload everything to S3. I used the commander gem to create a command line interface to use it.

So first, get it installed

sudo gem install mattfawcett-s3ify

You will need to have your Amazon web services access information set in your path, if you don’t have this already, your best bet is to create a hidden file in your home directory called .amazon_keys with the contents

export AMAZON_ACCESS_KEY_ID='abcdefghijklmnop'
export AMAZON_SECRET_ACCESS_KEY='1234567891012345'

then add the following to your shell’s rc file (usually .bashrc)

if [[ -f "$HOME/.amazon_keys" ]]; then
    source "$HOME/.amazon_keys";

Then to use it simply open up a shell and type

s3ify -b my_bucket -p my_folder_in_bucket ~/Desktop/my-email/ 

The -b flag is the bucket you wish to upload to, the -p flag is the path in the bucket that you want to upload to and finally you declare the path to the folder where your HTML email is on the file system. For each HTML file, the gem will create an additional file with the prefix of “_s3” which will have the image references pointing to S3.

The code is on Github

Posted by Matt Sat, 27 Jun 2009 12:35:00 GMT

Server Monitoring with SMS alerts

For the last few weeks Iv been working on a new website in my spare time. Its now live and is at runslikeclockwork.com. Its a server monitoring tool where you add your sites and you will get an SMS alert when a site goes down / comes back up again.

Its slightly different from most other services because it checks the rendered HTML of a page for a keyword or phrase specific to that site. This covers you when you misconfigure an Apache/Nginx Virtualhost which can sometimes leave you with the wrong site being served on a domain, but of course the server is still giving out 200 status headers.

At the moment its very simple but I am planning on adding some more features over the next few weeks.

Posted by Matt Sun, 07 Jun 2009 17:02:00 GMT