Wednesday, January 5, 2011

Rails and SSL (https://)

Two more caveats:
  • First caveat is that if you want to go back and forth between http and https, you MUST explicitly set all of your routes to have http or https protocol. If you don't, then when rails generates your routes it will just use the protocol of the current page. This is annoying, but I can see why they would do this (if you are looking at something securely, then just look at everything securely).
  • Second caveat is that you must use the route_name_url throughout your application instead of route_name_path, as some are accustomed to. The _url named route gives you the full url (duh), which is what you want if you are on a page using http and want to go to https (whereas _path only gives you the uri, and will use the protocol and host of the current page). If you already use _url, then you are one step ahead in the game
Here is a sample of some of my routes using the protocol option:

# Public routes using http protocol
map.with_options :protocol => "http" do |http|
  http.login '/login',   :controller => 'sessions', :action => 'new'
  http.logout '/logout',  :controller => 'sessions', :action => 'destroy'
  http.resources :users, :sessions, :requirements => {:protocol => "http"}
end

# Public routes using https protocol
map.with_options :protocol => ROUTES_PROTOCOL do |https|
  https.form_step '/form/*slug', :controller => 'forms', :action => 'show'
  https.form_edit '/edit/*path', :controller => 'forms', :action => 'edit'
  https.resources :forms, :requirements => {:protocol => ROUTES_PROTOCOL}
end
http://siannopollo.blogspot.com/2007/08/rails-and-ssl-https.html

Redcar

Redcar is a text editor for programmers written in Ruby. At the moment, it’s a young enough project that it is mainly of interest to Rubyists who want to use Ruby to script their editor.

https://github.com/redcar/redcar/wiki/installation
http://redcareditor.com/

Tuesday, January 4, 2011

Getting to Know the Ruby Standard Library – Timeout

Timeout lets you run a block of code, and ensure it takes no longer than a specified amount of time. The most common use case is for operations that rely on a third party, for instance net/http uses it to make sure that your script does not wait forever while trying to connect to a server:
def connect
  ...
  timeout(@open_timeout) { 
    TCPSocket.open(conn_address(), 
    conn_port()) 
  }
  ...

You could also use Timeout to ensure that processing a file uploaded by a user does not take too long. For instance if you allow people to upload files to your server, you might want to limit reject any files that take more than 2 seconds to parse:
require 'csv'

def read_csv(path)
  begin
    timeout(2){ CSV.read(path) }
  rescue Timeout::Error => ex
    puts "File '#{path}' took too long to parse."
    return nil
  end
end

Lets take a look at how it works. Open up the Timeout library, you can use qw timeout if you have Qwandry installed. Peek at the timeout method, it is surprisingly short.
def timeout(sec, klass = nil)   #:yield: +sec+
  return yield(sec) if sec == nil or sec.zero?
  ...

First of all, we can see that if sec is either 0 or nil it just executes the block you passed in, and then returns the result. Next lets look at the part of Timeout that actually does the timing out:
...
x = Thread.current
y = Thread.start {
  sleep sec
  x.raise exception, "execution expired" if x.alive?
}
return yield(sec)
...

We quickly see the secret here is in ruby’s threads. If you’re not familiar with threading, it is more or less one way to make the computer do two things at once. First Timeout stashes the current thread in x. Next it starts up a new thread that will sleep for your timeout period. The sleeping thread is stored in y. While that thread is sleeping, it calls the block passed into timeout. As soon as that block completes, the result is returned. So what about that sleeping thread? When it wakes up it will raise an exception, which explains the how timeout stops code from running forever, but there is one last piece to the puzzle.
...
ensure
  if y and y.alive?
    y.kill
    y.join # make sure y is dead.
  end
end
...
At the end of timeout there is an ensure. If you haven’t come across this yet, it is an interesting feature in ruby. ensure will always be called after a method completes, even if there is an exception. In timeout the ensure kills thread y, the sleeping thread, which means that it won’t raise an exception if the block returns, or throws an exception before the thread wakes up.

It turns out that Timeout is a useful little library, and it contains some interesting examples of threading and ensure blocks.

http://endofline.wordpress.com/2010/12/31/ruby-standard-library-timeout/

Monday, January 3, 2011

jspp

jspp: JavaScript Pre-Processor
A simple way to build web applications with embedded server side JavaScript. In a few minutes you can build dynamic backend logic in to any page (html, css, etc) using node.js, jQuery and server side DOM with php-like embedded code.
Basic usage
First thing you'll want to do is run the jspp server against your working directory. Any changes you make in the working directory are automatically picked up by the server and refreshed. No need to start and stop during development or even in production.

Now create an index.html and fill it with content.
You can embed server side processing in <?jspp style tags or in script tags with a content type of "application/jspp". Script style tags are only available in html documents and don't work for jspp.init definitions.
When you're in an html file you get most of the normal browser environments. window, document, and other objects are available. You also have a jQuery object at $ that you can use to manipulate any part of the page.
Inside a jspp block this refers to the current DOM node.
console.log is also available for debug logging in the server.
Every block has an end function that must be called to end that blocks processing of the page, future versions of node.js may allow us access to the event system that will allow us to remove this requirement.
There are many other jspp objects that are scoped to the resource and the page: resource, request, response, page, setHeader, and require.
The require function allows you to import any node.js modules you would like. Relative module imports, like require('./module'), are scoped to the directory the file is in.

http://www.jspp.io/

Sunday, January 2, 2011

DHTMLX Touch

JavaScript Framework for Mobile and Touch Devices

DHTMLX Touch is an HTML5-based JavaScript library for building mobile web applications. It’s not just a set of UI widgets, but a complete framework that allows you to create eye-catching, cross-platform web applications for mobile and touch-screen devices.
The framework is compatible with the major web browsers for mobile platforms. Applications built with DHTMLX Touch will run smoothly on iPad, iPhone, Android-based smartphones, and other popular devices. Download the alpha and start playing with DHTMLX Touch! More features are coming. 

Compatibility

DHTMLX Touch requires a mobile device to display and work correctly. You can also run the samples on usual computers in FireFox 3.6 and above and WebKit browsers (Safari, Chrome, etc.), but there still can be some issues since the library is oriented mostly for mobile devices.

Upcoming features and release plans:

  • Full-featured visual designer
  • Server-side integration
  • Global datastore
  • Carousel component
  • Additional animation
  • Extended support for mobile&touch devices
  • Detailed documentation
  • Support for client storage
DHTMLX Touch v.1.0 release is scheduled on first quarter of 2011. Before this time we are going to release updates on regular basis. Please follow our blog to be informed about available updates. You are welcome to leave your feedback about DHTMLX Touch at our Forum.

JSctypes

js-ctypes is a foreign-function library for Mozilla’s privileged JavaScript. It provides C-compatible data types and allows JS code to call functions in shared libraries (dll, so, dylib) and implement callback functions. The interface and implementation are modeled on the Python ctypes module.
The main objective for the library is to help developers avoid building binary (C++) XPCOM components when only a simple wrapper is needed. Such situations include:
  1. Developer wants functionality not built into the Mozilla platform, but easily supported by the native OS.
  2. Developer wants to use a 3rd party library in an extension or XUL app.
  3. Developer has methods in native code for performance reasons.
The usual answer to these problems is creating a binary (C++) component to act as a wrapper so the native features can be exposed to JavaScript via XPCOM. However, the process of building binary XPCOM components is significantly harder than JavaScript components. The macros and memory management rules of binary components are harder than JavaScript, but its really the compiler and linker flags, IDL, makefiles and SDK versioning that make the process painful. The build process must be repeated for each platform too. For many cases, its just too much work.
The goal of js-ctypes is to allow developers to declare methods in binary libraries and then expose those methods as callable JavaScript functions. Developers can then create pure JavaScript library wrappers around binary libraries - without making binary XPCOM wrappers.

https://wiki.mozilla.org/JSctypes

Equality in JavaScript

Table of comparisons in a quick JavaScript version (a cell is green when column == row)