SHX.ca

the SHelby matriX

Restaurant Picker Script

I decided to do a screencast on this simple script to see if I’m any good at presenting this sort of thing. Hope you enjoy!

If you prefer to read, just skip below the video.

Every week I head out to lunch with a couple of buddies from work and every week we have a hard time picking a spot to go to. We have even resorted to putting all of our favourite spots on little pieces of paper and pulling it out of a box to decide where we will go.

The main thing wrong with this method of choosing a place to eat is that we can’t remember what places we have written on all of those pieces of paper.

I decided to write a rails application that would replace this process as a fun little hack. I completed it and thought that rails was extreme overkill for what I wanted to do. Converting everything to a command line script was quick and easy.

Not all of our favourite places are in this script but you can get the idea of how easy it is to write your own.

Restaurant Picker
1
2
3
4
5
6
7
places = ["The Market",
                  "Under the Volcano",
                  "Moxies",
                  "Sushi",
                  "Winks",
                  "Jewel of India"]
puts places[Random.new.rand(places.size)]

And as promised in the screencast, here is the link to Ruby’s Random class here.

First Octopress Post

This is my first post written from start to finish in markdown. I have officially rid myself of Wordpress and it feels pretty good.

This website originally was a simple ruby on rails app with a blog written in Wordpress and the two did not really like to work together. I had a post a while ago detailing how to render static html in a view because I wanted the most popular posts from my blog on all of the pages. In the process I had to dig into plugins, themes and not to mention php to get it to work and the taste in my mouth afterwards was bitter. I had to spend a lot of time for such a small feature and I knew there had to be a better way.

What Made Me Switch?

The main thing that made me switch was that my Linode server had a hardware problem and my site had a little down time. Honestly, it wasn’t that big of a deal because Linode fixed me back up in no time but I got the bug to look into other hosting. Digital Ocean had a free trial so I decided to fire up a cloud server with similar specs as my Linode server and it was super easy to get started. I have officially moved over my sites to Digital Ocean and so far, so good. I may have to give a review of them in the future once I have used them more.

Why Octopress?

I did not feel like setting up and migrating my Wordpress site to a new host so I thought I would look for a pure ruby solution for a Wordpress replacement. As an exercise, I was going to build a markdown blogging engine from scratch but Octopress is almost what I had in mind and it was already coded for me.

There were some growing pains in the migration, but after learning about the theming system and how Octopress works in general, I think I will stick with this new (to me) platform and maybe work on some more content.

Things Still Left To Do:

  • Since there is no database in an Octopress blog, all of the comments did not come over. I need to hook into a 3rd party commenting system.
  • I’m almost using the stock Octopress theme right now. I would like to add some more flare to personalize it.
  • I got a Blue Yetti microphone on boxing week so I would like to do a basic ruby or rails screencast in the near future

In conclusion, if you aren’t totally happy with Wordpress and have a small blog, you might want to make the switch to a more lightweight and hacker friendly platform such as Octopress.

Scripting User Interaction in Bash

Linux Penguin

For some reason or other, you may want to script something that requires some user interaction.

In my case, I had some PDFs that were created in such a way (from Microsoft Word) that unexpected things were happening when marking them for redaction.  Very very weird problem which I could not fix in a Windows environment.

Since I did not have the original .doc files, I found the only solution was to print to a file from the document viewer from a Ubuntu laptop I have.

The following is my quick and dirty solution for opening a bunch of pdf documents in the document viewer and printing them into my documents directory.  At the end there is a little check to make sure that the source and destination have the same number of pdf files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash

fix_pdf () {
  evince $1 &
  sleep 2s

  xdotool key Control_L+p
  sleep 1s
  xdotool key Tab
  xdotool key Tab

  xdotool type $1
  xdotool key Return
  sleep 20s
  echo "exported $1"

  xdotool key Control_L+w
}

for doc in *.pdf; do
  fix_pdf $doc
done

# sanity tests
echo "Number of before pdfs"
ls -l *.pdf | wc -l
echo "Number of after pdfs"
ls -l ~/Documents | grep pdf | wc -l

The key to the user interaction is the xdotool utility.  I use it in a very simple way but the gist of it is that you use the ‘key’ argument to send a set of key commands or ‘type’ to type something

This will type CTRL+P (note the lower case ‘p’.  A ‘P’ will not work).

1
xdotool key Control_L+p

This will write “Hello my name is Steve”

1
xdotool type "Hello my name is Steve"

Adding Folders to Your $PATH Variable on Mac

I recently upgraded my Macbook Air to Mountain Lion and I’m pretty impressed at the $20 upgrade.

One problem I had was that some custom scripts I run regularly were not being executed. This was due to Mountain Lion’s installer resetting my $PATH variable.  The scripts were still there, just the O/S didn’t know where to find them.

The fix is pretty easy and I suspect I’ll have to do this again in a year when I upgrade OS X again.

Edit your ~/.bash_profile and add the directory you wish to put your scripts in.  I keep mine in ~/bin.

1
export PATH="$HOME/bin:$PATH"

If you just want to temporarily fix your $PATH variable you can just run that command but putting it in your ~/.bash_profile will persist your changes.

Trying Out Sublime Text 2

If you haven’t heard of Sublime Text 2 it’s a great (but kind of expensive) text editor for Mac, Linux and Windows.

On Twitter yesterday, Ryan Bates of railscasts.com fame posted a poll in which rubyists posted what editor they use for Rails development. You can see the poll here.

