Apache mod_rewrite and Ruby

December 30th, 2010
by Mark Lorenz

The Apache loves Perl, but I love Ruby. Below is an example mod_rewrite rewrite map written in Ruby.

httpd.conf will need to look something like this:
RewriteEngine On
RewriteMap my_map prg:/path/to/my_rewrite_map.rb
RewriteRule some_input_regex_with_two_matched_patterns http://some_rewrite_output/${my_map:$1}$2 [optional_flags]

Of course your matched patterns(i.e $1) and output format will probably have a different form

and my_rewrite_map.rb looks like:

#!/usr/bin/ruby
#
# Turn off buffering
STDIN.sync = true
STDOUT.sync = true

# Enter infinite loop
while (STDIN)
line = gets
if line =~ /my_regex/s
puts "desired_map_output\n"
else
puts "\n"
end
end

Of course you’ll probably want to do something a little more complex than a 1:1. For all things mod_rewrite I strongly recommend “Definitive Guide to Apache mod_rewrite” from Apress by Rich Bowen. It’s much easier to understand than the apache docs, and tips you off to a lot of the gotcha’s.

For comparison sake, similar code on Perl would look like:

#!/usr/bin/perl
#
## disable buffered I/O which would lead
## to deadloops for the Apache server
$| = 1;
#
## read URLs one per line from stdin
while (<>) {
my $line = $_;
if ($line eq "input_from_apache\n"){
print "my_desired_output\n";
}
else{
print "\n";
}
}

Posted in Uncategorized | Comments (0)

Calendar pop-up in Ruby on Rails (v2.3.8)

July 2nd, 2010
by Mark Lorenz

I did some serious hunting to find a good, user-friendly, date entry method for rails. The following is the best solution I could find and put together.

First we are going to install jQuery. I had been leaning towards jQuery form DOM manipulation for a while, and this issue was the tipping point.

  1. Download the latest jQuery.
  2. Copy jquery-<version number>.js to public/javascripts
  3. Download jQuery UI
  4. Open the folder that downloaded and copy:
    1. jquery-ui > js > jquery-ui-<version number>.min.js to public/javascripts
    2. jquery-ui > css > <theme name> > jquery-ui-<version number>.css to public/stylesheets
    3. jquery-ui > css > <theme name> > images > * to public/images
  5. Now the fun part, open up jquery-ui-<version number>.css and find each instance where an image is referenced and put a ‘/’ in from of the path, and surround the whole thing in single quotes.  For example:
  6. url(images/ui-bg_flat_75_ffffff_40x100.png)
    becomes 
    url('/images/ui-bg_flat_75_ffffff_40x100.png')

    There should be 20 or so instances.  Yes, this is very fragile and will break if you update the .css file, but it’s an easy fix.  The reason we are doing this has to do with the way Rails is routing the resources for you behind the scenes.  Without this change Rails will look for the images somewhere they don’t exist.

  7. Now that we have the files we need, we can setup our app for jQuery.  In application.html.erb we need to add the include tags for the javascript and the css.  They should look something like:
  8. <%= javascript_include_tag 'jquery-1.4.1', 'jquery-ui-1.8.2.custom.min', 'application', :cache => true %>
    <%= stylesheet_link_tag 'jquery-ui-1.8.2.custom', :media => 'screen' %>;

  9. We’re going to setup noConflict so that everything with prototype will still use the alias ‘$’ and jQuery will use ‘jQuery’.  in application.js add the line
  10. jQuery.noConflict() //relinquish $ to prototype

  11. Everything is setup at this point.  Now for the final touch, a nice date select pop-up.  In whatever .html.erb file you like to have the date picker appear in add the lines:
  12. <%= javascript_tag "jQuery(function() {jQuery(\"#datepicker\").datepicker();});" %>
    <p>Date: <input type="text" id="datepicker"></p>

  13. Ok, save and load your page.  When you click in the text box, the date picker should appear.  Use firebug or other inspector to make sure that no errors where thrown in the java script.  If you didn’t change the .css correctly, there will be a lot of complaining in the error log.

UPDATE: OR you can just install jQuery UI correctly, and save your self the find-replace hastle

Posted in Uncategorized | Comments (0)

Localization and SHA1() Digests

April 24th, 2010
by Mark Lorenz

I’m going to have to change the tag line of this blog from “Developing Software Sundown to  Sunup” to “The World is a Big Place, Not Everyone is Like You”.

For an upcoming application I need to include a SHA1 digest of a user generated string.  As I mentioned in a previous post on this blog, America is not the only country on Earth, and English is not the only language.  Unfortunately, PHP assumes ASCII string encoding when it generates SHA1 digests.  For the non-geek, ASCII is one way computers represent human-readable text in a computer-readable number.  The ASCII system is pretty old, and so only really considers non-accented latin characters.  This means the first time one of you enters “ğ” the code can break because ğ is not part of the ASCII system.  It is part of the newer UTF-8 system, which is why you can see it on your computer screen now.

