Archive for the ‘Ruby’ Category

RailsPR.com – A Ruby and Rails community for Puerto Rico

Posted on October 1st, 2007 in Open Source, Ruby, Ruby On Rails | View Comments

Note: Unfortunately, I had to take this site offline, due to its inactivity, and due to the fact that I have moved to New York City. I had plans for this site to be a starting point for me to start my own Ruby and Rails User Group in Puerto Rico, but I obviously can’t do that now. Hopefully someone in the near future in Puerto Rico will want to do something similar.

Although I finished this project a while back, I hadn’t announced it anywhere, so here it is. I just finished RailsPR.com, a site which I hope generates some much needed attention to the Ruby programming language and Rails framework here in my native land of Puerto Rico.

The main reason I started this site is because the IT industry in Puerto Rico is currently just centered on Microsoft products. I’ve said in a previous post that I really don’t mind Microsoft, and I think some of their products are great. However, with so many stable and useful open-source alternatives in the market, from operating systems to programming languages, I don’t see why it should be this way.

I’ve been learning and using Ruby for most of this current year, along with Rails. And it still baffled me whenever I talk to other programmers, they don’t even know what Ruby is. They’re still stuck with the same Visual Basic teachings they had in college. There are even some still using COBOL.

So my mission with this site is to create a community where interested programmers from Puerto Rico can be together in one place, sharing their knowledge and experiences, which is really what open-source is all about. It would make me proud to have my site be one of the main reasons people got into Rails.

Digg API with Ruby (and Rails too!)

Posted on April 25th, 2007 in Programming, Ruby, Ruby On Rails, Software, Web Development | View 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!

Lots of Rubies around

Posted on February 19th, 2007 in Open Source, Programming, Ruby | View Comments

I recently came across this blog post, where someone compared various implementations of Ruby, specifically to check the speed of each. When I read this post, I wasn’t surprised at the results. I was totally surprised at how many different Ruby implementations there are. I hadn’t realized how many there were.

Now, my question is, why are there so many different implementations that run Ruby code? I haven’t read about the others, such as Rubinius or Cardinal, so I still don’t know. I would imagine that there’s different needs for each (such as Gardens Point Ruby .NET, as the name suggests). But unless there’s a reason to have all these different ports, I wonder why there are so many.

Why don’t these developers focus on the original Ruby language and strive to make it better, instead of forking off to a different road and possibly confusing new users? Like I said, I haven’t read about these other implementations, so there may be something behind this. I’ll take the afternoon to read about the other implementations, and try to find out why there’s so much interest in making different Ruby implementations instead of focusing on the original.

Don’t take the easy way out

Posted on January 30th, 2007 in Linux, Programming, Ruby, Ruby On Rails | View Comments

I remember when I first started using Linux back in the day – “back in the day” referring to about the year 2000 – it was a total pain to work with. From not-so-great hardware detection, to installation and partitioning pains, to the ever-lasting fight against making a Winmodem work, it surely wasn’t for the weak-minded. However, something I sort of enjoyed during that period was downloading and compiling the programs myself. Well, I didn’t enjoy it much when dependency problems arose. But just the fact that I was taking this free and accessible source code, compiling it myself, then having fully functional software made me have a sense of accomplishment.

Nowadays most people have it easy. With friendly and easy-to-use Linux distributions, such as Ubuntu and OpenSUSE, all you need is to just click on a checkmark in a GUI interface and your programs, along with all dependencies, are immediately installed. Nowadays no one even has to use the command-line at all if they wish to do so. So as Linux gets easier to use, less and less people learn how to really use the operating system to its fullest.

Unfortunately, I too have fallen into that trap as well. I remember I used to compile the kernel every single time a new one was announced. Now, I barely do it. If the operating system I’m using at the time doesn’t have a current update, I don’t install it. Every time I see a new program I was to try out, I check first if there are already pre-compiled binaries for my distribution, and only compile the source code if all else fails. It’s kinda sad how much I’ve lost during these past few years.

