Detecting Browser Time Zone with Rails

February 27, 2009

I recently revisited the problem of detecting browser time zone because I wanted to try the new time zone functionality in Rails 2.1. I found this post from Dave Johnson. To my disappointment this was the same solution Spongecell used in the personal calendar 3 years ago.

I wanted a simpler solution: one that doesn’t require a js cookie library, nor around filters, nor a UI with a combo box with 100s of time zone choices.

The solution presented is to send browser info using jquery and then storing the time zone in the session for use in all subsequent requests.

in the view (this is haml), this can be in your layout on all pages:

    - unless @time_zone
      = javascript_tag "$.get('/controller/time_zone',
      'offset_minutes':(-1 * (new Date()).getTimezoneOffset())})"

in the controller:

 before_filter :init_time_zone
  #sets the time zone for this request if a session time zone exists
  #if it doesn't the default is UTC

  def init_time_zone

    @time_zone = ActiveSupport::TimeZone[session[:time_zone_name]] if session[:time_zone_name]

    Time.zone = @time_zone.name if @time_zone

  end

  #this receives browser info from a jquery request and stores 

  #time zone info in the session
  def time_zone

    offset_seconds = params[:offset_minutes].to_i * 60

    @time_zone = ActiveSupport::TimeZone[offset_seconds]

    @time_zone = ActiveSupport::TimeZone["UTC"] unless @time_zone

    session[:time_zone_name] = @time_zone.name if @time_zone

    render :text => "success"

  end

in the formatter:
  def format_time(t)

    return "" unless t

    return t.in_time_zone.strftime('%Y-%m-%d %H:%M:%S %Z')

  end

Look how simple that is! I choose to default the time zone to UTC if one cannot be determined on the first attempt. Now the formatter will output all the UTC times you have in your db or anywhere in the user’s browser’s time zone.

If there is a better solution in Rails please let me know. We ‘ll see if this solution works for daylight saving time.


NGINX does some crime

November 19, 2008

from CyberCrime and Doing Time

“The website sergej-grienko.com is in Russia and doesn’t run Apache or IIS or any other common webserver. Its running a webserver called “nginx” (Pronounced Engine-X). That’s a huge negative right there. Many webservers that host malware are using this webserver type.”

Spongecell and Engine Yard use NGINX but I think it’s a huge positive! We use it for analytics reporting in addition to normal mongrel load balancing. We’ve load tested nginx on a single slice being able to handle close to 10,000 requests per second. Perhaps we like it for the same reasons the criminals do: it performs.


Star in a Porno with Facebook Connect and Rails

November 17, 2008

I tried out Facebook Connect with Rails recently. The result is Zack N Miri.

znmFiguring out all the details wasn’t super straightforward. Essentially I took the jogging php example from Facebook and combined it with the Facebooker Rails plugin. You can check out the code at Github.

Who wants to get some milk with Zack?


Screen Sharing Crippled!

October 28, 2008

How annoying, Apple has disabled the power tools in Screen Sharing that are mentioned in this great article. This comes in a recent update to security or system or something. I got around the problem by downgrading my Screen Sharing to 1.0 which I found on an old computer in the office. The app is dated 10/2007.

I use Screen Sharing to connect to my mini at home when I am at the office or on the road and need to access media. I’ve never had to connect to my work computer because I keep that synced with imap, idisk, svn and git.


FileVault is secure if you keep the door locked

October 1, 2008
I did some research to see exactly how strong encryption is if you use Apple’s FileVault. (Windows has BitLocker).

 

1. If your laptop is stolen while powered on, if someone has the right hardware they can read your ram and steal all vault passwords. This is unlikely but possible.

 

2. If your laptop is off they can brute force your password. For a simple 6 character password this would take ~1000 EC2 computing days and cost ~$5,000. A 7 character password would cost $40,000 to break and 8 character $2.5 million. Use lowercase, uppercase, numbers and symbols for the best password protection. You must also use secure virtual memory or passwords will be written to disk.

 

If you don’t use FileVault nor BitLocker and your laptop is stolen then your bank accounts, your email, your passwords and you are compromised.

E-Commerce Presentation at RailsConf Europe

September 4, 2008

I’m in Berlin right now getting ready to give a presentation on E-Commerce. I’ll be comparing a few credit card payment solutions for rails. Europe has stricter banking rules than in the U.S but I’m hoping I can help some people out.

I created an example site, Rails Vendor, to demonstrate payment options and show some code. You can also see an example of one of our new Rich Media Ads picturing DHH himself.


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 »