Sunday, February 15, 2009

YahooCurrency Gem

I recently created the YahooCurrency Ruby gem that gets currency exchange rates from Yahoo! Finance. Originally the code was part of a working Ruby on Rails demo I created about 12 months ago. A project we are currently working on needed exchange rates, so i created a Gem from the original code so we could easily reuse it across any of our future Ruby projects. We discussed making the project open source, and agreed that it would probably be useful for others, and also great for Global IT Creations to contribute something back to the open source. It is only a small contribution, but you have to start somewhere. Here is some example code...
exchange_rate = YahooCurrency.get_rate!("JPY", "USD")
exchange_rate.from #=> "JPY"
exchange_rate.to #=> "USD"
exchange_rate.rate #=> 0.0111
exchange_rate.timestamp #=> Wed Feb 11 22:20:00 +0800 2009
Get the source or the gem from http://github.com/scottbarr/yahoo_currency/tree/master Hopefully this will be the first of many projects that GITC releases as Open Source.

Thursday, February 12, 2009

Horror Movie Code

For the last few days I've had the displeasure of working through some code that we have inherited from another company, which I will not name. One section of code I'm looking at has a 300+ line if/elseif block, up to 9 levels deep! And if you want to see some real crap, you can look anywhere else in the remaining 700+ lines of code. As for the project in general there are no meaningful unit tests, all the code is crammed into the Controllers, and it is so un-DRY you need a raincoat to get through it. Code like this makes me want to cry. This code is concrete evidence that if your parents are blood relatives you should not write software. (No offence intended to non-code writing inbreeds.)

Monday, September 22, 2008

Rails with NO Fixtures?

I have a Rails project, and I created a Country model. I added the basic details of Norway to the countries.yml fixture.
norway:
 code: NO
 name: Norway
I added a unit test to make sure I could get Norway by it's country code, which is NO
def test_find_by_code
 assert_equal countries(:norway), Country.find_by_code('NO')
end
Agreed, I probably don't need a unit test for this, but this should have worked, but it was failing with the following message.
Country id: 318845244, code: "1", name: "Norway" expected but was
nil
Apparently NO in the yaml file is interpreted as a 0 (zero). To prove the theory I changed the code of norway to YES. The unit test failed, and indicated that the model it expected should have had code: "1". Changing countries.yml to be...
norway:
 code: "NO"
 name: Norway
I personally really like fixtures and the nice features they bring in, but sometimes they can catch you out. I'm sure it's not a bug, I've just never encountered this before. Ever found anything strange in your fixtures?

Sunday, August 31, 2008

View source code for iPhone RSS feed

I tried to view the Slashdot RSS feed on my iPod Touch, but instead of an RSS feed I was redirected to a specially formatted feed for iPhones. Very nice!

The iPhone specific URL is http://reader.mac.com/mobile/v1/http://rss.slashdot.org/Slashdot/slashdot .

I was curious to about the source code of the page, really just to see if there was any interesting markup. So I tried to view the URL in Firefox... no dice. I was shown a page saying, "This Application Is Viewable Only On iPhone".

Hmm... it was time to cheat a little then. I googled around for the User Agent that the iPhone sends, opened a terminal and entered...


Thursday, August 14, 2008

Ruby on Rails doesn't scale you say?

You might want to read this how this Bumer Sticker, a Linked-In application, that is serving 1 billion page views per month. From the article... "Ruby on Rails is frequently criticized for lacking the ability to scale. While the road to a billion page views per month has certainly had some potholes, Bumper Sticker has clearly demonstrated that the Rails platform can scale quite well, so as long as the team behind it understands that many of the bottlenecks are exactly those faced by developers on any other database-driven web platform." Joyent also blogged about this. How many Rails application have you developed, then found scaling the app was fast, relatively simple... not to mention cheap? How many Java web applications have you developed, then found scaling the app was difficult, slower, and expensive? You probably aren't even aware of the extra work you're going through here if you've never tried to scale a Rails app. You can take your pick of frameworks, I'm not citing any specific Java web application frameworks here. I'm not directly comparing Rails to Java, but Rails to insert-your-java-framework-here. I've been working with Java for a long time, and it still has it's place. But I think Rails is eating Java's lunch in a lot of areas these days, particularly in the web application and web services area.

