ftp upload in one command

Posted by caike on March 04, 2010

As part of a backup routine, I had to upload a file to an ftp server. It actually took me some time to figure out how to do this with one command, so I thought I’d post it here:

curl -sS -T ${FILE_PATH}/${FILE_NAME} --ftp-pasv \
ftp://${FTP_USER}:${FTP_PWD}@${FTP_URL}/${FILE_NAME}

This example is uploading the file to the ftp server using silent mode (-s) and showing errors when they occur (-S), while connecting to the server in passive mode (–ftp-pasv).

Refactoring at ORUG 2

Posted by caike on January 19, 2010

Here is the slides for the Refactoring talk I did last Thursday at the Orlando Ruby User Group meeting. In case you want to hear me speaking along with the slides, I’ve put it up on vcasmo also.

Autotesting PHP applications (and others) 3

Posted by caike on January 12, 2010

So you’ve probably heard of ZenTest/Autotest, right?

In case you haven’t, it is a Ruby gem that (among other things) automatically runs your tests whenever it notices that a file in you project has changed. It’s a great productivity boost, because it saves you from having to run your tests each time you make a change to your program. The perfect tool if you are doing TDD.

So I wanted to get this autotest functionality going for projects in other languages - like PHP, for example - but apparently ZenTest/Autotest only works within Ruby projects.

Talking to the EnvyLabs’ guys I was told about Watchr, a Ruby gem that monitors files matched by a regular expression and triggers a user defined command whenever a file changes. That’s enough to get you started in an autotest-like tool for PHP - or just about any other language.

I’ve setup an example PHP project of Watchr playing an autotest role in a PHP project:


View on Vimeo.

The source code from this example can be seen at http://github.com/caike/watchr_running_php

AppleScript for my Rails terminal tabs

Posted by caike on December 10, 2009

While working on a Rails project I always have at least 4 different Terminal tabs that I use for

  1. spork server
  2. autospec
  3. ./script/server
  4. command line stuff (generators and general shell commands)

Setting up the tabs all the time is kind of a boring and repetitive task. This truly deserved some automation and it was the perfect excuse for writing my first AppleScript.

After some googling around I came up with this bash/AppleScript to do these tasks for me (and also open up TextMate):

#!/bin/bash

if [ $# -ne 1 ]; then
    PATHDIR=`pwd`
else
    PATHDIR=$1
fi

/usr/bin/osascript <<EOF
on new_terminal_tab()
	tell application "System Events" to tell process "Terminal" \
        to keystroke "t" using command down
end new_terminal_tab

set actions to {"spork rspec", "autospec", "./script/server"}

tell application "Terminal"
	do script with command "cd $PATHDIR && mate . && clear" \
	in selected tab of the front window
	repeat with action in actions
		my new_terminal_tab()
		do script with command "cd $PATHDIR && " & action \
		in selected tab of the front window
		if action contains "spork" then
			delay 7
		end if
	end repeat
end tell
EOF

If only I had used the right search words I would have found Nick Rutherfor had been through the same situation.

Using Authlogic for general email validation 1

Posted by caike on November 05, 2009

In case you are using Authlogic for authentication in your Rails apps, then you are probably using its email format validation in the user registration step. You get this validation for free just by specifying your model to acts_as_authentic:

class User < ActiveRecord::Base

  acts_as_authentic

end

That’s it for the user model and its registration field validations (login, email and password).

But what if you have some other model in your application with an email field that you also want to validate ? You could just get your favorite email format regexp and use it with the validates_format_of method:

class Band < ActiveRecord::Base

  validates_presence_of :manager, :description
  validates_format_of :email, :with => /SomeRegexpYouGotFromGoogle/

end

But that would be decentralizing the email validation concern - totally anti-DRY and error-prone.

A better solution would be to use the email regexp that’s in Authlogic, which is the same that’s already validating your user model email field.

class Band < ActiveRecord::Base

  validates_presence_of :manager, :description
  validates_format_of :email, :with => Authlogic::Regex.email

end

That’s it. Hope it helps someone keep their code clean.