Archive for the ‘Software’ Category

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!

All the porn you want! (Not really)

Posted on March 2nd, 2007 in Software | Comments

Note: I noticed that this is the most-viewed in all my blog, thanks to this appearing in Google search results when people search for “Puerto Rican Porn”. Sadly to say, there is absolutely no porn on this site, so if that was your reason of coming here, you’ll be disappointed. However, if you do enjoy reading about technology, feel free to browse any of my recent posts.

Wow, I really can’t believe that this past week, my little ol’ blog has been bombarded with tons of spam comments. And this is a blog that barely has any readers at all. I can only imagine the amount of spam the more popular blogs on the Internet. Really, I don’t want to know where I can get she-male porn videos, or female ejaculation pictures. I’m not an avid pornographer, and rarely scour the Web for that stuff (note that I said ‘rarely‘ - at least I’m honest). But if I’m feeling a bit freaky, I’ll know where to go. It’s not like that stuff is totally hidden.

Anyway, I wanted to mention the Akismet Plugin that’s currently included in all Wordpress installations by default. After I got the first couple of spam messages, I decided it was time to activate the plugin to see how it works. After a couple of days, the plugin has captured more than 30 spam comments, and none have gone through. Pretty good job, if you ask me.

So, if you have a Wordpress blog, I really advise on activing the plugin. It only takes a few minutes, and you’ll be grateful once you stop getting offers for bestiality flicks. If you don’t use Wordpress, Akismet makes plugins for more than 20 different platforms, so you can check if your application is supported. It’s really a great plugin that should be activated by default on any blog or CMS app.

Too bad it can’t all be Rails

Posted on February 22nd, 2007 in Ruby On Rails, Software | Comments

I finally finished the minor remodeling of my personal page, as I transferred the old, plain HTML files to Ruby on Rails. It didn’t take too long, since the site is all static (for the time being). Just in case you’re wondering, I moved everything to Rails mostly because I was tired of having to make a change to a menu, and I had to change every single HTML file. Not anymore. Although this apparently is the only benefit I’ve gained, I’m planning on adding many more things in the future (using Rails, of course).

Anyway, I was explaining what I was doing to the website to a co-worker. Although he doesn’t understand much about programming, he did seem to understand the whole Rails thing. So he asked me if the blog was also made with Rails. I told him no, it was using Wordpress, which runs on PHP. SO his next question was a natural one: Why don’t you use a Rails blog?

I’m sure some people who visit this site will ask the same thing. I have a couple of answers to this. I had tried to use Typo previously on this site, but rapidly removed it and started using Wordpress. First off, the web hosting company I’m using isn’t the fastest or most reliable host around for Rails applications. So Typo felt damn slow, probably due to the amount of memory it needs to run. I know there’s other blogging software built on Rails, but I haven’t had time to review them thouroughly yet, and I don’t seem to think they’re good for what I want.

Besides the slow speed, Typo hasn’t been updated in quite a while. The last update was in August of last year. Now that normally wouldn’t be a problem. But I think that at the very rapid pace Rails is evolving, most Rails developers need to keep up with this pace. Granted, that’s not always possible. But with all the new additions Rails gets with each release, I’m sure Typo and other Rails software could greatly benefit from those changes.

So for the time being, I’ll stick with Wordpress. It’s relatively fast and stable, and very easy to manage. Can’t ask for more. Maybe in the future Typo will be updated, or I’ll switch hosting companies and find Typo to be great, or maybe there’s some blogging software out there built on Rails that can solve this minor dilemma for me. For now, it’s too bad all my site can’t be built on Rails.

Brand-Spankin’ New Wordpress!

Posted on February 21st, 2007 in Software | Comments

A new version of Wordpress, the awesome blogging software used to power this very blog (and millions more out there, as well) is out!. It’s actually just a bug-fix release, but it’s still news, isn’t it? This will be a quickie post, just to let you people know how things have gone with this upgrade.

To tell you the truth, I didn’t think it would be so easy upgrading this software. But it’s basically all in one step, if you don’t count backing up your data. This is very important whenever you do a software upgrade. All I did was back up my original Wordpress installation folder on the hosting server, and backed up the MySQL database as well. I won’t go into specifics, as I don’t want to make this post too technical. Also, as a precaution, please de-activate all of your currently activated plugins. This is not necessary, but it’s nice to do this, just in case one of your plugins is incompatible with the current version of Wordpress, possibly breaking your installation.

Afterwards, I downloaded the new Wordpress release, decompressed it, and copied over my old installation directory. Then, all you need to do it go to a special upgrade page in your site (http://your.domain.com/wordpress/wp-admin/upgrade.php, where http://your.domain.com is your domain name (obviously) and http://your.domain.com/wordpress is the main page of your blog. A screen will appear, asking you if you want to go into the upgrade process. Click on the ‘Upgrade’ link, and that’s it! I thought it would be a long, drawn-out process, but it wasn’t. You canj then visit your shiny-new blog! Actually, it’s not that shiny, since it’ll be basically the same. But I’m sure you’ll feel comfortable knowing your blog is running the latest software,, with (hopefully) less bugs and insecurities as before, right?

All of these instructions can be found at the Wordpress website. So why did I bother to post this? I guess I wanted to let you all know that this is an easy upgrade, and that I’m up-to-date!

Edit: Seems like a hacker got into the previous Wordpress release, and modified some files to allow remote code execution. Just in case, if you have Wordpress 2.1.1 installed in your site, there are chances that you have one of these “infected” releases, and should seriously upgrade to Wordpress 2.1.2 now.

Be ready for Intype

Posted on February 16th, 2007 in Programming, Ruby On Rails, Software, Web Development | Comments

Last year, after a friend of mine lent me here Macbook for a week (I want one for myself so bad!), I discovered the most amazing programming software I had ever found, called TextMate. I’m sure many of you have heard about this program. In fact, if you have watched any of the Ruby On Rails screencasts, you know what software I’m talking about. I think it strikes a balance between simplicity and power that’s rarely seen, and that I never even thought was possible. I tell you, once you get used to their snippets function, you’ll never want to try another IDE again.

So I went off, searching for a nice alternative for Windows that would work similarly. Shockingly enough, there wasn’t any program out there that was even similar to TextMate. I thought that was pretty weird. But after doing enough searching, I found a place where some people were making a program for Windows that would basically be a TextMate-like clone for Windows. Unfortunately, the program was only in its initial phase, with no alpha released yet. Sadly, I thought this program would just die out like many other software projects I’ve seen before, never to see the light of day.

But early this year, I received an E-Mail, stating that the very first Alpha release was available for download! Giddy, I went to their site, and lo-and-behold, there it was. I would finally get to use the program known as Intype. I rarely get excited for a software release, but this was an exception.

So I tried out the program, and it worked fine. Of course, as all alpha software, it had its fair share of bugs, and most of the functionality found in other text editors was missing (more noticeably, an Undo / Redo function). Still, it showed lots of promise, and many other people in their forums shared the same excitement as I did.

After a couple of more releases, there is finally a very usable Alpha version - one with an Undo function - available right now. I’ve been using it while I’m learning Rails, and it’s helped greatly, in terms of writing code much faster. I won’t dive into specifics, as there are already a couple of reviews out there in the wild. Even though it’s an Alpha release, it’s coming along really nicely. If the project continues forward, and all the proposed features are added, this will truly be a blessing for the developers - like myself - that are stuck at work with a Windows environment.

Intype

If you have a chance, download the most recent version and give it a spin. Besides helping with bug reports, you’ll probably be pleasantly surprised at the current state of the project, and you’ll certainly be excited as to where it’s headed. Be ready.