Friday, March 4, 2011

Using Ruby for SCP file transfers

Today I needed to write a Ruby script to fetch a file using SCP. I thought it would be nice if it also displayed a "percent complete" counter as it went. This is what I came up with.

require 'rubygems'
require 'net/scp'
puts "Fetching file"

# Establish the SSH session
ssh = Net::SSH.start("IP Address", "username on server", :password => "user's password on server", :port => 12345)

# Use that session to generate an SCP object
scp = ssh.scp

# Download the file and run the code block each time a new chuck of data is received
scp.download!("path/to/file/on/server/fileName", "/Users/me/Desktop/") do |ch, name, received, total|

  # Calculate percentage complete and format as a two-digit percentage
  percentage = format('%.2f', received.to_f / total.to_f * 100) + '%'

  # Print on top of (replace) the same line in the terminal
  # - Pad with spaces to make sure nothing remains from the previous output
  # - Add a carriage return without a line feed so the line doesn't move down
  print "Saving to #{name}: Received #{received} of #{total} bytes" + " (#{percentage})               \r"

  # Print the output immediately - don't wait until the buffer fills up
  STDOUT.flush
end
puts "Fetch complete!"

Saturday, February 26, 2011

Best error message ever

A while back, I sent a text message and got this error back from AT&T.


Wow... thanks. I... didn't know you cared?

Apparently they just append the text of the message (this one was to my wife) to the end of the error, without saying "original message follows" or anything like that.

In case anyone wants to try getting their own amusing messages, I believe this happened because I mistakenly sent to a land line.

Wednesday, February 23, 2011

Watching your log for a specific value

If you use Unix, you probably know about piping one command's output through another. You may also know that
tail -f somefile
will show you whatever gets added to the end of a file in real time.

While working with Rails, I do
tail -f log/development.log
all the time to watch how Rails processes my page requests. Today, I was looking for a specific error message to show up - a MissingTemplate error that a user had experienced**, and I was trying to reproduce it.

Since I didn't care about anything that was being logged at that moment other than the error I was looking for, it occurred to me to do this:

tail -f log/development.log | grep 'MissingTemplate'

As I poked around our site, I saw nothing. Until I encountered the error, and BAM - that one line popped into my terminal. Error case located!

Just one more way that chaining simple tools together can be very powerful.

**Actually, the user didn't see the MissingTemplate error; they got a friendly error message, as they should. But we got an email from Hoptoad with the actual error.

Tuesday, January 11, 2011

PHP: now running in the browser!

Got this email from a recruiter just now:

Hope you are doing well. We have an urgent requirement for
PHP Front End Developer . Please go through the below mentioned job opportunity and let me know your availability for the same.

PHP Front End Developer!? Wow, those zany Facebook developers must be at it again. First they got PHP to compile, now they've got it running in the browser!

Thursday, January 6, 2011

IE: Still the biggest yak to shave

My conclusion after a day and a half of fixing IE-only Javascript bugs.



And this is IE8, mind you.

Come on, Microsoft. Just use Webkit, already.

Tuesday, December 28, 2010

Introducing Bindler

Rails. Hobos. Bundler. Bindles.

I awoke one day last week with this comic idea percolating in my brain. Too bad I'm lousy at drawing.

Monday, December 6, 2010

Chaining jQuery Pseudoselectors

I just finished a very simple Javascript tweak to an app. Here was the requirement: "When the user opens our sign-in modal, we should focus the on the first input." Whatever code I wrote, I would put it a callback when the modal was finished opening.

This was my first solution: target the id of the first input, and focus on it.

$('input#user_email').focus();

But I didn't like that, because it's not very flexible. What if we add another input to the beginning of the form? We'd have to change the javascript, too.

After a little tinkering with jQuery's psuedoselectors, I came up with this:

$('#sign-in-modal-form').find('input:not(:hidden):first').focus();

Besides being more flexible to future changes, I think this is still pretty readable: select the first input that isn't hidden and focus on it.