Lojic Technologies

Posts Tagged ‘utility

Setup shoulda and rcov for Rails 2.2.2

with 4 comments

I had a few mis-configuration issues when setting up shoulda and rcov for a new Rails 2.2.2 project, so I thought I’d jot down a few notes (mini tutorial, quickstart) to help save others from burning time on what should be a simple task.

shoulda is a library build on Test::Unit that provides helpers, macros and assertions to make testing easier.

rcov is a code coverage tool for Ruby.

1. Install rcov

sudo gem install rcov

2. Install shoulda

sudo gem install thoughtbot-shoulda --source=http://gems.github.com

3. Create your Rails project

rails myapp

4. Modify myapp/Rakefile

require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
require 'shoulda/tasks'
namespace :test do
  desc 'Measures test coverage'
  task :coverage do
    rm_f "coverage"
    rm_f "coverage.data"
    rcov = "rcov -Itest --rails --aggregate coverage.data -T -x " rubygems/*,/Library/Ruby/Site/*,gems/*,rcov*""
    system("#{rcov} --no-html test/unit/*_test.rb test/unit/helpers/*_test.rb")
    system("#{rcov} --no-html test/functional/*_test.rb")
    system("#{rcov} --html test/integration/*_test.rb")
    system("open coverage/index.html") if PLATFORM['darwin']
  end
end

5. Modify myapp/test/test_helper.rb

...
# Add the following line
require 'shoulda/rails'   # require 'shoulda' also worked
...

Conclusion
After you’ve written some shoulda tests, you should be able to use the following rake commands:

rake test
rake test:units
rake shoulda:list    # display specs from shoulda tests
rake test:coverage   # run rcov and display code coverage

Written by Brian Adkins

February 26, 2009 at 9:10 pm

Posted in programming

Tagged with , , , , ,

Ubuntu Linux 8.04 – Wake on LAN

with 3 comments

Now that I’ve switched to a Macbook Pro with OSX Leopard as my primary desktop, I’ve located my Ubuntu machine in another part of the house to be accessible to my children. Not wanting to walk to the room where it’s located just to flip the power switch, I researched how to get “wake on LAN” working, so I could power it up remotely.

1. Enable the appropriate setting in your BIOS. Mine had something to do with wake on PCI device.

2. Install ethtool if you don’t already have it.

sudo apt-get install ethtool
cd /etc/init.d
sudo vim wakeonlanconfig

Add the following lines to that file:

#!/bin/bash
ethtool -s eth0 wol g

Install the script:

sudo update-rc.d -f wakeonlanconfig defaults

Run the script:

sudo /etc/init.d/wakeonlanconfig

3. Keep the network interface alive after shut down.

sudo vim /etc/init.d/halt

Change the following line:

halt -d -f -i $poweroff $hddown

to the following line (i.e. remove the -i)

halt -d -f $poweroff $hddown

4. Get the MAC address

ifconfig | grep HW

5. Send the magic packet via the following Ruby program:

require 'socket'
mac_addr = "x21x53x39xB3x90x42"
s = UDPSocket.new
s.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, 1)
s.send("xff"*6 + mac_addr*16, Socket::SO_BROADCAST, '10.0.0.255', 7)

Written by Brian Adkins

September 3, 2008 at 12:23 am

Automatically Delete Unwanted Cookies in Firefox

with one comment

I prefer to not have cookies stored in my browser, but it’s impractical to not store any cookies since this would require repeatedly logging in to authenticated sites that I frequently use. A simple solution in Firefox is the following:

From the Edit menu, choose Preferences and then click the Privacy tab. You should see a dialog similar to the following one:

firefox1.png

Check the “Accept cookies from sites” checkbox. For the “Keep until” setting, select “I close Firefox”. The latter is the key – it will erase all cookies from Firefox whenever you close the program. Of course, we don’t want to erase all the cookies, so click the “Exceptions…” button on the right and you’ll see a dialog similar to the following:

firefox2.png

Just type the name of the web site you want to allow in the text box and click the “Allow” button, and Firefox will add it to the exception list so it won’t be deleted when you close Firefox. You can add a full URL such as http://www.MySite.com, or just the domain name MySite.com to allow cookies for any host in that domain. You an also add sites you want to disallow any cookies from by clicking the “Block” button.

I have about 30 sites that I allow Firefox to store cookies for, but this technique has helped me avoid accumulating tons of unwanted cookies in Firefox. I hope it’s helpful for you.

Written by Brian Adkins

January 26, 2008 at 1:06 pm

Posted in internet

Tagged with , , ,

Use vimdiff to display subversion diffs

with 3 comments

I prefer using vimdiff or gvimdiff to view differences between files. When researching ways to allow using vimdiff to view subversion differences, I came across this article.

The bottom line is that subversion passes the two relevant arguments as the 6th and 7th arguments, so the following shell script wrapper does the trick:

#!/bin/sh
/usr/bin/gvimdiff ${6} ${7}

Save the script as gvimdiff_wrapper.sh, make it executable and accessible on your path. Then modify $HOME/.subversion/config to have the following line:

diff-cmd = gvimdiff_wrapper.sh

That will allow you to use gvimdiff to display the diff generated by svn diff my_file.txt

Written by Brian Adkins

November 27, 2007 at 10:14 am

Posted in programming

Tagged with , , ,

del.icio.us Tag Bundling

leave a comment »

I’ve written about del.icio.us several times before (use the search box to find the articles). I’ve been using the service for quite a while and still consider it to be one of the most valuable web services I use.

I just discovered the tag bundling feature from this article and tried it out. Tag bundling, as you might expect, allows you to group your tags. For example, my first bundle was “people”, so now I can see all my people tags in one group. I’ll be adding more bundles soon.

If you’re not using del.icio.us, you should really check it out. And if you, are and don’t know about tag bundling, give it a shot.

del.icio.us makes it easy to share tags – for example, here’s a link for my bookmarks on the Ruby programming language. I haven’t discovered a similar way for sharing bundles, so if you know, please leave a comment.

Written by Brian Adkins

November 3, 2007 at 8:47 pm

Posted in internet

Tagged with ,

Greasemonkey Script for Netflix Half Star Ratings

with 3 comments

I wrote an article back in May about a way to give half star ratings on Netflix. It had the advantage of working in any browser and not requiring any software installation, but it wasn’t very user friendly.

Since then, I’ve been doing a lot of JavaScript coding, so I thought I’d give Greasemonkey a try. I found a script here to give half-star ratings, but I didn’t care for the hover captions and JSLint pointed out a few issues, so I cleaned it up a little:

Code

// ==UserScript==
// @name Netflix Half Stars
// @description allows half star user ratings on Netflix
// @include http://*netflix.com/*
// ==/UserScript==
// http://userscripts.org/scripts/review/8118
// Modified by Brian Adkins

if (!unsafeWindow.sbHandler) { return; }

var sbHandler = unsafeWindow.sbHandler;
sbHandler.sbOffsets = [8,18,27,37,46,56,65,75,84,94];

sbHandler.displayStrings[0.5] = ".5 stars";
sbHandler.displayStrings[1.5] = "1.5 stars";
sbHandler.displayStrings[2.5] = "2.5 stars";
sbHandler.displayStrings[3.5] = "3.5 stars";
sbHandler.displayStrings[4.5] = "4.5 stars";

sbHandler.sbImages[0.5] = new Image();
sbHandler.sbImages[0.5].src = sbHandler.imageRoot+"stars_2_5.gif";

for(var i = 2; i < 11; i++) {
sbHandler.sbImages[i/2] = new Image();
sbHandler.sbImages[i/2].src = sbHandler.imageRoot + "stars_2_" +
(Math.floor(i/2)) + (i % 2 === 0 ? "0" : "5") + ".gif";
}

sbHandler.getStarCount = function (evt) {
var x = unsafeWindow.getElementMouseCoordinate(evt, this.element);

for(var ii = 0; ii < 10; ii++) {
if(x <= this.sbOffsets[ii]) { return (ii + 1) / 2; }
}

return 0;
};

Installation

Save the JavaScript code with .user.js extension e.g. netflix_halfstar.user.js and then open that file in Firefox and Greasemonkey should prompt you to install it.

Written by Brian Adkins

August 27, 2007 at 4:38 pm

Social Bookmarking

leave a comment »

Here’s a video that explains why using a site such as del.icio.us can be useful. I think they may have failed to mention that you can mark bookmarks as private on del.icio.us, so it’s not necessary to expose your bookmarks to the world. However, in my case, I only mark a small fraction as private.

I’ve been using del.icio.us for quite some time. After I had been using it for a while, I realized that it had been a long time since I bookmarked something in my browser because I had developed a habit of bookmarking in del.icio.us. Most browsers force you into placing a bookmark into a hierarchical, or directory, structure, but on del.icio.us you can assign as many “tags” as you like to a particular bookmark so you can search for things more easily. del.icio.us also allows you to export your bookmarks so you aren’t at the mercy of a proprietary service.

Another thing that is handy is to subscribe to the del.icio.us feeds of your friends to be automatically notified when they bookmark something that may be of interest.

Written by Brian Adkins

August 7, 2007 at 8:34 pm

Posted in internet, video

Tagged with , ,

Surf Securely Using SSH

with one comment

This is so easy, you’re gonna love it! Thanks Tyler Pedersen.

Motivation

I’ve been using my laptop more frequently at wifi hotspots. Many web sites I visit encrypt traffic with SSL for authentication, but after that they send traffic in the clear which means the cookies that are used for authentication purposes are sent in the clear, so anyone with a sniffer within range of my laptop could easily intercept the traffic, steal my cookies and impersonate me on the web site. Not good! So, I went looking for a simple solution, and found a great article about using ssh for this purpose. Ya gotta love open source software 🙂

Prerequisites

I’ll assume the following:

  1. You’ve used ssh before
  2. You have access to a remote host running sshd

How To

Issue the following command on your local computer:

ssh -Nf username@hostname.com -D 1080

replace username@hostname.com with the appropriate information. Look at the man page for ssh, or read the article linked above for an explanation of the options.

The next step is to configure Firefox to use the SOCKS proxy you setup with the above command. I’m using Firefox 2.0.0.6 on Ubuntu 7.04 Linux.

Edit | Preferences | Advanced | Settings

Pulls up the following dialog:

socks.png

Notice how I’ve switched from “Direct connection to the Internet” to “Manual proxy configuration”. I’ve also set the SOCKS Host to be ‘localhost’ and the port to be ‘1080’.

I can now surf and have encrypted traffic between my local computer and the remote host I ssh’d to. The traffic between my remote host and the destination web site will be unencrypted, but hopefully that traffic is harder to sniff without being detected.

At this point, I tested it out and everything worked fine. I then killed my local ssh process and Firefox complained about the connection being reset, so I knew it was in fact sending data over the ssh tunnel.

The final step is optional, but if you want to avoid having the bad guys detect your DNS requests (or possibly redirect them – d’oh!), you can configure Firefox to route DNS requests through the proxy.

  1. Type about:config in the Firefox address bar.
  2. Look for network.proxy.socks_remote_dns and set the value to true

Is that easy or what? Thanks again Tyler.

Written by Brian Adkins

August 2, 2007 at 12:28 am

Posted in internet

Tagged with , , ,

Google Reader

leave a comment »

I hate to promote Google given their trajectory to take over the world, but I just switched over to Google Reader for reading RSS feeds. I had accumulated over 60 RSS feeds, and it was becoming difficult for me to determine which feeds I should keep and which I should delete.

I was hoping for an automated tool that would keep track of which feeds are beneficial and Google Reader has exactly what I was looking for!

The trends feature will keep track of which articles I read from each feed and report on the total number and the percent. So, over time, I’ll be able to easily delete the feeds that have a low number and/or low percentage of read articles. If you decide to use Google Reader, you should be aware of some idiosyncrasies. When viewing in “Expanded view”, the default is to mark articles as read when you scroll past them which totally defeats the trends feature. You can turn that off in the settings.

settings | preferences | scroll tracking

I like using the “list view” instead which allows me to quickly view the titles. After I’ve read the articles I want to from a feed, I click “mark all as read” and Google Reader is smart enough to not count those in the “read” statistics.

If you’re already using a different RSS reader, you can easily import all your feeds via an opml file. I was using Liferea and had folders of feeds, and I had also renamed the feeds – the import to Google Reader kept track of all of that – nice.

Google Reader has a lot of other nice features such as keyboard shortcuts, tags, folders, etc., but once I discovered the trends feature, that was all I needed to see 🙂

I suppose the trends feature can be “unfair” though. Consider the following scenario:

  1. You have two feeds A and B
  2. Each day each feed publishes 10 articles
  3. The feeds overlap on 5 articles that are worth reading
  4. Feed A has 1 unique article that you read
  5. Feed B has 3 unique articles that you read

If the feeds are read in alphabetical order, then you’ll read the 5 overlapped articles from Feed A along with the 1 unique article -> total = 6, or 60%. Then you’ll read the 3 unique articles from Feed B -> total = 3, or 30%. The stats will show Feed A as being twice as valuable when clearly Feed B is more valuable. I suppose to get good stats, I should read the feeds in random order, but that seems difficult to manage.

UPDATE: ah, never mind. Simply view the folder that contains A & B and you’ll see the union of their articles in chronological order – whoever gets the overlapped story first wins 🙂

Written by Brian Adkins

July 30, 2007 at 11:41 pm

Posted in internet

Tagged with , , ,

Adblock Plus

with one comment

Facebook.com just ran an ad that was quite offensive to me. I should’ve taken Scott Moonen’s advice from his blog earlier, but better late than never. He has simple instructions for installing Adblock on his blog. Check it out and get rid of ads!

Written by Brian Adkins

July 23, 2007 at 12:29 pm

Posted in business

Tagged with , , ,