I run the Pow web server (http://pow.cx/) for local development of Ruby web apps. But I also like to prototype sites as static html templates early on in development. Fortunately Pow can handle this as it will serve up html (etc.) from a site’s /public directory.
So create your Pow site as usual but be sure to put the static html in a /public subdirectory, e.g.:
mkdir -p ~/Sites/prototypename/public
cd ~/.pow
ln -s ~/Sites/prototypename
Now you can access your site via http://prototypename.dev/ and it will serve up your ~/Sites/prototypename/public/index.html file.
Here’s a quick and dirty way to redirect requests for www.domain.com to domain.com in a Sinatra app:
before do
redirect request.url.sub(/www\./, ''), 301 if request.host =~ /^www/
end
I’ve finally found a reason to publish my own Ruby Gem. It’s called Tide and it simply fetches and returns Canadian tide prediction data from waterlevels.gc.ca.
http://rubygems.org/gems/tide
My plan is to turn it into a more ActiveRecord-like interface to the tide stations and their data as it evolves for use in a demo app:
http://waterlevel.heroku.com/
I recently had a need to get tide data from waterlevels.gc.ca but they didn’t offer a feed or data service so I had to build my own.
I wrote it in PHP to fit a specific project, but rewrote it in Ruby for my own use. If you happen to need Canadian tide data on your site check it out:
I recently had need for a simple referral tracking mechanism in a Rails application, what we used to refer to as promo codes at AOL. Here’s the method I chose for implementation.
In my application controller I’m capturing any ‘referral’ variable in the url as a session variable of the same name.
before_filter :capture_referal
private
def capture_referal
session[:referral] = params[:referral] if params[:referral]
end
This just writes a session variable called referral with the value passed in the url. Later, during checkout/registration I’m capturing the session variable as part of the order.
@order.referral_code = session[:referral]
@order.save
Lots of ways to make this fancier, but I love the simplicity of it. Referral codes can be generated for any affiliate by any of the sales and marketing staff, and there’s no need to set them up in advance. Regular order reports will show the code and allow searching for a specific code
I’ve very particular about the html I write. On top of writing to a strict doctype, keeping tags semantic and limiting the number of divs and classes, I also like to ensure the html is easy to read and looks great. Part of this is formatting indents to make nested elements easier to follow.
Unfortunately when I’m dealing with dynamic content those indents are often out of my control. Typically this will happen in a Rails project when running content through textilize. My solution was to add a very simple String method to add tabs to the beginning of each line.
class String
# adds tabs to the beginning of each line
def indent(tabs=3)
self.gsub(/^/, "\t"*tabs)
end
end
Now anytime I want to ensure proper indentation, I just call my indent method on the string, and I can even pass the number of tabs to use (defaults to 3):
<div class="section">
<div class="article">
<%= textilize(@article.body).indent %>
</div><!-- // article -->
</div><!-- // section -->
The above would output something like this:
<div class="section">
<div class="article">
<p>A formatted line.</p>
<p>Another formatted line.</p>
<p>Etcetera.</p>
</div><!-- // article -->
</div><!-- // section -->
Ruby is awesome. Regular expressions are certainly not my strong suit, so there may very well be a better way to gsub the tabs, but this is working well in my limited tests.