Thoughts on Technology, Methodology and Programming.

Install RabbitMQ on Snow Leopard with Macports

Posted by Marcus Wyatt on 10 March 2010

Before we can start to install RabbitMQ, we need to update the MacPorts repository to use the latest RabbitMQ release:

Open /opt/local/etc/macports/sources.conf in text editor, and replace the ‘rsync://’ line with the following:

rsync://rsync.macports.org/release/ports/ [default]

Now we can use the sync command to cause MacPorts to retrieve the package indices from the newly-added repository

sudo port sync

Before you install, it will be a good idea to check the available versions of RabbitMQ server by running ‘port search’.

Now, we can install:

sudo port install rabbitmq-server

All good so far, right.

Then the instructions tells you when you want to run the newly installed RabbitMQ server, all you need to do is execute:

sudo rabbitmq-server

Or you can enable the launchd startup item:

sudo launchctl load -w /Library/LaunchDaemons/org.macports.rabbitmq-server.plist

BUT, you get the following error when running ‘sudo rabbitmq-server’:

The error is caused because Erlang cannot find the rabbitmq libraries. The libraries should be installed into the same directory as the Erlang/OTP’s core libraries. The directory usually is ‘/opt/local/lib/rabbitmq/lib’ when you used MacPorts to install Erlang.

You can find out where the libraries is install by using the Erlang shell:

  1. Enter the Erlang console by executing ’erl’ in your shell
  2. Now execute ’code:get_path().’ to get the Erlang libraries path, you should see the following:

Next we need to find out where the RabbitMQ libraries are installed. Normally, with MacPorts install it should be ‘/opt/local/lib/rabbitmq/lib/rabbitmq_server-1.7.2’ (can be another version as well). But, if you used another install method, then you can just search the filesystem for ‘rabbit.beam’ to find the location.

Now, we need to symlink the rabbitmq_server library directory to the Erlang library directory.

cd /opt/local/lib/erlang/lib/
sudo ln -s /opt/local/lib/rabbitmq/lib/rabbitmq_server-1.7.2 rabbitmq_server-1.7.2

And you should be good to go. Now just start your RabbitMQ server with:

sudo rabbitmq-server

And check the server status with:

sudo rabbitmqctl status

You shouldn’t see any error output for the status call.

