all things web and not web

allskonar

When I write web apps I try my best to keep my urls clean and legible. One thing that has always troubled me is how to handle label creation of named things web users key into the app. For example if a user enters a name of a restaurant Alicanté Café the uri safe for of that (if there is no intervention) ‘Alicant%C3%A9%20Caf%C3%A9’, which is total gobblygoo and one might as well show a uid or something of that nature since at least that doesn’t physically hurt the eye. As someone who has worked on a lot of Restaurant related apps this has always been something I wanted there to be an easy solution to. This weekend I decided it was worth trying once again to see what would work best. And after reading the “Internationalization in Ruby” in The Ruby Way. Here is the super simple trick


require 'iconv'
strange_name = "Alicanté Café" 
converter = Iconv.new('ASCII//TRANSLIT', 'UTF-8')
converter.iconv(strange_name)
"Alicant'e Caf'e" 

It leaves some odd ’ and “sfor some reason but they will get stripped away with all the other potential weird stuff out (”,’,etc). Still this discovery makes me stop complaining about pretentious foreign sounding restaurant names.

Using AWS::S3

Now setting a object to :public_read is trivial if it’s done when you store it to S3. That doesn’t seem to be the case if you want to change on an object that already has been stored privately. After banging my head against it a bit I finally “got it” and I figured I would share it to either show off or try to be helpful.

dial into s3sh session see url above for details on how to store keys in environment variables etc.

once in there get a bucket object

  • my_bucket = Bucket.find(‘foo’)

now get the s3obj that you want to become public

  • s3obj = my_bucket[‘object_name’]
  • policy = s3obj.acl
  • grant = ACL::Grant.new
  • grant.permission = ‘READ’
  • grantee = ACL::Grantee.new
  • grantee.group = ‘AllUsers’
  • grant.grantee = grantee
  • policy.grants << grant
  • s3obj.acl(policy)

The access control on the buckets must be very rich since it’s this tricky make things public. I must be overlooking something.

Bad Mephisto Move

July 10th, 2008

A while back I decided to get fancy and start hosting this blog on ec2 cloud just to sort of get the hang of working on that kind of a system. This of course involved moving database over and the works. Everything seemed to be going fine until I started posting… things were just busted bad. I kept getting