That will probably change from now on. Last night, I took a spare computer in my apartment (1.0 GHz Athlon with 768 MB of RAM and a 20 GB hard drive) and decided to put it to good use. Seeing that I’m trying to learn Ruby and Rails, I wanted to install an easy-to-use distribution to save me some time with installation and configuration duties. So I chose Ubuntu, and it all installed just fine, until it came time to install Ruby and Rails afterwards. With Ubuntu, installing Ruby is just as easy as doing this:

sudo apt-get install ruby

Ubuntu did its magic, even though I didn’t have a good feeling when I saw it was installing version 1.8.4 (Ruby is now at 1.8.5). Oh well, I thought. It was just a minor revision, no big deal. I then went on to install RubyGems, and it installed okay, until I started getting some warnings. Since it finished the installation, I just kept on going. When it came time to install Rails, This time one of the gems didn’t install (I believe is was Action Web Service). I had installed Rails in other distributions without warnings, even on Windows. Since I had a couple of spare minutes at night, I decided to go the long route, and install everything from scratch, and make sure it’s up-to-date.

I’ll skip the entire process, as I’ll probably cover it in another, more formal, post. But I grabbed the source code of Ruby and after uninstalling all the mess the Ubuntu binaries caused, I compiled everything and I had absolutely no errors at all! I was even able to install the Mongrel gem without any problems.

So, that taught me a lesson as far as relying on pre-compiled binaries goes. Besides the obvious reasons why you should try to compile your own software (if the code is available), such as optimizing the compiled binary for your system and making sure you have the latest versions, you can avoid problems from the beginning, even if package managers such as Ubuntu’s apt are supposed to help you there. Also, it brought back that sense of accomplishment I used to previously have. So from now on, as long as time and skill allows me, I’ll be compiling my own code.

Time to pay a visit to the Linux Kernel Archives. Long time I haven’t been there.

Ruby makes me want to rock out

Posted on January 28th, 2007 in Programming, Ruby, Ruby On Rails | View Comments

During these past few weeks, I’ve been making a conscious effort to learn a couple of new programming languages, more specifically Ruby. As many people on the Internet, I was introduced to the beauty of the Ruby language when Ruby on Rails started to become popular around version 0.14. After reading a lot of Internet beginner tutorials, I decided to take the plunge and start learning Ruby now.

After grabbing the Programming Ruby book (lovingly known as ‘Pickaxe’ around Ruby circles), I have really fallen in love with this programming language. It’s not too difficult to learn, and you can produce really clean, readable and over-all beautiful code. I even feel Ruby has helped me become a better programmer, in the sense that I am striving to have all my code as clean and readable as possible. Being a full-time PHP programmer, you can imagine that clean and readable code isn’t my strong suit. After learning a bit more about Ruby, I’ll go ahead and dedicate myself to start learning Rails. I know I can start using Rails without vast Ruby knowledge, but I want to start with the basics, and I feel that’ll make me better at learning Rails.

However, the reason I’m writing this post today is because of an age-old question I’ve had ever since I started taking my programming courses in college. A lot of people question my choice of music while programming. This has popped up in some conversations because I love Rock music, especially anything that’s Punk Rock or Metal. The harder and faster it is, the better. So in a profession where I need to have absolute concentration to produce the best results possible, how can it be that I can concentrate more with some Bad Religion or Bullet For My Valentine on my MP3 player? It’s really weird.

At my current job, I tend to put some Rock music when no one’s around (just because I’m considerate of others). When I do, I notice I don’t move from my seat, and just start coding away. When I don’t have any music on, I tend to be distracted more easily, and go out for frequent bathroom breaks or just to chat with other co-workers. I wish my current employers could notice this and let me bring my own MP3 player to work and just leave me alone for long periods of time. I can guarantee that I can work much better and efficiently this way. But that seems like wishful thinking for now.

This past week, I think I’ve learned a whole lot with Ruby, partly because I’ve taken more time out of my schedule to sit down and practice hands-on. But it could also be because I just bought a couple of more CD’s for the collection, and I tend to listen to my CD’s completely when I code, instead of just having them in my car and listening to one or two songs before I move on to the next. I should do a formal study of this some day. Has anyone else in the world noticed this?