Of course the solution to this is to convert the international UTF-8 character to an ASCII character, but this has to be done in a lossy way, such that “ğ” becomes “g”. Of course, the big limitation is that SHA1 digests are used to easily compare the same-ness of two things, and even though “ğoodie-ğoodie ğumdrops” is not the same as “goodie-goodie gumdrops”, depending on how lossy the conversion to ASCII is, it may have the same digest.

For the programmers out there, the code to do this (borrowed heavily from Craig Spitzkoff) is:

-(NSString*) digest:(NSString*)input{
	NSData *data = [input dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
	uint8_t digest[CC_SHA1_DIGEST_LENGTH];
	CC_SHA1(data.bytes, data.length, digest);
	NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

	for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
		[output appendFormat:@"%02x", digest[i]];

	return output;
}

Posted in Uncategorized | Comments (0)

New iPhone App, iDoStyle

April 17th, 2010
by Mark Lorenz

I was super excited, as dorky as that may sound, at the release of iTryItOn. But that pales in comparison to the way I feel about the app that was just approved in the app store on Friday, iDoStyle. So yes, I am nerd.

iDoStyle is the natural progression for iTryItOn. Which means I took all the innovative features like finger-trace-cut-out and found a new niche for them. Building on the framework I got to do a few things I wanted to do with iTryItOn but either 1) didn’t have the know-how or 2) didn’t have the will power.
First of these was a “social media bar” where the programmer can make a social media button for any social media service that deals with the business logic for that specific service, but defers (or delegates, as we say in cocoa) to the “media panel” for general things like the text and images the user wants to post.
The second was what I call my “scooby view”. A pun on the old Hanna-Barbera cartoons. If you were born in the ’80s you may remember watching Scooby Do walk and carry on a dialog with Shaggy. The background would scroll by as Scooby walked, and no sooner would some distinctive feature scroll off the back of the screen, than a very similar (the same) feature would appear at the front of the screen. That background scene must have been maybe one screen wide. I used that same technique and some accelerometer input to make some nice fluffy clouds that will float by endlessly. I’m giving myself a scooby snack for that one.

If you downloaded iDoStyle, let me know what you think of these features. I’d love to hear some outside opinions.

Posted in Uncategorized | Comments (0)

App Sales, a Three Week Review

January 9th, 2010
by Mark Lorenz

iTryItOn has been alive and well in the app store for approximately three weeks bringing a little joy to our lives each morning.  Everyday we get a short report of yesterday’s sales in our inbox by-way of a helpful script from Rouge Amoeba.

A small aside: Rogue Amoeba has some of the most talented Objective-C programmers working for them, heck, the venerable Mike Ash works there!  Mike has personally influenced our coding style here at Dapple Before Dawn, so they are just the company that keeps on giving.  Thank you Rogue Amoeba.

Now, back to that script and the reason for this post.  The first interesting sales trend has appeared, and we’d like to share it with you: The USA is not the only market for iPhone apps!

I personally feel a little embarrassed by this trend.  As CEO here it’s my job to think about these kind of things.  Heck, I even spent several years working for a Japanese company.  You’d think I would know better.  Our sales for the first three weeks break down by percentage like:

iTryItOn Sales By Country

iTryItOn Sales By Country

For the sticklers among you, yes I know that my pie chart only adds up to 99%.  That’s rounding for you.  So there you have it, the majority of our sales have actually been outside the USA.  Dapple Before Dawn takes great pride in our customer service, and responding to feedback quickly.  As far as this pie chart is concerned we could not have asked for any more clear feedback.  If you speak multiple languages and are interested in doing a localization for your native tongue, please contact as at: localization@black-box-technology.com.

“Doing a localization” means we would send you a text file containing all the English phrases used in iTryItOn, and you would write the translation for each.  It’s very simple, and there really aren’t that many words in iTryItOn anyway.

We look forward to hearing from you, in any language, and happy shopping!

Posted in Uncategorized | Comments (0)

iTryItOn Lands in the App Store

January 2nd, 2010
by Mark Lorenz

Our first app, iTryItOn was released in the app store the week of Christmas, 2009.  We are very excited about the prospect of having “actual users” since this app started it’s life as a technical demonstration a little less than six months ago.  We are also looking forward to all the feedback, and interacting with our users.  If you’re one of them, let us know what’s on your mind.

If you have interest, you can check back here periodically for a look at what’s going on inside the company.

Posted in Uncategorized | Comments (0)

  • Dapple Before Dawn