Thursday, August 7, 2008

Stop and start PostgreSQL on OSX

To manually stop or start a PostgreSQL 8.2 server that was installed via macports, use the following commands.


Stop


# sudo launchctl stop org.macports.postgresql82-server

Start


# sudo launchctl start org.macports.postgresql82-server

Obviously this will also work for PostgreSQL 8.3 or 8.4 if you change the commands slightly.


I prefer the approach Linux distros have generally taken for service management, where all services are listed under the /etc/init.d diretory. To me /etc/init.d/postgresql-8.2 is easier, and you can use command completion to type it.

Friday, July 18, 2008

sudo bash > /dev/evil

Today I sat down with one of my team while he logged into a linux server to perform some administration tasks.

After ssh'ing to the machine, the first command entered was:

# sudo bash

Ouch!

Most of the time when we log into a remote server we are intending to perform admin tasks, but not every command needs to be executed as root. Executing "sudo bash" defeats the whole purpose of using sudo to begin with.

Sudo helps provide elevated privileges to multiple users of a machine. I don't want to limit the sudo rights on these machines as they are development and integration machines and many services are experimented with, configured, stopped, started, and generally played around with. I just want the team to treat root with respect.

Am I being picky about this?

What do you think?

Sunday, June 22, 2008

Re-Architecting J2EE with REST

I'm investigating re-architecting parts of an existing client's application. I prefer not to re-architect existing applications of non-trivial size but the current application has outgrown its current lack of architecture. Because of it's size (not to mention the development environment being used!), development work on the application runs at a snails pace, fear of breaking code is high, and deployment is a long, painful process. The state of the project is at least partly my responsibility, as I have worked with this project in the past. My previous attempts to get client buy-in on improving the architecture failed. I am planning to use other methods and tactics to try and illustrate to the client just what they are getting out of this. But I digress... I have become a big a fan of REST over the last few years with success on various projects built using REST. I looked at the Restlet project about a 6-9 months ago, but the documentation was inadequate for me at that stage. This isn't surprising because Restlet was quite early in the development at that time, so I'm not having a go at the Restlet team here in any way, they've been doing some really good work with their REST implementation. But on having the need for REST in Java again, I've taken another look at Restlet. The documentation has improved a lot. The examples, tutorials and explanations of the Restlet implementation are now at a stage where you can quickly evaluate Restlet, and decide if it can work for you. I would of course rather do all of this in Ruby or Python, but with the existing application in Java/J2EE the Restlet implementation of REST will mean the current team can still use Java, and means I have one less hurdle to try and negotiate. As I have experienced on other large applications, when you move components out into discreet services you end up with a nice SOA application that naturally enforces separation of layers. I am expecting to see large improvements in development speed, quality of code, and deployment. I'll blog about how the proof of concept goes in a later post.

Thursday, June 19, 2008

MacBook Pro Trackpad Gestures

Sometimes its like a light bulb just got switched on in your head, and you have a strong suspicion you were the only one still floundering around in the dark :) I was just reading some pages at the memcached site using my MacBook Pro, when I accidentally dragged 2 fingers down the trackpad. And what do you know... the page scrolled! I didn't realise that using two fingers on the trackpad was a gesture for scrolling. After a little investigation theres a whole bunch of gesture options under the Mouse and Keyboard options in System Preferences.

ARDAgent Local Explot on OSX

Ouch... Nice one. # osascript -e 'tell app "ARDAgent" to do shell script "whoami"'; root Hopefully this is patched soon. I'm sure there are some apps out there that could make creative use of this hole. This is discussed at length on Slashdot at, "Mac OS X Root Escalation Through AppleScript".