ActiveRecord::StatementInvalid (Mysql::Error: Duplicate entry ‘0’ for key 1

errors and for the longest time I just ignored it since I was able to post by using a some of my masterpiece drafts that I wasn’t going to use any how… but then I ran out of those and I felt an urge to post but that meant I needed to tackle the problem and as it turned out it was a simple fix after I had done the research. The primary key fields had lost the AUTO_INCREMENT magic during my db move and the solution was simply to go in and ad them where a appropriate.

ALTER TABLE `contents` CHANGE `id` `id` INT NOT NULL AUTO_INCREMENT;

It’s a continues struggle to become efficient in using VIM, especially if one ventures outside of what one regularly needs to do. It’s well worth it though since it might be the last text editor that you’ll ever need to master.

I was faced with having to produce pdf with selected code snippets.

Here is the fastest approach… be warned this example is for macs but I am sure it can work in other Li/Unix environments as well.

Install CUPS PDF and setup a virtual printer as is described on that page. Make sure that it’s the default printer after you have set it up.

The command to print in vim is :ha which is a short for :hardcopy this will spool the whole file to the print que for that driver and ultimately drop a pdf in cups-pdf folder on your desktop. To print out a selection simply highlight some text before issuing :ha command. I needed the line-numers of the code samples and this can be achieved by setting printoptions.

set printoptions=number:y

Now to be a complete terminal snob you should check out pdftk

Javascript Event Handling evolved

February 21st, 2008

Just this week, motionbox the company I work for released it’s first open source code the Motionbox EventHandler spearheaded by Topping Bowers with contributions from the rest of us in the group. The javascript library implements event delegation techniques and is built on the prototype library. There are other such libraries for yui which this was in part inspired by.

One of the kewlest thing about the library is the ability to subscribe dom elements to events before they even show up in the dom. Don’t take my word for it it was written up in ajaxian. There you have it: Topper, me and my colleges are almost famous now. If you are using prototype go and get the source and try it out it’s totally brilliant.

I have written about event handling before: The evolution of body onload Unobtrusive Javascript which might give some context on the topic of event handling in javascript.

It’s not uncommon that databases are filled with duplicate data sets. And it’s even more common that you want to clear it out so you don’t have to bothered with duplicates. This is obviously the sort of thing you want to fix at the entrance, on the way in to the database and not collect duplicates, but this solution works well if that is too late.

To get things started let’s write up a file that we can play with. I am using MySQL and I don’t know the SQL standard well enough to say but I am sure this example can be modified to work on other database servers. I generally just keep a file open and then retrieve it on the mysql console. Download the example file.


mysql>  \. dup_tutorial.sql

Creating a simple table to play with. And stuffing it with some data.

CREATE TABLE peoples (
  id int(11) NOT NULL auto_increment,
  f_name varchar(128) NOT NULL default '',
  l_name varchar(128) NOT NULL default '',
  age text NOT NULL,
  updated_on timestamp NOT NULL default
       CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO peoples (f_name, l_name, age) 
   VALUES ('baldur', 'gudbjornsson', 28);
INSERT INTO peoples (f_name, l_name, age) 
   VALUES ('baldur', 'gudbjornsson', 28);
INSERT INTO peoples (f_name, l_name, age) 
   VALUES ('baldur', 'gudbjornsson', 28);
INSERT INTO peoples (f_name, l_name, age) 
   VALUES ('baldur', 'gudbjornsson', 31);

In this sample we have 4 rows and but there are only 2 rows unique if updated_on and id fields are not taken into the account. Now the way to query this to only receive the unique set group by can be used or SELECT DISTINCT.


SELECT *
FROM peoples 
GROUP BY f_name, l_name, age

And here is the trick to collect the others to dispose of them. First we’ll join the peoples table with a unique rows from it self in a sub query to retrieve the ones that don’t match.


SELECT o_peoples.id 
FROM peoples o_peoples
LEFT OUTER JOIN 
  (SELECT id 
   FROM peoples 
   GROUP BY f_name, l_name, age) u_peoples 
ON o_peoples.id = u_peoples.id
WHERE u_peoples.id IS NULL;

Then we’ll delete those extra rows from the database. Do this at your own risk and obviously back up your database five times and have a quick recovery plan it things go south.


DELETE o_peoples 
FROM peoples o_peoples
LEFT OUTER JOIN 
  (SELECT Id FROM peoples 
   GROUP BY f_name, l_name, age) u_peoples
ON o_peoples.id = u_peoples.id
WHERE u_peoples.id IS NULL;

jslint from the terminal

November 29th, 2007

Once you get into the habit of using jslint it’s hard not to, it’s just so satisfying when it passes and all the lint has been cleared away. The first few times I used I went to the web application and pasted the file in there and looked at the results, fixed what was wrong and pasted it back into the original file. This is especially inconvenient if you use vim or emacs since pasting from an external environment has always been a bit of a challenge. But fear not, Douglas Crowford, best known for his work on the json spec, has an extended version that works with rhino the java javascript interpreter. The advantage of using jslint through rhino is that you can inspect your javascript files from the terminal. Installing it on a mac is trivial just follow Peters Michaux Install instructions

Here is the short version:


$> curl -O ftp://ftp.mozilla.org/pub/mozilla.org/js/rhino1_6R7.zip
$> unzip rhino1_6R7.zip
$> cp rhino1_6R7/js.jar /Library/java/Extensions/

Download the jslint file


$> curl -O http://www.jslint.com/rhino/jslint.js

And then you should be able to:


$> java org.mozilla.javascript.tools.shell.Main jslint.js your_program.js

Helma, server-side Javascript

November 8th, 2007

When you’re on a Ruby on Rails binge, it’s healthy to sober up every once in a while and checkout other things. Not necessarily because you want to re-saddle somewhere else rather to get a chance to learn new tricks and get fresh ideas. I’ve been been playing with Helma the javascript server-side project that was featured in the November issue of Linux Magazine.

The Helma server is build on the rhino javascript interpreter and the java based jetty server. Which means that it’s possible to use java libraries by placing .jar files into the lib/ext directory on the server, and it will automatically get added to the classpath. For example if you need a JDBC driver for MySQL you just get the jar file for Connector/J which is the official JDBC driver for MySQL, and place it in the lib/ext and configure it.

File: db.properties


myDataSource.url      = jdbc:mysql://localhost/blog
myDataSource.driver   = org.gjt.mm.mysql.Driver
myDataSource.user     = user
myDataSource.password = password

There is an obvious benefit by having the client-side code in a interpretive language like javascript, Ruby or any of the others out there, it gives the developer the flexibility of writing code rapidly. There is also a benefit of having it sit on java especially if you need to boost performance or accomplish something that javascript cannot handle. Using javascript on the server-side, presents an interesting opportunity in sharing code base both on the client-side and the server-side, which could, if done right, prove to be a powerful concept.

Using the same language on both the server-side and client-side should help with keeping the applications code base dry. It’s easy to imagine being able to slip a javascript functions or modules through a xhr request at runtime similarly to how ruby on rails delivers the output of rjs templates or simply host the javascript modules you want shared in a place where both the server and client can reference them.

There is a downside as well, you can’t perform all tasks you might want on the server with javascript. Which means any utility scripting such as those that either needs access to the filesystem or for any other things that javascript is unable to do requires using Java or dynamic language such as Perl, Ruby, Python or bash scripts. But if there is a real possibility to sensibly share code on the server and client-side, that seems trivial.

I have been cobbled together some helma code in preparation for an actual tutorial. Where the objective is to develop a little application and explore how sharing of javascript between server and client can be accomplished. To do this I have written a very simple blog post engine where one can enter a posts, comment on posts anonymously. If you practice vigorously you can probably pretend you can code the blog application in a 15 minutes

Keep in mind that this is just a rough draft.

For a bash novice like me it’s often hard to argue why working in the terminal is superior than not working in the terminal. Then I find something like THIS a page that explains ways to quickly jump around directories in the terminal by using CDPATH, pushd and popd. Even with tab completion navigating up and down directories with cd can become very tiresome and those three commands can alleviate that pain. The page explains it better than I will be able to but here is the gist of things.

CDPATH By setting your CDPATH to for example

export CDPATH=/home/{user}/sites:/home/{user}/documents

it will allow you do simple do

cd foo

and it will drill down into directory by the name of foo in the ~/sites folder.

This will take over a directory by that name in locations defined after ~/sites in CDPATH and even if you are trying to go into foo in the current directory

pushd If you are in a directory and you know you will be going there again soon you can put it into a dirs stack with the command pushd . and then you go to another dir and by doing popd it will throw you back to the dir that is first in the dirs stack.

As I mention before it is sometimes hard to explain how the terminal can be a better place to do your work so trying it out is the best way to get more and more comfortable with it.

 
allskonar Powered by Mephisto