Merb, Safari and “Cannot decode raw data”
Today I was attempting to get Merb to output a comma-delimited file of data dumped from the database. In config/init.rb I’d added:
Merb.add_mime_type( :csv, :to_csv, %w[application/csv], "Content-Encoding" => "gzip")
but I kept getting this error from Safari: cannot decode raw data.
Turns out the gzip encoding was mucking up the data. Using this line instead produced the desired outcome:
Merb.add_mime_type( :csv, :to_csv, %w[application/csv] )
Detecting DateTime timespan overlap in Ruby
The Scenario
Let’s say you have an instance of DateTime that represents a starting time, and you have a duration that, when added to the starting time, represents an ending time.
Let’s say you have two such things and you want to know if they overlap (or intersect, or collide). For instance:
- March 29, 2009 @ 10:00am; 120 minutes
- March 29, 2009 @ 11:00am; 90 minutes
In this example there’s one hour of overlap in which #1 stretches from 10:00am – 12:00pm and #2 stretches from 11:00am – 12:30pm.
How to detect this? I’m sure there’s many clever ways, this way is mine.
We’re Gonna Need More Monkeys
First, we need to monkey-patch some core classes. Monkey-patching is fun, it makes us feel 31337.
As an aside: it’s patently absurd that DateTime doesn’t have a built-in function for converting to a Time instance (which is layered atop the patently absurd need for Ruby to have three unique Date/Time classes… but that’s another rant)….
Anyhow, let’s monkey-patch DateTime so it can return itself as an instance of Time:
class DateTime
def to_time
return Time.mktime( year, month, day, hour, min, sec )
end
end
Now we need to alter Range to detect intersections with other ranges the way Array does with its & operator. We do this with Bill Siggelkow’s clever intersection() method:
class Range
def intersection(range)
res = self.to_a & range.to_a
res.empty? ? nil : (res.first..res.last)
end
alias_method :&, :intersection
end
Ta-da; monkey-patching complete.
DateTime is the VB.Net of Date/Time Classes
You’ve likely inferred from the code above that we won’t be using DateTime for this jaunty action, but rather instances of Time. You’d be correct. DateTime is remarkably unsuited for this sort of thing. But since DateTime is so popular and prevalent that’s where we’ll start.
Our first moment of comparison will start right now to 60 minutes in the future:
a = DateTime.now a_start = a.to_time a_end = a_start + ( 60 * 60 )
Our second moment in time will start 30 minutes from now to 120 minutes in the future:
b = DateTime.now b_start = b.to_time b_start = b_start + ( 30 * 60 ) b_end = b_start + ( 120 * 60 )
Free-range Comparison
Now that we have our times, let’s convert them into ranges:
# Turn these into ranges a_range = (a_start.to_i..a_end.to_i) b_range = (b_start.to_i..b_end.to_i)
and compare them:
puts "Result A-B: #{a_range.intersection( b_range )}"
As of this writing the output is:
Result A-B: 1238340860..1238342660
which indicates an overlap/intersection/collision between the two timespans, displayed as seconds. Had there been no collision, nil would have been returned instead.
Where’d the Comments Go?
After much consideration I’ve decided to close comments on all posts on this blog. I do this for a number of reasons:
- Once again dealing with the comment spam has become far too annoying. Akismet has been great but enough spam was slipping through that it had become a chore to manage each morning. Under different circumstances I might continue to put up with it but…
- The nature of this blog is such that for the most part the posts don’t generate comments-based discussion. I use it place primarily as a storage device for things I think might be useful later and often, via Google, other people find these posts and find them useful as well. Thus, most of the comments are of the “thank you” nature, which is immensely gratifying and I thoroughly enjoy them but massaging my own ego is not a good enough reason to wade through the spam each morning!
- And on the few posts that do warrant discussion or criticism I’m very much on board with reading your responses on your blogs (and thus I’ve left open trackback and pingback). It seems that people do tend to write more thoughtfully when writing from their own pulpit so if you’re so inclined then link back to me and let’s run discussions that way. It’ll be fun!
All that said, whether or not you’ve ever left a comment, thanks for reading!
Pagination in Merb
Pagination is one of those things that almost every site needs and for which there is absolutely no glory. Boring stuff, pagination, the Edmonton of software development. Which might explain why, when I needed to figure out how to paginate in Merb a little while ago, it was so difficult to find clear instructions on how to do so. When you find yourself in pagination you do what you have to do and then move on, with nary a glance back (apologies to both my readers in Edmonton, but you know I’m right.)
Oh sure, lots of posts on the web say “use Lori Holden’s dm-is-paginated with merb-pagination” but none say quite exactly how. And while I’m fully willing to concede I may be a little thick sometimes I found the examples on her pages somewhat under-enlightening. Hints of light bulbs but no switches, as it were.
So, embarking under the assumption that both dm-is-paginated and merb-pagination are installed on your system, here’s how I got pagination working with Merb. In this example I’ll use my DbAdminLog class as an example.
First, make sure you have the dependencies declared in dependencies.rb. At the time of this writing, these were:
dependency "merb-pagination", "0.0.1" dependency "dm-is-paginated", "0.0.1"
In the DbAdminLog model, I added this line underneath my property definitions and validations:
is_paginated
This provides the pagination methods to the model necessary elsewhere. If you don’t add this line, no pagination for you, which is funny because it isn’t mentioned anywhere on the example pages linked above ( “…on public display in the local planning department for months, locked in a filing cabinet in the darkened cellar behind a sign that says, ‘Beware of the Leopard’”.)
In the DbAdminLogs controller I added the following to the index() method:
def index @current_page = ( params[:page] && ( params[:page].to_i > 0 ) ) ? params[:page].to_i : 1 @page_count, @db_admin_logs = DbAdminLog.paginated(rder => [ :created_at.desc ], :conditions => cond, :page => @current_page, :per_page => 20 ) display @db_admin_logs end
The first line figures out which page of results we’re displaying, based on the GET parameter supplied (or not supplied, in the case of the default “1″.) The second line is the pagination magic in which I define the order to be displayed (they’re log files, so LIFO), any filtering conditions via the cond variable (if you want every record unfiltered you can leave :conditions out or replace cond with ["1"]), the page number and the number of entries to be displayed per page.
(For extra credit, swap out the && with andand and be 1337.)
That’s the controller.
The view is pleasantly straightforward. In index.html.haml I added the following at the bottom below the display code:
= paginate( @current_page, @page_count, :inner_window => 5 )
And there you have it: merb pagination.
Brutal But Honest: A Developer’s Code?
I wish I knew the original source for this quote. Anyone?
Duplicate code is the root of all evil in software design. When a system is littered with many snippets of indentical, or nearly identical code, it is indicative of sloppiness, carelessness, and sheer unprofessionalism. It is the guilt-edged responsibility of all software developers to root out and eliminate duplication whenever they find it.
DataMapper on Red Hat EL5
Last month I started rewriting the admin reporting section of one of my sites in Merb rather than refactor the existing PHP version, for a number of reasons. The pace of development with Ruby and Merb was tremendous and within about a week and a half I had a fully-functional, extensible reporting site developed, replete with graphs and statistics. DataMapper made tying into the legacy database, with it’s rather novel table and column naming schemes, trivial. It was all tremendously satisfying and, dare I say it for a project so unglamorous, fun.
And then I tried to get it to run on our production server, which is running Red Hat Enterprise Linux 5, and the fun went away.
I first tried installing Phusion Passenger (a product I’m absolutely enthralled by) but no such luck; it had a hell of a time with the default Apache install and lack of development headers and various file locations. I wasn’t willing to muck about with the production server in a vain attempt to force it to work at the risk of the rest of the site.
Instead I figured it ought to run just fine on Rack. And every dependent gem installed with nary a hitch save for DataMapper, specifically do_mysql, which would fail to build the native extension with the following error:
In file included from /usr/include/mysql/my_global.h:83, from do_mysql_ext.c:6: /usr/include/mysql/my_config.h:15:28: error: my_config_i386.h: No such file or directory
The solution was ultimately provided by Dan Kubb:
We probably should make it so that all the DO driver specs can be run on installed gems, but in the meantime, checkout the source from git using the following commands:
git clone git://github.com/datamapper/do.git cd do/data_objects sudo rake install cd ../do_mysql ... remove the references to my_config.h in do_mysql ... rake compile spec sudo rake installThis will first install the edge version of DataObjects, and then will compile and run the specs for do_mysql, and then install it. You will want to remove all references to my_config.h from the do_mysql C libs just prior to running the specs of course.
The really important bit here: “remove the references to my_config.h in do_mysql”. That’s the magic, and with that everything was good and right and Merb was run, and the users were happy.
(For the very curious, the entire DataMapper Google Groups thread.)
MacSpeech Dictate
this is an attempt to write a blog using nothing but voice recognition. The software in question is called MacSpeech Dictate which I’ve been using on and off for about the last two or three months. So far it’s extremely impressive; this post as you see it is exactly as MacSpeech Dictate has translated it without any manual corrections made by me.
So far the hardest part of using MacSpeech Dictate has been cognitive; I still find it very difficult to compose speak and read at the same time, for the process of dictation is very different in the process of type. That last sentence should read: “… very different than the process of typing.”
In a nutshell, while it’s taken me probably four or five times longer to compose this post by speaking rather than typing it is pretty cool not to touch the keyboard at all. And for someone who’s recovering from an RSI MacSpeech Dictate just might be the ticket.
(I’m very impressed that recognized RSI)
Smarty Gotcha: Spaces
One of the projects I’m working on uses Smarty as its templating engine. Frankly in a post-Rails/Merb/CakePHP/Django world Smarty is pretty painful to work with and its age and implementation restrictions become readily apparent… but that’s another post for another time.
This post is about how spaces in Smarty templates might trip you up. Witness the following code from a Smarty template (yes, this is kind’a like PHP but less functional and less readable):
<img src="{$deletion_item->default_image(false)}" />
<img src="{$deletion_item->default_image( false )}" />
One would tend to think, or at least I tend to think, that those two lines of HTML/Smarty code would be functionally equivalent. They look the same, save for the second developer’s preference for some extra whitespace.
However example one works with Smarty, example two throws the following error:
Fatal error: Smarty error: [in /Users/chris/Sites/dreambank/templates/en/delete_item.tpl line 42]:
syntax error: unrecognized tag: $deletion_item->default_image( false ) (Smarty_Compiler.class.php, line 446) in /Users/chris/Sites/dreambank/includes/smarty/Smarty.class.php on line 1095
So, there you have it. There’s not much more to this than to say: when working with Smarty be very careful with your whitespace.
Mephisto 0.8.2
Last week Eric Kidd announced the latest version of Mephisto, the Rails-based blogging system, has been released:
Mephisto 0.8.2 is now available on the download page!
Mephisto’s JavaScript is in much better shape, and most of the remaining “tainted string” errors should now be fixed. The default article and comment filter is now Textile (instead of raw HTML), and our gem management has been cleaned up.
I mention this here, vainly, because I’m cited in the contributor notes, having fixed a couple of very minor bugs.
Mephisto still has a long, long way to go before it can lay claim to rivaling the likes of WordPress but it appears to be coming along slowly but surely ( “Don’t call me Shirley”). At the very least I can say without hesitation that the Mephisto source code is much nicer to work with that that of any other blogging platform I’ve had to dig into; in fact, compared to hacking WP, Mephisto is pure pleasure.
To check out the blog I’m running on Mephisto, take a look at Beers ‘n Booze, a blog dedicated to drinking quality beer in Vancouver.
Build a Business on Twitter? Really?
In an otherwise excellent rebuttal to a rather ill-thought-out complaint about Twitter rate-limiting their API, Marco misses what I think is the main reason for not building a business completely reliant on Twitter: Twitter has no business model; Twitter is burning through cash; Twitter could quite literally cease to exist by this time next year.
The original complaintant states:
I’m arguing that 20,000, or any request-rate limit for that matter, limits any app out there from being able to develop on the Twitter platform, and I don’t see why any able-minded entrepreneur would want to build on it if there’s such a rate limit in place.
I’m all for digging at the edges to find hitherto undiscovered business opportunities but frankly if your business model involves 100% reliance on a flaky, profitless company to provide you your platform and the life-blood of your enterprise then Twitter isn’t the one screwing your business, you are. Repeat after me: “Twitter owes me nothing. Twitter owes me nothing.”
In the end, Marco nails it:
Even if they call their services “application platforms” and you call your business “new media” or a “mashup”. Building a business exclusively on top of another service is irresponsible and naïve.
To better understand the ramifications of this sort of odd behaviour I highly recommend reading Fuck The Cloud by Jason Scott.