installing git is not fun but it is more fun than a fork in your eye

April 29, 2008

Here is more or less what I did to install git on OS X and on Media Temple. Newer versions of the below files will exist by the time you are reading this i.e. now.

I got the below mostly from http://dysinger.net/2007/12/30/installing-git-on-mac-os-x-105-leopard/

# GPG (if you didn’t have it already)
curl ftp://ftp.gnupg.org/gcrypt/gnupg/gnupg-1.4.7.tar.bz2 | tar xj
cd gnupg-1.4.7
./configure
make
sudo make install
cd ..

# GetText
curl http://mirrors.usc.edu/pub/gnu/gettext/gettext-0.17.tar.gz | tar xz
cd gettext-0.17
./configure
make
sudo make install
cd ..

# GIT
curl http://kernel.org/pub/software/scm/git/git-1.5.4.4.tar.bz2 | tar xj
cd git-1.5.4.4
./configure
make
sudo make install
cd ..
curl http://www.kernel.org/pub/software/scm/git/git-manpages-1.5.4.4.tar.bz2 |
sudo tar xj -C /usr/local/share/man

# Setup GIT
git config –global user.name ‘My Name’
git config –global user.email me@mydomain.net

#can you see two dashes before “global”?


Capistrano Remote Cache Easter Party ‘08

March 25, 2008

Thankfully we all survived Easter this year. To celebrate the resurrection of our savior we resurrected our servers into a new cluster at Engine Yard. The new cluster has plenty of room for us to grow as we need more resources. We didn’t kill our servers on Good Friday but we did have two hours of easter downtime right before midnight as we copied the database.

We discovered some new deployment recipes in the process.

Engine Yard has a gem eycap that allows remote caching and dramatically speeds up deployment for our plugin laden application. It works by checking out our code on the remote system. For every deploy the code is updated and then copied minus the .svn directories. That means an entire svn checkout is not needed!

First install the gem:

gem install eycap –source http://gems.engineyard.com

Then add this to your deploy.rb:

require ‘eycap/recipes’

set :deploy_via, :filtered_remote_cache

set :repository_cache, “/var/cache/somewhere”

The license included says anyone who has the software can use it so I think I’m allowed talk about it. Enjoy.

images.jpeg


A Lazy Javascript, a Ruby Load and a Sponge plugin to a bar

March 21, 2008

Lazy Load Content

Sometimes a portion of your page takes a really long time to generate and render. Instead of allowing the user to spin his thumbs, you can display the bulk of content to user and send off a separate ajax request to gather the “slow” data. Lazy Load Content makes late loading a portion of your page through ajax as easy as setting up a remote_function call. With options to render fragment cached if they exist, and perform the late load if not, your app will fly like the sloth eating eagle.


Install

ruby script/plugin install  http://arperftoolkit.rubyforge.org/svn/trunk/lazy_load_content/

Copy the file javascripts/lazyLoadContent.js into your public/javascripts folder and include it with prototype.js in your views.


The Basics:

In your rhtml or haml file:

<h1> I love giraffes.. basically </h1>
<% lazy_load(:remote => {:update => 'lazyLoadContent', :url => {:action => 'lazy_load_action'}})do -%>
  <div id = "lazyLoadContent"> We wait while the giraffes are smelling themselves. </div>
<% end -%>

In your controller define the specified function

def lazy_load_action
  render :text => 'The giraffes are ready!'
end

When the page loads, the loading text is displayed: We wait while the giraffes are smelling themselves.
Then after the ajax call we see: The giraffes are ready!

…….. and more………. Read the rest of this entry »


Delegate to the Giraffe

February 12, 2008

I have gotten very tired of writing the same code over and over. And apparently I was so tired I forgot to publish this last year.
def foo_bar
my_member ? my_member.foo_bar : nil
end

The rails delegate method allows you to delegate a method to another class. Neat! There are two tweaks that I made to suite my salty tastes.

1. Nil freaks out. Sure you can write something like
:to => (my_member or return nil) as pointed out on the rails ticket. But that is sort of groddy. boo!

2. I want to rename the destination method sometimes. Avoid collisions, make new friends. Win-Win.

Here is delegate_x. Delegate extra! You can list out the delegated methods or, to rename them, put them in a hash of destination_method_name => target_method_name

class WebTwoPointOMGCorp
attr_accessor :giraffe

delegate_x :hovercraft, :to => :giraffe
delegate_x :to => :giraffe, :giraffe_spots => :num_spots
end

The first is equivalent to:
def hover_craft
giraffe ? giraffe.hover_craft : nil
end

and the second

def giraffe_spots
giraffe.num_spots if giraffe
end

With ActiveRecord :has_one and :belongs_to associations, check out Stefan Kaes’s piggyback plugin to piggy back attributes (define methods) to queried records.

Sweet. A little (terribly formatted) code below. Jam it in the lib or something. Make it a plugin. Just remember, the hovercraft owned by the giraffe is really the hovercraft owned by WebTwoPointOMGCorp.