I haven’t been sold on any of the major editors but number two in the list is actually Sublime Text 2 which until yesterday has been in Beta. I’ve used the first place finisher, vim, quite a bit but something about it doesn’t sit well with my workflow.

I’ve been experimenting with emacs for a while and that just seems slow to me.  I love the features but to load my init.el file and it’s dependencies it takes a good couple of seconds which seems to annoy me more and more every time I use it.

So my project for the next couple of weeks (one of which I’m on vacation) is to test the functionality of Sublime Text 2 and hopefully get some programming done.  I’ll be following up on this post in the next little while with my findings.

RIP London Budget App

It’s time to say goodbye to a project which I spent a lot of time on and will always have a place in my heart.

What is the London Budget App?

The London Budget App is my winning submission to www.budgetappcontest.com(link long since dead).  I won first prize and am quite proud of the achievement but the usefulness of the site has come to a conclusion as the budget passed a couple of months ago.

As a courtesy to the City of London (Ontario) I hosted the site on my own server.  Rails websites can be tricky to set up for rails newbies and I was happy to provide that service. This is not something that I had to do but I thought it was the least I could do since I received some prize money.  But I need to clear up some resources on my server for my current projects.

My Photo in the Metro NewspaperMy Photo in the Metro Newspaper

Why I’m Proud and Why I Like the App

  • I won the contest as a lone contestant while there was a submission from a TEAM of full time web developers / designers
  • I programmed this site to be useful
  • This was the first Ruby on Rails site I have released to the public in a while
  • I didn’t write any tests for the contest but I definitely would have had this been client work

What I Would Change in the App / Regrets

I programmed a LOT of backend functionality into the site that was not even seen by the user.  Mainly importing csv budget data and a whole system for a budget feedback system.  If I was to do this again, since it was just a contest I would have imported the data and been done with it.  I added a lot of error handling so people using the app next year would have less troubles.

My main regret is that I never really got feedback from the City.  The obviously liked the end product because they chose my site as the winner but I wish this could have been a more collaborative effort between the City and myself.  I’m sure I could have cut half of the time I spent on the project if the City was a client instead of a judge. Live and learn.

As with any project, I always feel I could have done better but looking back on my London Budget App experience, I learned a lot and will bring that experience with me to my future endeavours.

Rendering External HTML in a Rails View

The situation came up where I needed some information from a Wordpress site displayed on a Rails page. The solution had me delve into the guts of Wordpress and what I did was generate a single “page” which contained only the HTML (generated from the database) that I needed.

I won’t go into detail on how to mangle with Wordpress because I find it inelegant and maybe even a little ugly, but what I’ll show you now is how I rendered that information in my Rails project.

The first thing we need is a URL for our rails app to scrape. For the sake of this example lets create a small list at http://myurl.com/small-list.html

1
2
3
4
5
<ul>
  <li>I am the first element in the list</li>
  <li>I am the second element in the list</li>
  <li>I am the last element in the list</li>
</ul>

Note that this does not have to be on the same server as your Rails app.  This code will be downloaded over the internet if it is available. If not, you will have to do some error handling.

Next I need to write a Ruby method in my controller to download the file. I will put this in my Application Controller so it may be accessed by anything.

1
2
3
4
5
6
7
8
def fetch_url(url)
  r = Net::HTTP.get_response( URI.parse( url ) )
  if r.is_a? Net::HTTPSuccess
    r.body.force_encoding("UTF-8")
  else
    nil
  end
end

What the fetch_url method does is take a url as an argument and returns the value in between the and tags. If there is an error, return nil.

So now it’s pretty trivial to write a small method to get our small-list.html file.

1
2
3
def get_small_list
  @small_list = fetch_url "http://myurl.com/small-list.html"
end

For the sake of this example lets say we want to view the small-list.html on every page contained in MyController.

1
2
3
4
5
6
7
class MyController < ApplicationController
before_filter :get_small_list

def index
end

end

Now in your view it’s as simple as adding one line to your page.

This is a simple example and there are caching concerns if you are pulling information from a database but if you need an effective yet quick and dirty way to render some html from another site, here it is!

Mass Convert Images With ImageMagick and Powershell

In my other life as a systems administrator, I still have to deal with the Windows operating system and Windows Servers.

Not to go into too much detail but I was tasked to convert some images supplied by a forensic accounting firm for importing into one of our legacy systems.

The images were in TIFF format, each containing a page of a document. All of the TIFFs in each folder belong to the same document.

My main problem is that the TIFFs would not be recognized by our importing software so I needed a quick and easy way to convert all of the images before importing.

I tried to convert these images with software such as ACDSEE which our company had purchased for this task in the past but the 22,000 images in 3000+ directories consistently crashed the computers I was running them on.  So to develop a free solution in combining ImageMagick and Powershell was fantastic!

ImageMagick is free, downloadable software from ImageMagick.com and Powershell is a scripting language from Microsoft that is available for free in windows update from XP Service 3 onwards.

Once I had ImageMagick installed, I executed the following command from my command line to convert all of my TIFFs.

powershell "dir -recurse -include *.tif | %{mogrify -compress Group4 $_.FullName}"

After a couple of hours, the images were in a format I could use.

In a nutshell, powershell iterates over all of the TIFF files in all subdirectories and runs the mogrify command that is contained in the ImageMagick package. Mogrify modifies the file in place and overwrites the original.

Group4 is the image compression that I needed for this particular task but you could choose whatever you want.

The power of running a one line command to mass modify 22,000 files is quite powerful and I hope you Windows users find it helpful.