Skip to content

Instantly share code, notes, and snippets.

@Sixeight
Created June 1, 2009 05:27
Show Gist options
  • Save Sixeight/121246 to your computer and use it in GitHub Desktop.
Save Sixeight/121246 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
# -*- coding: utf-8 -*-
# You need a jQuery library: ./public/javascripts/jquery.js
# Run: ruby app.rb -s mongrel
require 'rubygems'
require 'sinatra'
require 'haml'
require 'sass'
trap(:USR1) { ENV['typenow.go'] = 'true' }
get '/' do
haml :index
end
post '/type' do
ENV['typenow.val'] = params[:value]
Process.kill :USR1, $$
end
get '/wait' do
ENV['typenow.go'] = 'false'
start = Time.now
loop do
break if ENV['typenow.go'] == 'true'
break if (Time.now - start) > 30
sleep 0.05
end
ENV['typenow.go'] == 'true' ? ENV['typenow.val'] : halt(304)
end
__END__
@@ layout
!!! XML
!!! Strcit
%html{ :xmlns => 'httj://www.w3.org/1999/xhtml' }
%head
%meta{ :'http-equiv' => 'Content-Type', :content => 'text/html', :charset => 'utf-8' }
%script{ :type => 'text/javascript', :src => 'javascripts/jquery.js' }
:javascript
(function() {
var error_count = 0;
var waiting = function() {
$.ajax({
url: '/wait',
success: function(res) {
if (res) {
$('#result').text(res);
}
waiting();
},
error: function() {
if (++error_count == 5) { return false; }
waiting();
}
});
}
$(function () {
waiting();
var type = function() {
var val = $('#type').val();
$.post('/type', { value: val }, function() {});
}
$('input#type').keypress(type).keydown(type).keyup(type);
});
})();
%body
%h1 Typenow
= yield
@@ index
#result{ :style => 'width: 500px; height: 30px; border: 1px solid #aaa' }
%input#type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment