Archive for April, 2007

San Francisco, here I come… Hopefully!

Posted on April 28th, 2007 in Open Source, Programming | Comments

Before starting this post, I want to thank whoever posted my previous post about using the Digg API with Ruby and Rails to dzone. It recently made their front page, and a good amount of traffic headed this way. I hope someone found a good use on that small beginner tutorial.

Anyway, after working a while on this blog and my main site, I would like to tell the story on why this site is here today. I decided to take a look at some Rails hosting, as I wanted to start learning and actively using Ruby on Rails, so I got this small website to just experiment with different things. I had never gotten any type of web hosting before (well, outside my awful Geocities pages back in 1998 while learning HTML with Frontpage, but I hope those remain buried forever), so it was nice to finally have my own little space on the web, with a nice-sounding domain name.

However, after a while, what else was I going to do with this web space? So, I decided to start a blog, and use the main site as a way to post my resumé, my work and what I do for a living. Granted, I still don’t have too much stuff going, but that’s something I hope to change in the coming months. But the real reason of this site’s existance? I want to move to San Francisco and get a job over there.

Don’t get me wrong. I love Puerto Rico, and all of my family and friends are here, who I’ll certainly miss a lot if I leave. However, I find my chances of career advancement very limited here. The entire programming landscape, like most everywhere, is dominated by Microsoft products. And at this moment in time, I have no interest in learning .NET, C# or any other Microsoft-based language. I’m not anti-Microsoft at all, but I think my time will be better spent with what really interests me at this time, which is virtually all about open-source.

That’s the biggest difference between Puerto Rico and the United States, particularly San Francisco. There’s just many, many more opportunities for me to work with Linux, Ruby, Rails and any other open-source software available. Every time I search for job openings here in Puerto Rico, it’s pretty much all the same: System Administrators need people with MSCE certifications to work with Windows 2000/2003, or web developers who need to know ASP.NET. I’ve never seen an opening that needs someone who knows how to work with Red Hat Enterprise Linux servers, or web developers who know Rails, Django or any other open-source programming language.

Searching in different websites and job boards, it seems that California has many jobs that fit my interests. And while I’m still a relative ‘newbie’ in many programming languages, I know that if I find work in something that I would totally do for free if given the chance, I would give the proverbial 110% to that job, instead of viewing it as simply a paycheck. While I’m currently a bit happier at work due to what I’m doing now, I’ve been down that road at my current job, and it’s not really a good feeling to just go to work simply to pay the bills. I want to accomplish something. I know I won’t do that here, at least at the moment. I want to go somewhere and be part of a team that does something great.

I’ve been planning most of this year on what I need to do. I really don’t need to worry about family, as they’re all very supportive of me, and with no girlfriend or spouse, nothing is really tying me down here. I’m just saving money to be able to go later this year, hopefully before my 27th birthday this November. If all goes well, I’ll be a San Francisco resident by the time in 2008.

I’ve done a lot of research about the city and the Bay Area in particular. I’ve seen a lot of job openings, and I imagine it’s very competitive to land a good job there. But the thing that’s really holding me back now is that it’s expensive. I mean, at my current apartment, I’m paying a mere $200 per month; The cheapest one-room apartments over there are at least $750. So that’s a pretty scary thought, going over there without a job will be tough. I guess I’ll have to get used to Ramen noodles. I hope they’re as cheap as they are here!

So, any recommendations? I really hope someone from the Bay Area gets to read this and can leave a comment to set the record straight as to how life is over there, especially as far as job openings and the cost of life is. And if by any small chance you’re an employer (or are close to one), feel free to browse my resumé. I’m completely available to move to the Bay Area is needed. Hopefully this blog leads to the change I need for my career.

Digg API with Ruby (and Rails too!)

Posted on April 25th, 2007 in Programming, Ruby, Ruby On Rails, Software, Web Development | Comments

Note: This site has changed a lot over the last year. I’m not using Rails anymore on the site, so there’s no place where I’m currently using the Digg API. However, I’ll leave this post intact, as it might help others with something similar.

If any of you readers have come in through my main site lately, you should notice that I added a little something on the sidebar. One of my favorite time-wasting activities of the day is browsing Digg. I tell you, there should be some sort of “Diggers Anonymous” for us simple-minded folk who can’t stop from visiting the site for stories many times a day!

Anyway, they recently announced the release of the Digg API, to allow any user to access the stories on the site in any way, shape or form we desire. Now, I must say that I have no real use for this. Of course, being the geek I am, I just wanted to try it out and see how I could use a simple API using Ruby, for use on my Rails site (this one). This is also made in case anyone else wants to do the same. One note, however: I’m only a Ruby / Rails beginner. I’m sure there are much better and efficient ways to do this. Since I haven’t dabbled in this too much, and “it works on my machine“, I’ll leave it as it is for now.

Just for the record, the testing and development for this code was made on my computer running Ubuntu 7.04, along with Ruby 1.8.6 (compiled by myself, not downloaded from the Ubuntu repos), RubyGems 0.9.2 and Rails 1.2.3. It should work correctly with any fairly recent version of the software mentioned above.

The Digg API is rather simplistic right now. All you need is a simple request using a URL like this:

http://services.digg.com/stories?appkey=http%3A%2F%2Fdennmart.com&type=xml&count=5

In the example above, the API is called to the ‘http://services.digg.com’ server. The parameters afterwards (in this case, ‘/stories‘) is the request. You can add more parameters for a finer-grained search.

Once the request URL is complete, you need some additional actions to add as well. The ‘appkey‘ is required, but can be anything for now. Digg isn’t generating application keys (a la eBay or other external API’s), but an appkey is required for ’statistical purposes’, according to the documentation. The ‘type‘ is how the data is going to be returned to the app, and it can be either XML, Javascript, JSON or PHP. I use good ol’ XML for now, until I can actually play around further with the other response types. Finally, the ‘count’ action is to limit how many stories are returned. I’m only going over these options quickly, as the API documentation has much more information.

Once you figure out which request you want to make (using Mozilla Firefox can help greatly, as the XML response is nicely formatted), it’s a matter of getting that data into your Ruby or Rails app. Going back to the ol’ trusty Pickaxe book, I found a nice little module integrated in Ruby called open-uri. This module allows the Ruby application to open a URL (either http, https or ftp) and get the returned contents. To use this module, a simple line of code is needed:

require 'open-uri'

That should load your module correctly. Now it’s just a matter of having Ruby open the API connection and store its XML response in a variable:

response = open('http://services.digg.com/stories/popular?appkey=http%3A%2F%2Fdennmart.com&type=xml&count=5').read

The ‘open‘ function, well, opens the connection to the API, while the ‘read‘ method gets the data that’s returned from the URL. Like I said, rather simple.

However, I was getting timeout errors when calling the ‘open’ function. Strangely enough, the function worked fine when using any other URL. Upon further reading of the API documentation, I found this little tidbit:

“All API requests must include a User-Agent HTTP Header. A request without this header will receive no response.”

So, after that small mistake, I edited the request like so:

response = open('http://services.digg.com/stories/popular?appkey=http%3A%2F%2Fdennmart.com&type=xml&count=5', 'User-Agent' => 'Ruby/1.8.6').read

The ‘User-Agent‘ parameter can be anything. I just decided to use the Ruby version I have. Once I added that, I had my nice XML with the five most recent Digg stories that have been promoted to the front page.

After that, I needed to parse that XML. I searched around the Internet, and found a great little module called ‘XmlSimple‘. This module reads and writes XML, and formats it according to whatever’s needed. In my case, I needed to read the XML response. Just like the ‘open-url’ module previously, you need to load the ‘XmlSimple’ module as well, once installed (“gem install xml-simple“):

require 'xmlsimple'

I did run into some minor problems when loading this module. The Ruby interpreter cried out loud, saying it couldn’t load the module. How come? I verified that the gem was installed correctly, and it was. Then I realized that since this is a gem, I need to have ‘RubyGems‘ loaded before loading ‘xmlsimple’:

require 'rubygems'

I believe that Rails already loads the module for you. But if you’re testing this out on Ruby and not on Rails, you’ll need it.

Okay, once I did that, I was able to load the ‘xmlsimple’ module. Now I need it to parse the XML response that I stored in the aptly-named ‘response‘ variable. A simple line of code can convert the XML into a hash:

XmlSimple.xml_in(response)

That takes the entire XML string and puts it into a neatly organized hash. I really don’t need everything in the hash, just the story title, link and how many diggs the story has. So I just access those particular keys:

digg_hash = XmlSimple.xml_in(response)
story_title = digg_hash['story'][0]['title']
story_link = digg_hash['story'][0]['link']
story_diggs = digg_hash['story'][0]['diggs']

In the example above, the ‘story_title‘ variable has the most recent story title, the ‘story_link‘ is the link that takes you directly to the story, and the ‘story_diggs‘ has the current amount of diggs that story has. The number zero used in the array is the story number. If you want to get more than one, you’ll need to loop through the array and get the other stories.

The code I’m using on the main site right now is the following:

<%
  require 'open-uri'
  require 'rubygems'
  require 'xmlsimple'

  response = open('http://services.digg.com/stories/popular?appkey=http%3A%2F%2Fdennmart.com&type=xml&count=5', 'User-Agent' => 'Ruby/1.8.6').read
  articles = 0
  while articles < 5
%>

(Dugg <%= XmlSimple.xml_in(response)['story'][articles]['diggs'] %> times)
<% articles += 1 end %>

In all, this was rather simple, and I’m sure there will be a much more creative use for the Digg API soon. But for those who want to learn how to use it, or those who need a very simple approach, this works just fine. I’m interested in listening on how other people have put this to use. Hope this helps someone!

Rails Screencasts = The best learning tool on the Internet

Posted on April 16th, 2007 in Programming, Ruby On Rails | Comments

Well, it’s been a while since I last posted. Maybe it was due to the fact that I received so many damn spam comments that Akismet seemingly didn’t block. I’ll look into that when I have some free time to check it out. For now, I’ll just give a quick post on what’s on my mind.

Even though I haven’t been posting on this blog, it doesn’t mean I haven’t been occupied with my Rails learnings. I just discovered the wonderful new world of screencasts as a learning tool. If you don’t know what a screencast is, it’s basically a video tutorial. Instead of reading something off the Internet, where the instructions aren’t always crystal clear, a screencast shows you what to do, and how it’s supposed to work. I’ve found them to be an invaluable tool in my quest for Rails wisdom.

There are a couple of sites that offer Rails screencasts exclusively. The first site is Railscasts. This site is frequently updated with short screencasts about various Rails topics. I’ve noticed these videos are truly great for beginners, as most of the topics covered so far seem geared more towards people without much Rails experience. Even though the videos are normally short (about five minutes in length, maybe less, as an average), they’re packed with useful information that’ll help any beginner get a better grasp at certain Rails topics.

The other site I’ve visited is Peepcode. These people make high-quality and full-length screencasts (over an hour long!) for the low price of $9.00 for each video. Many may think “But why pay for screencasts that can be freely accessible from the other site?” There’s a very large distinction between the screencasts from both sites. While length is obviously the main factor, the material shown on Peepcode screencasts is much more extensive and covers absolutely all the bases. Many of the screencasts that are currently available for purchase on Peepcode are geared to more advanced users, though. But in all honesty, $9.00 is a very small price to pay, given what you learn.

So if you’re trying to learn Rails like myself, I’d suggest you head over to one of these site pronto. You’ll be amazed at the great work that’s put out on the Internet for the sake of teaching others. That’s the beauty of the Internet.