(Actually I just wrote this whole post to show Joel’s awesome new spongecell giraffe that breathes fire! We love giraffes and so should you.)

Read the rest of this entry »


Finding and Fixing Slow and Leaky Requests this MLK

January 21, 2008

Happy MLK! For this year’s MLK Spongecell has released a new ultra-beta Boxed Calendar. It’s so ultra-beta we haven’t even mentioned it on our main blog yet.

If The Reverend had been a system administrator he definitely would not have allowed slow or leaky requests on his system. Spongecell was recently experiencing a very bad leaky request. Luckily we were logging PIDS to support Rawk so simply watching our mongrels and seeing when a process exploded let me know when and which process was bad. I looked at the logs and saw a few requests for that process at that time. I ran those requests on our staging server and very quickly saw a bad sql query. Turns out some new code found some very old code and the mix wasn’t pleasant.

I’ve also been spending a little bit of time optimizing requests that Rawk reports have shown our server spends a lot of time on. For one of these MySql had decided to start using the wrong index on a paginated join. Thanks to Blythe, our resident ActiveRecord expert, we were able to change the has_many relationship to use an index hint. Here’s what the code looked like for User:

has_many(:email_addresses, :foreign_key => :user_id, :index_hint => :fk_ea_user

Engine Yard also put in fair scheduling into our Nginx setup. By default nginx will distribute requests round-robin which is bad if one request is slow. When nginx sends to that slow mongrel the request will queue and wait for completion. With fair scheduling Nginx sends to available mongrels first thus less waiting.

I found this picture on the internet. “Leaky pipes are the biggest wasters.”

bigstockphoto_leaky_hose_743314.jpg


The complete nginx solution to sending flowers and files with rails

November 13, 2007

Rails send_file is effed. It may send you flowers and whisper sweet nothingsblythedoll into your ear, but believe me, it will steal your memory, your lover and your freetime. If you are using apache or lighttpd, all the cool kids are using the x_send_file plugin. Nginx fans, check out my monkey patch so you too can send files, flowers and love without crashing mongrels with nginx’s equivalent X-Accel-Redirect header.

1. Install the x_send_file plugin.

ruby script/plugin install http://john.guen.in/svn/plugins/x_send_file

2. Copy monkey patch to

Replace vendor/plugins/x_send_file/lib/x_send_file/controller.rb with http://www.acidlunchbox.com/blythe/rails/x_send_file/controller.rb

3. Configure things

nginx.conf

remember to also add to server section if sending over ssl

location /files {

root /data/spongecell;

internal;

}

environment.rb

#send files through nginx

XSendFile::Plugin.options.update(

:header => ‘X-Accel-Redirect’,

:root => ‘/data/spongecell’,

:file_paths => ‘/data/spongecell/files’

)

In this example, x_send_file will create the following response header

>> x_send_file ‘/data/spongecell/files/snow/iheartbigdumps.jpg’

>> response.headers['X-Accel-Redirect']

=> ‘/files/snow/iheartbigdumps.jpg’

 

Other resources:

Nginx X-Accel-Redirect

Nginx X-Accel-Redirect with rails and php examples

 

 

 

Read the rest of this entry »


Rails Credit Card Processing with ActiveMerchant

September 20, 2007

With just 4 man-weeks of research and development we just released our first stab into e-commerce!

I began by researching what libraries were out there. Quickly there were 2 big solutions to choose from: ActiveMerchant and Amazon’s new payment service. If we used Amazon we would have to display their branding. People trust Amazon so using their branding may be positive but it would also wreck the continuity of doing a Spongecell purchase. ActiveMerchant would allow us to keep everything on our site but we would have to set up our own gateway and merchant services accounts.

ActiveMerchant is a ruby library that lets you use a few different credit card gateways. I got a some test accounts with different gateways and began to experiment with the library. I tried ActiveNet, TrustCommerce and UsaEPay. I’ve had bad experiences with PayPal so I stayed away from them. TrustCommerce was the only one I could get to work well in less than 30 minutes. TrustCommerce does require a C library which is a little bit of a downside but that library installed without headache.

purchase.png

The people at TrustCommerce were responsive and they had a good connection at Bank of America to get our merchant account set up. Getting all the accounts set up took about a week. We already had our federal tax id.

We began testing ActiveMerchant from inside our application with real credit cards 1 week before release was scheduled. I’m glad we did this because a lot of the return codes differed when using the real TrustCommerce account. I learned that it’s up to the credit card processor to deny the transaction if an incorrect cvv or address. That means that if you find a lost credit card on the street and a shady website then you are in business!

Now go to Spongecell and buy something!


Haml is great

June 6, 2007

I wrote ages ago (way back in November) about seeing an interesting little templating system called Haml at RailsCamp. Well, curiosity led to experimentation, experimentation led to infatuation, eventually we were living an alternative Haml-oriented lifestyle. Haml has matured quite a bit since then. 1.0 came out in January; 1.5 came out recently (like a month ago) and things are looking quite nice. We had a couple minor hitches due to the shifting sands of active development on a young technology, but that mostly lies now in the long long ago, in the before time. A lot of that is probably due to Haml being a very small, self-contained thing. Which is also why it should take you about twenty minutes to get going with it. There is a nice little tutorial that gets you going from installing the plugin to making your first Haml template.

Here is my greatest hits list of reasons people give for not trying Haml and why you should ignore them.

Reason #1: This is Ruby, not Python! I’m afraid of active whitespace!

Why this makes no sense: Heard of YAML? Which is all over Rails? And is also full of active whitespace? I don’t want/use a programming language with active whitespace, but it makes a lot of sense for certain kinds of data, so get over it. Getting rid of explicitly closing tags immediately got rid of hunting through RHTML for why unmatched tags were wrecking the intended structure of a file. Using indentation to show the structure of HTML documents is awesome and less error prone.

Reason #2: I like syntax highlighting in my editor! Haml is young and saucy, certainly this must not be available!

Why this makes no sense: People who like Haml are super nerds, and have been busily hacking away so that they can use it more pleasantly in their favorite editors. I use the textmate one. There is one for Eclipse/RadRails/Aptana. In progress/useable for Komodo. Vim? Eclipse? Many options are available, more will come.

Reason #3: Learn a whole new language! OMG SO HARD!!

Why this makes no sense: It is not a whole new language. Describing it as a language is almost insulting to the concept of a computer language. It’s markup, it is a combination of elements from CSS, Ruby, and HTML, which assuming you are interested in this to begin with, you are already using. This is so not freaking rocket science, people. It has like 5 concepts. Maybe 4 if flattening gets fully deprecated. Seriously, if you can’t figure this out in less than an hour, turn in your geek badge and maybe take up sorting colored glass or something more your speed.

Reason #4: It can’t do loops! Or multi-line statements! Or some other weird thing I saw while reading email archives from November!

Why this makes no sense: All the block stuff you are familiar with from RHTML files also works in Haml. Your helpers will continue working, the world will continue spinning, people will still find the Rails community vaguely glassy eyed and cultish. Many things have been improved over Haml’s short little lifetime, it’s worth remembering that stuff that was true last December may not be true now.

Reason #5: It might be slow!

Why this makes no sense: Actually, that’s not a bad complaint. Rails in general is slow, ERB is slow, maybe you should worry about this? Of course most people who bring this up just make blind guesses about what probably they think is happening behind the scenes and never bother to benchmark anything. I did and it worried the hell out of me. Haml was > 3x slower than ERB. So I kind of tweaked out about it, started jabbing at the code and optimizing things. Now the development version is only about 1.7x slower, I intend to have it close to about 1 in the near future. I did a proof of concept getting rid of prettification of the HTML output that runs about 2x faster than ERB. So: speed is coming, there is nothing about the Haml concept that makes it intrinsically slow. Also: open source means getting to investigate, identify and fix your own damn problems. Let this be a lesson to you.

(Another lesson to you is that you should try Erubis. Pure ruby, 3x faster than ERB, works great with Rails. To those who think application performance is always bound by db and never by view rendering, you are wrong and you should not generalize your case to everyone. Railsbench is your friend and you may be surprised by how much snappier you can make things.)

Reason #6: But in such and such specific case, RHTML is better/easier to read!

Why this makes no sense: Actually, this is completely true. I noticed just today that I prefered to use RHTML for some email templates I was making. They have no CSS related structure, they just drop a couple names into a big blob of email text. So I just used RHTML. Haml is appropriate for things in which CSS and structure of the document interact heavily. If this isn’t the case, don’t use it. Mixing and matching is awesome. RHTML and Haml are both painfully simple. If you can handle walking and chewing gum at the same time you should have no problem using them both.

Well, this post… ended up much, much snarkier than I originally intended. I’ve seen a number of odes to the joy of using Haml recently and so I took the opposite tack of lining up the main objections I’ve seen knocking around and taking them (at least partially) down. Share and enjoy!


Spongevox: Remind me to drink tequila tomorrow

May 4, 2007

We have a new feature for cinco de mayo! It’s ultra-beta so I’m discreetly posting on our tech blog.

1. Create a Spongecell account
2. View your calendar
3. Click on My Profile
4. Enter your cell phone number
5. Click ‘Send Test Message’ to make sure you entered your number correctly
6. Dial 415-692-3392
7. After the message, describe an event e.g. “remind me to drink tequila tomorrow”
8. In about a minute you should see the event appear on your calendar
9. Tomorrow you will get an sms reminder. It’s takila time!

cinco.jpg

This cool new feature is powered by Spinvox who was gracious enough to lend us a phone number. Spinvox’s translating power combined with Anthony Yam’s original Spongecell NLP let’s ordinary humans change speech into events.

You can also use this service to query for events. Say the word “today” to get an sms of today’s events, “tomorrow” for tomorrow’s events or “next” for a description of the next event on your calendar.


Tom is more productive on a bouncy ball

March 12, 2007