Status of node ‘rabbit@marcus-macbook’ …[{running_applications,[{rabbit,”RabbitMQ”,”1.7.2”},
{mnesia,”MNESIA CXC 138 12”,”4.4.12”},
{os_mon,”CPO CXC 138 46”,”2.2.4”},
{sasl,”SASL CXC 138 11”,”2.1.8”},
{stdlib,”ERTS CXC 138 10”,”1.16.4”},
{kernel,”ERTS CXC 138 10”,”2.13.4”}]},

If you are having troubles with starting the server, try the following:

  • Make sure your ‘/etc/hosts’ file has an entry to ‘’ pointing to your computer name. To locate your computer name just look in System Settings > Sharing and at the top you’ll see a box containing the computer name.
  • If you are still having issues, try deleting the mnesia database backing RabbitMQ. (When the server starts successfully it will create a new database.)

    sudo rm -rf /opt/local/var/lib/rabbitmq/mnesia/rabbit

Hopefully, peeps will find some of this helpful.



Posted in Uncategorized | Tagged: , , | 5 Comments »

Gems, gems, and more gems…

Posted by Marcus Wyatt on 20 February 2010

I’ve been working frantically at gemifying some plugins. I can’t unfortunately lay claim to have written these gems, but I needed to use them as gems with the new bundler system. So I created or forked the projects on github and used jeweler to gemify them

  • pgexplain – The Rails gem shows the output, in your application logs, of applying “Explain” or “Explain Analyze” to the select queries of your rails application using PostgreSQL.
  • railsonpg – This is timesaver for middle/large Rails application which use PostgreSQL as database. Create/drop Views, Functions, Triggers, Foreign keys in your migrations using ruby syntax.
  • swffu – swffu (pronounced “swif-fu”, bonus joke for french speakers) uses SWFObject 2.2 to embed swf objects in HTML and supports all its options. SWFObject 2 is such a nice library that Adobe now uses it as the official way to embed swf! SWFObject’s project can be found at http://code.google.com/p/swfobject swffu has been tested with rails v2.0 up to v3.0b and has decent test coverage so rake test:plugins should reveal any incompatibility. Comments and pull requests welcome: http://github.com/marcandre/swffu

The swffu plugin to gem conversion was the most work. Since the plugin used install.rb and uninstall.rb to add/remove some assets (javascripts, etc) to the rails application structure. I had to rethink how this functionality would be achieved. I decided to use rake tasks to accomplish the same functionality. The only trick is to add the require ‘swffu/tasks’ to the Rakefile.

So enjoy these new gems!

Posted in Uncategorized | Tagged: , , | Leave a Comment »

How to fix the iconv require error in Ruby 1.9

Posted by Marcus Wyatt on 3 February 2010

So you’re trying out the pre-release version of Rails 3.0 with Ruby 1.9 and you keep on getting the following error:

activesupport-3.0.pre/lib/active_support/inflector/transliterate.rb:2:in `require’: no such file to load – iconv (LoadError)

If you are using rvm to install and switch between different versions of ruby then follow the following steps:

  1. Install readline using rvm: rvm install readline rvm package install readline
  2. Now install iconv by executing: rvm install iconv rvm package install iconv
  3. if you already have a version of Ruby 1.9 installed, we need to remove it by executing: rvm remove 1.9.1/1.9.2
  4. The final step is to re-install the version of ruby: rvm install –trace 1.9.2 -C –with-iconv-dir=$HOME/.rvm/us
    (If you are having problems with the command, use the pastie that Glenn Rempe posted in the comments. Thanks Glenn)

Now to test that it worked:

  1. Change to the ruby version you installed: rvm use 1.9.1/1.9.2
  2. Check you are on the right version of ruby: ruby -v #=> ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10.2.0]
  3. Start a new irb session
  4. Now you should be able to require ‘iconv’ and get a ‘true’ result

    irb(main):001:0> require ‘iconv’

Now you should be good to go!

Posted in Rails, Ruby | Tagged: , , | 9 Comments »

scrAPI on Snow Leopard

Posted by Marcus Wyatt on 3 November 2009

Today I needed to scrape some data from a website and tried to use the trusted old scrAPI to do the job. Grrrr, its not working. Throwing an error:

Scraper::Reader::HTMLParseError: Scraper::Reader::HTMLParseError: Unable to load /Library/Ruby/Gems/1.8/gems/scrapi-1.2.0/lib/scraper/../tidy/libtidy.dylib

After some time on google I didn’t find any fixes for the issue. So I decided to build from source…

I grabbed the assaf’s github repository.

  • git clone git://github.com/assaf/scrapi.git

Then tried the tests by running

rake test

117 tests, 346 assertions, 0 failures, 44 errors

Nope errors all over the show… Looking at the original exception message, I checked if the libtidy.dylib exist on the lib/tidy directory. Nope, not there….

So where do I get this library file….

MacPorts to the rescue… Install tidy from MacPorts using the following command:

  • sudo port install tidy

Now we need to find where MacPorts installed the files using the following port command:

  • port contents tidy

The result:

Port tidy contains:

Now all we need to do is copy the library file to our scrAPI source directory:

  • cp /opt/local/lib/libtidy.dylib [your source location]/lib/tidy/libtidy.dylib

Ok, before we speed ahead. Lets just run those test to check if all is fine:

117 tests, 474 assertions, 0 failures, 0 errors

Awesome, we are almost there. Next we need to build the gem using the rake:

  • rake package

Make sure you get a ‘Successfully built RubyGem’ message. Now we are ready to install the newly build gem and test scrAPI again.

  • sudo gem install pkg/scrapi-1.2.1.gem

And there you go, scrAPI working again.

Posted in OS X, Ruby, RubyGems | 5 Comments »

My bash history

Posted by Marcus Wyatt on 24 April 2009

Inspired by Ben Maybey and his bash history summary, here is mine:

Read the rest of this entry »

Posted in Uncategorized | Leave a Comment »

Execute sql within rails environment without using your models

Posted by Marcus Wyatt on 19 August 2008

When you have a query that you need to run as raw sql against the database, like batch data processing or maybe some data cleanup, and you don`t want to create ActiveRecord models to handle the data. Because creating models is expensive and slow. So we want to execute directly against the database. But how do we execute directly on the database from within our rails application environment? The solution is to use ActiveRecord::Base.connection to do the work:

sql = "my complex sql statement"

This is really nice way to execute queries within rake tasks. In the following example I show how you would establish a database connection as well:

namespace :db do
	  desc "Cleanup the database by setting rows to deleted when older than xxx. Defaults to development database.  Set RAILS_ENV=[production, test, etc.] to override."
	  task :cleanup => :environment do
	    sql = <<-SQL
	      -- do some cleanup code
	    # used to connect active record to the database


Here is a very funny tutorial on rake. That`s all for now folks…

Technorati Tags: , ,

Post created with TextMate.

Posted in Rails | 5 Comments »

Easily create strong passwords

Posted by Marcus Wyatt on 11 July 2008

Create strong passwords on the command line by typing

openssl rand -base64 6

and the output:

=> 050L+4Kx

Run the command again:

=> /sUIIy8s

Cool, now you should be able to create strong passwords quickly.

Posted in OS X, Tips, Ubuntu | Tagged: | Leave a Comment »

Databind a Combobox to an Enum Property – Version 2.0

Posted by Marcus Wyatt on 18 April 2008

I’ve first posted this tip on the 14 of May 2005 and I still get good traffic to the post. So I decided to update the post and code samples to use Framework 2.0 (I’m still not using 3.5) and improve the code somewhat.

The problem:

How do I databind my combo box to my domain object that has a property of some enum type?

The solution:

Quite simple, firstly we will create an object (EnumComboBoxItem) that would encapsulate and represent each item in the combo list data source. EnumComboBoxItem is a pure fabrication class that has two properties we’ll use when databinding to the combo. We have a Value property which is used by the ValueMember property on the combo box and EscapedValue that is used by the DisplayMember property of the combo box. Note that when we construct a new EnumComboBoxItem instance that we call the AsciiEscape method in the constructor when populating the escapedValue field.

Here is the code:

1 using System;

2 using System.Collections.Generic;

3 using System.Text.RegularExpressions;


5 namespace BindComboToEnum

6 {

7 ///

8 /// The enum combo box item

9 ///

10 public class EnumComboBoxItem

11 {

12 #region Constants


14 public const string DisplayMember = “EscapedValue”;

15 public const string ValueMember = “Value”;


17 #endregion


19 #region Readonly &amp; Static Fields


21 private readonly string escapedValue;

22 private readonly object value;


24 #endregion


26 #region C’tors


28 ///

29 /// Initializes a new instance of the class.

30 ///

31 ///

The original value.

32 public EnumComboBoxItem(object originalValue)

33 {

34 value = originalValue;

35 escapedValue = EscapeAsciiValues(originalValue.ToString());

36 }


38 #endregion


40 #region Instance Properties


42 ///

43 /// Gets the escaped value.

44 ///

45 /// The escaped value.

46 public string EscapedValue

47 {

48 get { return escapedValue; }

49 }


51 ///

52 /// Gets the value.

53 ///

54 /// The value.

55 public object Value

56 {

57 get { return value; }

58 }


60 #endregion


62 #region Instance Methods


64 ///

65 /// Returns a that represents the current .

66 ///

67 ///

68 /// A that represents the current .

69 ///

70 public override string ToString()

71 {

72 return escapedValue;

73 }


75 #endregion


77 #region Class Methods


79 ///

80 /// Creates the data source for the enum type.

81 ///

82 ///

Type of the enum.

83 /// When the type passed is not an enum the exception is raised.

84 ///

85 /// Returns a generic list to be used as the combo datasource.

86 ///

87 public static List&amp;lt;object&amp;gt; CreateDataSourceFor(Type enumType)

88 {

89 if (!enumType.IsEnum)

90 throw new ApplicationException(“When using the Enum combo data source you are only allowed to use enum types.”);


92 Array enumValues = Enum.GetValues(enumType);


94 List&amp;lt;object&amp;gt; result = new List&amp;lt;object&amp;gt;();


96 for (int index = 0; index &amp;lt; enumValues.Length; index++)

97 {

98 result.Add(new EnumComboBoxItem(enumValues.GetValue(index)));

99 }


101 return result;

102 }


104 private static string ConvertAsciiValueToStringRepresentation(string currentRegexMatchValue)

105 {

106 string result = string.Empty;

107 const int BaseValue = 16;


109 if (currentRegexMatchValue.StartsWith(“0x”))

110 {

111 // grab the remainder string after the 0x

112 string asciiCodeValue = currentRegexMatchValue.Substring(2);

113 // convert the remainder string to a byte

114 byte byteValue = Convert.ToByte(asciiCodeValue, BaseValue);

115 // now we can convert to a character and find the string of the character

116 result = Convert.ToChar(byteValue).ToString();

117 }

118 return result;

119 }


121 private static string EscapeAsciiValues(string rawValue)

122 {

123 string result = string.Empty;


125 if (rawValue == null)

126 return result;


128 // Find 0x with any four alpha-numeric characters

129 Regex regex = new Regex(“0x[0-9A-Z]{4}”);


131 MatchCollection matchCollection = regex.Matches(rawValue);

132 Match matchPrevious = null;


134 for (int i = 0; i &amp;lt; matchCollection.Count; i++)

135 {

136 // if no match (ie. null) set to 0, else the current index + length

137 int startIndex = (matchPrevious == null ? 0 : matchPrevious.Index + matchPrevious.Length);


139 // add the part of the string that is not the ascii expression

140 result += rawValue.Substring(startIndex, matchCollection[i].IndexstartIndex);


142 // convert the ascii expression to a normal string

143 result += ConvertAsciiValueToStringRepresentation(matchCollection[i].Value);


145 matchPrevious = matchCollection[i];

146 }


148 if (matchPrevious == null)

149 result += rawValue;

150 else

151 result += rawValue.Substring(matchPrevious.Index + matchPrevious.Length);


153 return result;

154 }


156 #endregion

157 }

158 }

The EscapeAsciiValues method is used to Replace the Ascii characters in our Enum with the character representation. We do the Ascii characters because we want our combo to display “Bi- Annual” instead of “BiAnnual”. Look at the following enum as an example:

1 namespace BindComboToEnum

2 {

3 public enum PaymentFrequencies

4 {

5 Annual = 1,

6 Bi0x00A00x002D0x00A0Annual = 2, // Bi – Annual

7 Quarterly = 4,

8 Monthly = 12,

9 Fortnightly = 26,

10 Weekly = 52

11 }

12 }

Now, all that we need is the following code behind the form to bind our Combo box and we are away:

22 private void InitDataBindings()

23 {

24 PaymentFrequencyComboBox.DataSource = EnumComboBoxItem.CreateDataSourceFor(typeof(PaymentFrequencies));

25 PaymentFrequencyComboBox.DisplayMember = EnumComboBoxItem.DisplayMember;

26 PaymentFrequencyComboBox.ValueMember = EnumComboBoxItem.ValueMember;

27 PaymentFrequencyComboBox.DataBindings.Add(“SelectedValue”, new Loan(), “PaymentFrequency”);

28 }

The EnumComboBoxItem.CreateDataSourceFor method creates an List&amp;lt;object&amp;gt; for us to use as a data source for the combo box.

And there you have it, a Combobox bound to an Enum!

Posted in Software | Tagged: , , | Leave a Comment »

Run gem install behind a firewall in Windows

Posted by Marcus Wyatt on 18 March 2008

I’m contracting at a client running a Novell network and wanted to create some small little ruby scripts to automate some tasks for me. But I ran into problems using gem install behind their firewall. I’m getting the following error:

ERROR: While executing gem … (Gem::RemoteSourceException) HTTP Response 407

Here is the steps I followed to get gem install working on my windows xp machine in painstaking detail: 🙂

Firstly we need to set the HTTP_PROXY environment variable. I’ve played around with different options, using uid and pwd and other suggestions, but all I needed was the following:

  1. On the desktop right click the ‘My Computer’ icon and select properties.
  2. Now you can either add the HTTP_PROXY variable to the ‘User’ or the ‘System’ variables by clicking the ‘New’ button.
  3. Within the new system variable dialog, specify ’HTTP_PROXY’ as the variable name and in the value area a url in the following format – http://%5Bproxy_ip%5D:%5Bproxy_port%5D – i.e.

Next we install the Ruby Win32 SSPI which gives ruby the ability to authenticate with ISA natively. We need this library because as far as I understand, it patches the Open-uri library because of some incompatibilities with windows. Anyways, follow the following steps to get it working:

  1. Download rubysspi from the Ruby Win32 SSPI project page(also available as gem install rubysspi but that doesn’t help much, does it?)
  2. Install the gem locally using the following command: gem install [local_path_to_gem]\rubysspi-1.2.3.gem i.e. C:\gems\rubysspi-1.2.3.gem
  3. Now copy the spa.rb file from the gem install directory and paste it in the site-ruby directory. i.e. If ruby is installed in C:\ruby, then the paths should be:
    • origin path – C:\ruby\lib\ruby\gems\1.8\gems\rubysspi-1.2.3\spa.rb
    • destination path – C:\ruby\lib\ruby\site_ruby\spa.rb
  4. Find gem.bat in your ruby bin directory (C:\ruby\bin) and replace the last line (i.e. "%~d0%~p0ruby" -x "%~f0" %*) with the following line – @"ruby" -rspa "c:\ruby\bin\gem" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. Test you can access the remote ruby gems by executing the following command: gem list rails -r
  6. You should see similar output as shown above.

Now we can work again without any issues when trying to install or update gems.


Technorati Tags: , ,

Posted in Development, Rails, Ruby, Software, Tools, XP | 36 Comments »

TextMate command to annotate your current ActiveRecord Model with the DB Schema

Posted by Marcus Wyatt on 18 March 2008

Edit: If you can’t wait until the end, here is the download link.

Who hasn’t used the Annotate Models plugin written by Dave Thomas, of Rails Pragmatic Studio fame? If you haven’t don’t fret… The plugin basically adds a comment block to the top of all of your model classes documenting the current database schema for the given model. I like to have the info in my model files to make it a little easier to work with models.

Annotated Models Plugin

The rails bundle within TextMate gives you a ‘Show DB Schema for current class’ command you can press to show a tool tip with the database schema (Control + Shift + Command + S). This works great, but I find the command sequence to long and the slight wait to see the schema info breaks my rhythm.

Show DB Schema for Current Class

To solve the problem I made a copy of the source files used by the command and modified it slightly to output the info as comments at the top of the file. Now I have nicely annotated model files. This is a real time saver.

Annotated Models Bundles

The bundle also contain the the Beautify command. This command adds automatic code formatting capability for your ruby code. I found the beautify command from Tim Burks. Cheers Tim…

You can download the bundle here.

Technorati Tags: , , , ,

Blogged from TextMate

Posted in Mac, OS X, Rails, Ruby, Software, TextMate | Leave a Comment »