8 posts tagged “ruby”
If you find yourself writing text that you’ll eventually post to the web, hopefully you’ve heard of text-to-HTML translators like Textile and Markdown. These tools take plain text formatted with a very light syntax of special characters that can generate formatting like bold text, bulleted lists, and so forth.
I am a big fan of Textile and use it frequently when I set out to write something new that I plan to post to the web (like this post). I’m coming around to see the value in Markdown, however, because it does a better job of taking existing text and translating it to HTML without any manual cleanup. It works especially well when copy-pasting text in e-mail that you want to post online.
Both of these are very useful when writing web applications, since they allow you to convert plain text input from the user into something formatted when displayed in the browser. I’ve used them both from within Ruby scripts, but unfortunately there seems to be an issue with the Ruby gem port of Markdown (BlueCloth). The standard gem install does not seem to do what’s necessary to allow you to use BlueCloth in your own apps or from the command line. (RedCloth doesn’t have this problem.)
So, here’s what I did to get around it…first, install the gem.
sudo gem install bluecloth
If you hit an error on that step, follow this tutorial for installing Ruby on your Mac. Then, run the BlueCloth installer.
sudo ruby /usr/local/lib/ruby/gems/1.8/gems/BlueCloth-1.0.0/install.rb
At this point you should be able to use BlueCloth from within your own Ruby scripts. But let’s take it a step further and enable the bluecloth command line app.
sudo chmod +x /usr/local/lib/ruby/gems/1.8/gems/BlueCloth-1.0.0/bin/bluecloth
At this point it’s runnable, but it still needs to be added to your PATH. Add the following line to your .bash_login file:
export PATH="/usr/local/lib/ruby/gems/1.8/gems/BlueCloth-1.0.0/bin:$PATH"
Woo hoo! You can now use BlueCloth in your Ruby apps and call bluecloth directly on the command line.
Note: According to the documentation, RedCloth is supposed to be capable of rendering Markdown-formatted text via additional options. I could not get this to work, and from what I have read not all of the Markdown syntax is supported anyway.
Not long ago, I switched to TextDrive after being let down by a cheaper web host. So far, it’s been great…it’s a lot more feature rich and the people over there really know what they’re doing.
The basic $8/month plan includes access to 5GB of file storage for backup/sharing/whatever. I use it to back up regular ol’ documents and files on a daily basis.
They make this easy to do with a UNIX tool called rsync that compresses and transfers only the files that have changed. rsync is the real workhorse here, but I’ve rigged up a backup solution that utilizes all of the following tools:
- rsync – does the actual file transfer
- ssh keys – used to avoid entering manual passwords, which enables scripting
- Automator – ties together the scripts into an application
- iCal – enables scheduling of the application
- Growl – provides notification onscreen that the script ran
- Highrise – keeps a running history of the activity and provides RSS access
To cut to the case, download this file and check out the scripts. For more detail, read on.
This idea uses the UNIX philosophy of employing many small tools each perfectly suited for their job. (I love it.) Let’s take it step by step.
- rsync – It’s easy to familiarize yourself with this portion, since you can run it “interactively” on the command line. I do something very similar to what this gentleman did, though I supply slightly different options to rsync as per the recommendations of the Strongspace folks.
- ssh keys – If you play with rsync for a while, you’ll see that it asks for a password when you run it. Since we want to script this, we can’t have that, now can we? Fortunately it’s easy to generate ssh keys, which basically make your computer and your Strongspace account “trust each other” so that checking ID isn’t necessary.
- Automator – This part is pretty easy…I use Automator in OS X to string together the basic sequence of steps: backup the files, send the log to Highrise, and flash a Growl notification. Download this file to check it out.
- iCal – I chose to use iCal to schedule the Automator action (using this basic method) rather than a daemon like cron because it seemed like a more natural, Mac-like fit.
- Growl – I’ll be honest, this step is mostly for fun…Growl is just good times. However, I do use it as a quick validation that iCal lauched the script.
- Highrise – Growl is cool, but it didn’t provide much in the way of detail as to what happened with the script. I kinda wanted to see a log of what happened, except I didn’t feel like managing log files. I was really looking to get the log info via RSS, and for that I had to post it to the web somewhere. Highrise solves all these issues, and the default Dashboard view is great in the way it highlights the latest info. (After all, I really don’t care about yesterday’s backup after today.) My script e-mails the contents of the log file to my Highrise Dropbox, which was really easy to do with just a few lines of Ruby code.
So far, this has worked without a hitch, and I feel much better having my files backed up everyday.
I've gone back to putting everything into 37signals apps after a brief honeymoon with other tools. The 37signals stuff just works too well to use anything else.
My current approach is to use Highrise for work stuff and Backpack for personal stuff...it's a pretty good split. I find that work is more people-oriented (call this person, e-mail this person, etc.) and home stuff best organized by keeping track of lots of little lists and bits of data. It's also kinda nice to keep them separated so you're not thinking about work all the time.
Anyway, besides keeping lists, I also get a lot of use out of Backpack's reminders at home. It's great for reminding you about little domestic things like picking up stamps and paying your credit card bill. My problem is short term memory...if I think of it, I've got to write it down immediately. This is especially true if I'm on the go.
To deal with this problem, I've written a little bit of Ruby code that creates a reminder when I send an e-mail to a certain address. I did something like this a while ago, but at the time I didn't have a way to run the code without constantly polling a POP account for new messages (which is no good). Thanks to the fact that I've now switched to TextDrive for web hosting, I can call my script when an e-mail comes in, which is much more reliable. (The message is fed to the script via good ol' STDIN...nice.)
The script tries to be a bit clever and interpret text like "Call Mom tuesday 5pm" into a reminder with the text "Call Mom" that will fire next Tuesday at 5pm. Take a look at this to see other examples of input it will interpret, but please ignore the sucky POP-related code and just go with this simpler version if at all possible.
The Rails apps I've written are deployed on a shared host, which is fantastic because the sysadmins can come around at any moment and pull the rug out from under me by changing something in the configuration. Yesterday, I logged into one of my apps only to be greeted by a wrong number of arguments error. Not cool.
After a bit of Googling and investigation, I noticed that Rails had recently been updated to version 1.2.3 for compatibility with the newly released 1.8.6 version of Ruby. My hosting provider had updated Ruby, but not Rails. Also not cool...as far as I can tell this will break any Rails app that uses a MySQL database, which would be almost all of them.
Luckily, there's an easy way to get your Rails app back in business...just freeze a copy of the correct version of Rails into your app. (Basically, this will copy all of the Rails code into the vendor/rails directory of your app.) In my case I "upgraded" to Rails 1.2.3, because that's the version that works with the Ruby install on my host. You could also "downgrade" and keep an older version for backward compatibility.
All in all, this was pretty easy to fix once I figured out what to do, and it was a good learning experience. However, I will still be notifying my web host of this issue, because I think they should not be so quick to update the software without considering the consequences.
Computers are such a pain in the ass sometimes.
Excuse me while I jot this down for myself:
require 'uri'
enc_uri = URI.escape("http://example.com/?a=\11\15")
puts enc_uri
# => "http://example.com/?a=%09%0D"
puts URI.unescape(enc_uri)
# => "http://example.com/?a=\t\r"
UPDATE: I have switched web hosts and can no longer host this Rails app in a running form. You can still download it and use it yourself.
--
I guess it is a small world after all. About two months ago I created a FeedFlare to make it easier to post your articles to digg. Wouldn’t you know it, I’m surfing the web a few days ago, minding my own business, and I see that not only is someone else actually using this FeedFlare, but posting the item to digg caused a huge traffic spike to his web site! As John ‘Hannibal’ Smith used to say, “I love it when a plan comes together.”
So, I was inspired to create another FeedFlare…number 56 on the list of 101 Flares: Feed Circulation. If you’re lucky enough to get your item on the digg home page, this is an easy way to watch your numbers go through the roof. ;)
This FeedFlare is dynamic (meaning there’s actual code that makes it work) and uses FeedBurner’s Awareness API. So, before you use it, you’ll need to enable Awareness API access to your feed.
How can I use this for myself?
It’s easy:
- Login to your FeedBurner account.
- Go to the
Optimizetab. - Click
FeedFlarein the left menu. - Copy/paste this URL (http://rossbelmont.com/feedcirculation.xml) into the text box under
Personal Flareand clickAdd New Flare. - Scroll down to the bottom and click
Save. (This is easy to forget.)
This doesn’t seem to work for me.
I tried to keep this pretty simple to avoid any problems, but, hey…stuff happens. To that end, I wrote this in such way that you can “debug” this a little with any web browser. If you’re having issues, hit this URL:
http://www.rossbelmont.com/feedcirculation/↵ circ?feedUrl=YOUR_FULL_FEED_URL_HERE
So, in my case, this looks like:
http://www.rossbelmont.com/feedcirculation/↵ circ?feedUrl=http://feeds.feedburner.com/Arbited
(Make sure to include the full URL.) You might see an error message like the following:
Error occured (1): Feed Not Found
The error code 1 and the message Feed Not Found are returned from FeedBurner and can be looked up on this page. They’re informative, if not exactly verbose, and they should help you pin down what went wrong.
If you see a tiny snippet of XML that tells you how many subscribers you have, you’re good to go.
How does this work?
Three words: Ruby on Rails. If you’d like to look at the code (or run it on your own server instead of mine) you can download it here.
It’s pretty straightforward, really…it grabs your feed URI from the full feedUrl and makes a call to the FeedBurner Awareness API to pull up the number of subscribers. That number is embedded in the XML the code returns, which is then parsed and displayed in your feed or on your site.
To give credit where credit is due, the code to parse the XML returned by FeedBurner was aped from the Ruby wrapper for the Backpack API, written by DHH himself.
I hope y’all like this…I’d love you get some feedback on this so please post a comment if you have any.
Let me set the stage a little bit: I was playing around with 30 Boxes and I had an idea for how to create Backpack reminders via e-mail or SMS. I threw that together without too much hassle and I was feeling pretty good about myself.
So, a little later on, I was looking at 30 Boxes more in depth, and as I was contemplating ditching iCal I was thinking that one thing I would miss is my Backpack reminders being embedded into my calendar. Backpack lets you “subscribe” to your reminders as another calendar using the iCalendar format.
Lo and behold, I was poking around in the 30 Boxes settings when I discovered that it has the ability to display RSS feeds in an embedded manner in your calendar. It was at this point when the wheels started turning. ;)
So, I whipped up another little Rails app to serve my Backpack reminders as an RSS feed. I’ll admit it’s somewhat underwhelming to simply read this feed in your aggregator. But, if you plug it in to 30 Boxes, it’s pretty sweet.
How can I use this for myself?
Unlike my previous Backpack API hack, this one basically requires that you have a web hosting account with a provider that supports Ruby on Rails. If you’re in the market for one, I’ve been really happy with my experience at A Small Orange. I’ve also heard good things about Textdrive.
The other thing to watch out for here is security. You don’t want anyone who can guess the URL to be able to read the feed. So, I embedded a randomly generated security token in the URL that is “unguessable” for all intents and purposes. Also, I avoid storing that token in the source code in clear text by using a standard encryption algorithm. Just keep in mind you’ll need to deal with this.
To use this, the steps are more or less as follows:
- Download the Rails app (reminderfeed.zip, 64 KB).
- Edit the file
app/controllers/feed_controller.rbto include your Backpack username and API key. - Choose a security token to be embedded in your feed’s URL. In this example, let’s pretend it’s
mytoken. - Generate the SHA-1 hash of your security token using this Ruby script, and put this value in the appropriate spot on line 6 of
feed_controller.rb. - Upload the Rails app to your web host and test the URL. It should be something like
http://myhost.com/reminderfeed/feed?sectoken=mytoken.
That’s all there is to it.
How does this work?
The code here is so dead simple, it’s almost sample code. Go ahead and poke around in there if you’re interested in how it works…the files of interest are app/controllers/feed_controller.rb and app/views/feed/rss.rxml.
If you look at rss.rxml, you’ll notice that I ran into a time zone gotcha because my web host is in a different time zone then my Backpack. If anyone has suggestions on how to improve this, holla atchya boy.
So, what’s next?
Word on the street is that Backpack will grow a calendar of its own, and maybe I’ll be able to throw this code out the window. No biggie…it was still a good learning exercise and it took maybe an hour tops.
It’s no secret that I’m a huge fan of Backpack. And though it’s great to store all my miscellaneous info and personal mini-projects in one central location, I’d forget to do anything without Backpack’s reminders feature. It’s simple and it works beautifully.
The one problem I have with Backpack reminders is that I almost never think of the things I need to be reminded about when I’m sitting at a computer. And, unfortunately, the browser on my phone just takes too long to load, or else I’d use Backpack’s mobile version more often. However, the GUI on my RAZR isn’t so crappy that I can’t dash of a quick SMS before I forget my critical reminder-worthy thought.
So, what I’ve done is whipped together a quick Rails app that gives me the ability to create Backpack reminders via e-mail or SMS. Here’s how it works:
- Realize that you haven’t called Aunt Betty in a while.
- Whip out your phone and type
call aunt betty sat aftinto a new text message. - Send the message to “remindme@someaddress.com”. This can be any e-mail account you have that supports POP3.
- Sometime later, the software I wrote picks up the message and sets a Backpack reminder for Saturday at 2pm with the text
call aunt betty. - Go about your business until Saturday afternoon, when your Backpack reminder fires off, and alerts you either by e-mail or SMS.
- Call Aunt Betty and stop feeling guilty.
You can use any e-mail program to send a reminder message to the “remindme” address, which is handy when you’re in front of a computer. The SMS example above relies on the feature most cell phones have in which a text message sent to an e-mail address magically “becomes” an e-mail.
I got the idea for this after experimenting with 30 Boxes, a new online calendar app. They have a nifty little feature they call the One Box that allows you to add appointments to your calendar just by typing in something like dinner fri 6:30pm. It figures out when Friday at 6:30pm actually is and creates your appointment.
In my version of this, you can enter the date and time in either order. Here are some other examples:
Call Dave(reminds you in three hours)Pick up dry cleaning 5pm wed(time comes before date…that’s OK)Make doctor's appt tom(reminds you tomorrow at 9am)Pick up dinner 5pm(reminds you today at 5pm)Send in taxes 4/15 9:30am(You do pay your taxes, right?)Report for active duty 9/1/07 15:00(Sept 1st of next year at 3pm)Post files soon(reminds you in 10 minutes)
Those of you that use Backpack reminders already will recognize morning and afternoon corresponding to 9am and 2pm, respectively. I also added the keyword of night (or nite or even nit) to translate to 6:45pm. Call Dave later will work the same as Call Dave, reminding you in three hours. I also added soon to remind me in 10 minutes because you’d be surprised how quickly something flies out of my brain. Finally, you can enter the input text into the subject line of the e-mail or the first line of the body itself.
How can I use this for myself?
Unfortunately, I can’t host this application for you, so you’ll need to download and run it yourself. But, on the plus side, you don’t have to pay for your own web hosting account either. All you really need is a computer that can run Ruby, and a way to kick off the script to run every once in a while.
Here’s what you’ll need to do, in no particular order.
- Obtain a POP3 accessible e-mail address you’ll use for this purpose.
- Download the Rails app itself (mobilereminder.zip, 76 KB). Go here if you don’t have Ruby on Rails already.
- Edit the file
lib/checker.rbto specify your e-mail configuration. - Edit the file
lib/reminder.rbto specify your Backpack account info. - Run the Checker every so often. I used
cronto execute the following command every five minutes:
/usr/local/bin/ruby mobilereminder/script/runner ‘Checker.run’
If you actually want to set this up but are having trouble, drop me a line and I’ll try to give you a hand.
How does this work?
Essentially, I set up a cron job that kicks off the process every five minutes and POPs off all the messages. It parses each one, sets a reminder if there’s a future date, and deletes the messages. It’s pretty solid, overall…I only got one error from the cron daemon about an issue accessing the POP mailbox that didn’t resurface.
The parsing was not that hard, really, because Ruby’s such a cool language and has so many great features baked right in. This definitely would have been a bigger pain to write in Java, severely reducing the fun factor. Also, I’ll note that this took me about a week of nights and weekends to put together, and I’m not a Ruby expert. Now that I think about it, that’s probably something worth repeating: I’m not a Ruby expert or a master, wizard-level coder. If you download the code and laugh at it, that’s OK. At the same time, if you have a suggestion for improvement or want to help me out, by all means let’s chat.
Speaking of master coders, I need to give credit where credit is due and point out that this was possible to put together because the good people at 37signals have released a really nice API for Backpack. But, more than possible I’d say it was easy to put together because they went one step further and created a sweet Ruby wrapper that made the Backpack calls drop-dead simple. The only way they could have been better is to add this feature into the official Backpack product and render my silly hackery irrelevant. (Hint hint!)
There’s one gotcha to this whole thing: since there’s no way to determine which time zone your Backpack is in through the API, I had to hard code some stuff in there to offset for Central time because my web host is on Eastern time. If you run this app from within the same time zone as your Backpack, you can delete the offset stuff from the code.
So, what’s next?
Well, I mentioned at the beginning that I got the idea for this by using 30 Boxes…my next app gave me a way to display my Backpack reminders on my 30 Boxes calendar. Check it out if you’re interested.