Got API?
August 29th, 2006
Just ran across this very useful AJAXified api reference while trolling through the RoR mailing list:
It has support for a ton of different languages including:
Ruby, RubyOnRails, Javascript, HTML, CSS, XML, Actionscript, Java, PHP, Perl, C, C++, and more…
I am pretty impressed with the interface from my brief experimentation. Very snappy. Check it out.
Runit cheat sheet
August 28th, 2006
Here is a nice RUnit cheat sheet courtesy of nubyonrails.
I am all about the cheat sheets, such as this handy prototype cheat sheet
Fetching Data The Easy Way with Ruby
August 23rd, 2006
I recently ran into some difficulties while fetching random html pages and images using ruby so I thought others may benefit from my experience.
Ruby has two (and probably many more) great libraries for fetching http data:
OpenURI has a much nicer API and takes care of things like http redirects for you. However, if you try to fetch data over HTTPS, you will see a message like Certificate Verify Failed.
To get around this limitation I’ve written this simple method for fetching data that will enable ssl when necessary and follow all redirects:
def self.get_data(url, limit = 10)
raise "Too Many Redirects" if limit == 0
uri = URI.parse url
server = Net::HTTP.new uri.host, uri.port
server.use_ssl = uri.scheme == 'https'
server.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = server.get uri.request_uri
case response
when Net::HTTPSuccess then response['host'] = uri.host; response['path'] = uri.path; response
when Net::HTTPRedirection then self.get_data(response['location'], limit - 1)
else
response.error!
end
end
Also, you will notice that I add the host and path into the response since in my case I use that information for building the full path to the images.
Enjoy!
The Gift Hat blog
August 9th, 2006
I moved the main GiftHat discussion over to the shiny new Gift Hat Blog. So, if you’d like to stay up to date, please stop by.
A new Gift Hat release
August 3rd, 2006
I just snuck out another GiftHat.com release which includes two great new features:
- You can publish your wishlist right on your website or blog. See My Wishlist on the right as an example.
- I added a new feature to make it easy to select an image for your gift. Just type in a website for your gift and I will fetch all the images from that page and let you select one to go with your gift:

Also, pretty soon I will be adding a blog to the Gift Hat so I can move the majority of these types of announcements over there.
I am going to try to keep this blog more focused on web development using RubyOnRails and javascript, but I will include lots of examples from my projects. If you would like to know how I did any of the features in the GiftHat.com, just drop me a note and I will add it to my list of topics to discuss here.
CodeRay Syntax Highlighting for Typo
August 1st, 2006
NOTE: This blog is no longer running Typo
In this article I am going to discuss the steps necessary to add the excellent CodeRay syntax
highlighting library to your Typo blog installation. The newest version of Typo ships with
support for the syntax gem, but I have found that the
CodeRay gem does a much better job and supports a lot more languages including:
- ruby
- C
- Delphi
- HTML
- RHTML
- Nitro-XHTML
Install CodeRay
There are a couple different ways you can install CodeRay:
- By gem
- In the typo/vendor directory.
For my case, I chose to install CodeRay using the vendor directory since the gem way
is not ideal for a shared hosting environment.
To install CodeRay into your typo/vendor directory:
# Either download the CodeRay source into your typo/vendor/coderay directory or setup
an svn:externals for CodeRay. To setup the svn:externals, type this from the root
of your typo install:
svn propedit svn:externals vendor
and add this line into the externals for the vendor directory:
coderay svn://rubyforge.org/var/svn/coderay/trunk/coderay/trunk/lib
and then update your CodeRay src with this command:
svn up vendor
Create the CodeRay text filter
Now we will create a custom Typo text filter for sending code through CodeRay.
NOTE: This requires either Typo 4 or greater, or an earlier unreleased Typo that supports
text filters.
If you want to skip the explanation, here is a patch file
that you can apply from the root of your typo installation:
patch < typo.coderay.diff
This diff contains the following four changes:
- A modification to the typo environment
- A CodeRay text filter
- A CodeRay css stylesheet
- A modification to the add the CodeRay stylesheet to the typo theme
A modification to the typo environment
In order to have typo load the CodeRay code into our rails environment, we needed to add
the vendor/coderay path into the config load paths. The patch will do this for you.
A CodeRay text filter
The CodeRay text filter is very similar to the Code text filter. It is impressive how
little code it takes to add this CodeRay functionality into typo. All of the work for this
text filter is done in this method:
def self.macrofilter(controller,content,attrib,params,text='')
lang = attrib['lang'] || 'ruby'
tokens = CodeRay.scan(text, lang)
result = tokens.div(:line_numbers => :table)
'<notextile>#{result}</notextile>'
end
A CodeRay css stylesheet
After applying the patch, the CodeRay css file will be located here:
public/stylesheets/coderay.css
A modification to the add the CodeRay stylesheet to the typo theme
The patch above will apply the CodeRay css to the default azure theme. You can manually
add it to any theme by just adding this line to the top of your theme within the tags:
<%= stylesheet_link_tag "/stylesheets/coderay", :media => ‘all’ %>
Using the CodeRay text filter
To use the CodeRay text filter in your code, surround any code you wish to display in
typo:coderay tags and optionally specify the language to format (the default is ruby).
Here is a simple example (Note: remove the space between typo and coderay, I only added it so it would not be grabbed by the coderay text filter):
<typo: coderay lang="html">
<div id="somediv">The div that I am</div>
</typo: coderay>
Subscribe to the feed!