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.

Here is an excellent case of a big problem … with a incredible simple solution. I upgraded my slicehost ubunto vpn from dapper to hardy. A risky business with nothing real in jeopardy except for the pride of a 380 days up time on the server. I shut down everything running on the server and upgrade to hardy. All is fine except that I realize that when I am starting all the services that small things tab completion doesn’t work among other annoyances and doing work on a server without that is practically unbearable. So I figured I would make some time to fix it… the best place to go on these issues is slicehost … THE ROCK. Tony on the slicehost campfire chat immediately identified the problem and it was fixed within 5 minutes.

*Tony | try just running “bash”

And as he said that I was like shit and ran echo $SHELL only to realize that I was in “sh” but now everything is fixed and dandy and I have my tab completion back along with other normal bashy things … thank god. Did I mention that slicehost rocks!

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

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

11/14 2007

November 14th, 2007

Two things in my .bash_history today.

file merging and resolution conflict tools

vim has an answer for every problem vimdiff file1.txt file2.txt

FileMerge looks promizing also

the ever lasting quest for decent terminal

the screen app. see http://jmcpherson.org/screen.html

11/12 2007

November 12th, 2007

One cool thing I learned today was that you can set the columns when you generate the rails migration from the terminal. The prepopulates the tabel generation code with the table names and datatype. This isn’t such a big find since it’s well documented in script/generate model—help. But here is how it looks.


 script/generate model members name:string age:integer
 script/generate model portfolio member_id:integer body:text

Another thing I just came to realize is that defining explicit foreign_key constraint to the database is probably a good idea. I don’t know too much about database administration but if I understand it correctly it helps with dataintegrity and probably performance if the table grows big. Adding indexes is also a plus. A little specifity never hurt anyone. This example is for MYSQL with the InnoDB engine which I believe is the default for rails now.


. . .
def self.up
  create_table :members do |t|
    t.column :name, :string
    t.column :age, :integer
  end
end
. . .

def self.up
  create_table :portfolios do |t|
    t.column :member_id, :integer
    t.column :body, :text
  end
  execute 'ALTER TABLE portfolios ADD CONSTRAINT fk_portfolios_member \\
     FOREIGN KEY (member_id) REFERENCES members(id)'
end 
. . .


		
		
	

11/8 2007

November 8th, 2007

Here is one neat little vim trick


def some_method
   return bla
end

These vim mappings allow you to quickly comment a block of code in VISUAL mode by pressing ,# or any other key command you prefer

#def some_method
#  return bla
#end

And pressing ,c removes them again.

def some_method
   return bla
end

11/7 2007

November 8th, 2007

It’s educational to watch other people at work! especially if they are really good at what they do. I had such luck today. I was clever enough to make sure I had a backup of my .bash_history file so I can go through his actions and try them myself in the hopes that I would remember them later on when I needed them again and that’s where I had this idea of maintaining a page where I can put down those tricks for me to remember and help others that happen to stumble upon it.

The fist thing I learned was that when you are in vi on the terminal and you want to quickly go out of it and get to the console you can hit control+z and that brings you to the bash shell and then you type fg to get back and if there is more than one buffer open you can do fg1 fg2 etc, pointer you can also get to the shell via the :sh command.

Here are some other things.


svn status | grep "^?" | grep -v tmp |   \\
      sed -e 's/^? *//' | xargs svn add
svn diff > changes_to_patch

svn diff -rHEAD:999 > changes_to_patch
patch -p0 < changes_to_patch

svn log --stop-on-copy

 
allskonar Powered by Mephisto