status codes 101

18 June 2009 | 6:47 pm by codeboxer

Ah, the old http status code. Very nice. I recently built my first pro-grade web service and had some problems figuring out the status codes. Here are some tips from my friend happyfrenchy2:

Usually I write my rest service so that a problem with the parameters (wrong ID, not enough params, etc) returns a 400 (bad request), a auth failure (wrong signature) returns a 403 (forbidden) and an internal problem (couldn’t access DB for instance) returns a 503 (server error). Return code 500 should be when the error happens before your code was even reached (like the server or web-server is down).

Very helpful. This is what that looks like, in the create method of the /installs web service:

  def create

    respond_to do |format|
      format.xml {
        install = OnInstall.new do |i|
          i.ip_address = request.remote_ip
          i.mac_address = params[:mac_address]
          i.on_dvd_id = params[:on_dvd_id].to_i
          i.on_dvd_number = params[:on_dvd_number]
          i.on_dvd_set_id = params[:on_dvd_set_id].to_i
          i.on_game_id = params[:on_game_id].to_i
        end
        ts_param = params[:redacted]
        hash_to_match = params[:redacted]
        begin
          #FK validations
          test = OnDvd.find(install.on_dvd_id)
          test = OnDvdSet.find(install.on_dvd_set_id)
          test = OnGame.find(install.on_game_id)
          
          #validate md5 hash
          live_matcher = "redacted"
          digest = Digest::MD5.hexdigest(live_matcher)
          if digest.upcase == hash_to_match.upcase
            if install.save
              headers['location'] = on_install_path(install.id)
              render :nothing => true, :status => "201 Created"
            else
              render :xml => install.errors.to_xml, :status => "400 Bad Request"
            end
          else
            render :xml => "Your s param hash did not match the POST fields.", :status => "403 Forbidden"
          end
          
        rescue Exception => e 
          render :xml => e.message, :status => "503 Server Error"
        end
        }
      format.html {if current_user == :false then redirect_to home_url else super end}
    end
  end



sudo port command not found

1 June 2009 | 7:32 pm by codeboxer

sudo: port: command not found

are you getting this error? Add the below to your ~/.bash_profile -

export PATH=$PATH:/opt/local/bin
export MANPATH=$MANPATH:/opt/local/share/man
export INFOPATH=$INFOPATH:/opt/local/share/info

and then run

sudo port -d selfupdate

you should be fine (for Mac Leopard 10.5)

this worked for me

1 May 2009 | 11:56 am by codeboxer

recursevly remove .svn files from an existing repo.

If you need to take a project out of scm, you can run this bash script from the top folder - and it will remove all .svn folders recursively.

Works well for
- changing an svn checkout into and svn export
- switching from svn to git

find . -type d -name '.svn' -exec rm -rf {} \;

tunnel of love

23 April 2009 | 2:43 pm by codeboxer

This little puppy is my tunnel of love. Connecting to SQL databases just keeps getting more complicated. :)

ssh -L 8888:localhost:3306 rails@staging.gmgdev.railsmachina.com

good stuff on remote rsync

22 April 2009 | 6:15 pm by codeboxer

Making remote copies

What if you want to copy files offsite to a remote host? No problem -- all you need to do is add the host and user information. So, for instance, if you want to copy the same directory to a remote host, you'd use:

rsync -avhe ssh --delete /home/user/dir/ user@remote.host.com:dir/

If you want to know how fast the transfer is going, and how much remains to be copied, add the --progress option:

rsync --progress -avhe ssh --delete /home/user/dir/ user@remote.host.com:dir/

If you don't want to be prompted for a password each time rsync makes a connection -- and you don't -- make sure that you have rsync set up to log in using an SSH key rather than a password. To do this, create an SSH key on the local machine using ssh-keygen -t dsa, and press Enter when prompted for a passphrase. After the key is created, use ssh-copy-id -i .ssh/id_dsa.pub user@remote.host.com to copy the public key to the remote host.

What if you need to bring back some of the files you copied using rsync? Use the following syntax:

rsync -avze ssh remote.host.com:/home/user/dir/ /local/path/

The z option compresses data during the transfer. If the file you are copying exists on the local host, then rsync will just leave it alone -- the same as if you were copying files to a remote host.
Wrapping it up with a script

Once you've figured out what directory or directories you want to sync up, and you've gotten the commands you need to sync everything, it's easy to wrap it all up with a simple script. Here's a short sample:

rsync --progress -avze ssh --delete /home/user/bin/ user@remote.host.com:bin/
rsync --progress -avze ssh --delete /home/user/local/data/ user@remote.host.com:local/data/
rsync --progress -avze ssh --delete /home/user/.tomboy/ user@remote.host.com:/.tomboy/

also remember to to use the --dry-run option with your commands

** good stuff on remote rsync **

add textmate to your Mac 10.5 path

22 April 2009 | 11:49 am by codeboxer

This is the puppy that worked for me:

sudo ln -s /Applications/TextMate.app/Contents/Resources/mate /bin/mate

** add textmate to your Mac 10.5 path **

php headers for expiring includes

1 April 2009 | 2:32 am by codeboxer

By default, representations processed by PHP are not assigned validators, and are therefore uncacheable. However, developers can set HTTP headers by using the Header() function.

For example, this will create a Cache-Control header, as well as an Expires header three days in the future:

?php
Header("Cache-Control: must-revalidate");

$offset = 60 * 60 * 24 * 3;
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
Header($ExpStr);
?

Remember that the Header() function MUST come before any other output.

As you can see, you'll have to create the HTTP date for an Expires header by hand; PHP doesn't provide a function to do it for you (although recent versions have made it easier; see the PHP's date documentation). Of course, it's easy to set a Cache-Control: max-age header, which is just as good for most situations.

For more information, see the manual entry for header at http://us2.php.net/manual/en/function.header.php

... php headers for expiring includes ...

this site rocks

30 March 2009 | 3:40 am by codeboxer

http://layouts.ironmyers.com/

Awesome free css layouts? Ok!

Original post blogged on codeboxer.com.

My Sites

from Last.fm

noonchild's Profile Page

Green Web Hosting! This site hosted by DreamHost.

My Site Links

Screenshots are featured above. If you visit gmgpulse, you may login as demo/demo.

Rockstar Television


© 2008-09 Krister Axel and codeboxer.com