Thursday, May 27, 2010

Cross-Origin Resource Sharing (CORS)

Cross-Origin Resource Sharing (CORS) is a W3C Working Draft that defines how the browser and server must communicate when accessing sources across origins. The basic idea behind CORS is to use custom HTTP headers to allow both the browser and the server to know enough about each other to determine if the request or response should succeed or fail.

http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/

Tuesday, May 18, 2010

Get xpath string expression of a document element

XPath is one of those things you don’t hear too much about these days. In the days when XML ruled, XPath was very important to developers as a means of random access within a large structure. Since JSON was popularized, XPath has gotten less and less attention, but there is still fairly good support for XPath queries in browsers.
function getElementXPath(elt)
{
     var path = "";
     for (; elt && elt.nodeType == 1; elt = elt.parentNode)
     {
    idx = getElementIdx(elt);
 xname = elt.tagName;
 if (idx > 1) xname += "[" + idx + "]";
 path = "/" + xname + path;
     }
 
     return path; 
}

function getElementIdx(elt)
{
    var count = 1;
    for (var sib = elt.previousSibling; sib ; sib = sib.previousSibling)
    {
        if(sib.nodeType == 1 && sib.tagName == elt.tagName) count++
    }
    
    return count;
}
http://www.nczonline.net/blog/2009/03/17/xpath-in-javascript-part-1/
http://snippets.dzone.com/posts/show/3754

Sunday, May 9, 2010

Awesome Print

awesome_print is a Ruby tool that provides "pretty printing" support for your objects. It's a bit like p, pp or, if you prefer, puts obj.inspect, but with significantly improved, contextual, colored output. Its creator and maintainer is Michael Dvorkin of Fat Free CRM fame.
Being able to see "inside" Ruby objects on the fly can prove useful whether you're debugging some code your tests did not dare reach or you're just playing around in irb. The most common way to examine objects is with p or the inspect method, but these don't format their output in a particularly easy-to-read way. pp - part of the standard library - is a pretty printer that improves matters but it still leaves a lot to be desired. awesome_print takes it all to the next level.
A visual comparison between pp and awesome_print proves compelling:

awesome_print's most compelling features are showing you the index of array elements within its output, inheritance for the classes of embedded objects, and color coding. Further, it's highly customizable, with the ability to set indent levels as well as the colors for each type of data shown.
To get up and running, gem install awesome_print and then require 'ap' and use ap in place on anywhere you'd usually use p. Yep, that's it.

http://github.com/michaeldv/awesome_print
http://www.rubyinside.com/awesome_print-a-new-pretty-printer-for-your-ruby-objects-3208.html

Wednesday, May 5, 2010

RGraph: HTML5 canvas graph library based on the HTML5 canvas tag

Great Graphs, Charts & Tables That Build Real-Life Math Skills: High-Interest Reproducible Activities That Give Kids Practice Interpreting and Creating Bar Graphs, Line Graphs, Pie Charts, and MoreThis library uses HTML5 features which are implemented in recent browsers. As such you'll need to be using one of these latest browsers: Firefox 3.5+, Chrome 2+, Safari 4+, Opera 10.5+ or Internet Explorer 8.

Microsoft Internet Explorer 8 is now (December 2009) supported in a limited fashion. You can read more about it here.

Older versions of Opera and other older browsers are supported in a limited fashion. If they don't support text or shadows these will naturally be unavailable.

RGraph is a HTML5 canvas graph library. It uses features that became available in HTML5 (specifically, the CANVAS tag) to produce a wide variety of graph types: bar chart, bi-polar chart (also known as an age frequency chart), donut chart, funnel chart, gantt chart, horizontal bar chart, LED display, line graph, meter, odometer, pie chart, progress bar, rose chart, scatter graph and traditional radar chart. RGraph is covered by the RGraph License.

http://www.rgraph.net/

Sunday, May 2, 2010

Find duplicates in an Array with grep

The following example finds duplicates in an array by selecting all array items which have apear more than once.
a = %w(a b a c)
a.uniq.select {|x| a.grep(x).length > 1}
#=> ["a"]

a = %w(a b a c d d d e f g h f)
a.uniq.select {|x| a.grep(x).length > 1}
#=> ["a", "d", "f"]
 
a = %w(a b a c d d d e f g h f)
a.group_by(&:to_s).select{|k,v| v.length > 1}
#=> {"a"=>["a", "a"], "d"=>["d", "d", "d"], "f"=>["f", "f"]}
http://snippets.dzone.com/posts/show/11219

Monday, April 26, 2010

jsplumb

This jQuery plugin provides a means for a developer to visually connect elements on their web page, in much the same way you might have seen on Yahoo Pipes. It uses Canvas in modern browsers, and Google's ExplorerCanvas script for stone-age browsers. Full transparent support for jQuery dragging is included, the API is super simple, and the compressed version of the script is just 11.6K.

You can see a demonstration here.
TweetPlumb is a great Twitter visualisation built using jsPlumb.

http://code.google.com/p/jsplumb/

DrX


DrX, the good doctor, is a small object inspector for Ruby.
DrX is for newbies and gurus alike.
Instead of focusing on the contents of your object, DrX instead focuses on its object model. As a result, DrX is most suitable for programmers wishing to understand Ruby's object model better. It's especially adept at showing you how a "magical" library works (e.g., DataMapper).

Key features:

  • See everything about a Ruby object: its 'klass', 'super', 'iv_tbl', 'm_tbl'. See your singletons with your very own eyes!
     
  • Double-click a method to launch an editor and position the cursor on the exact line where the method is defined!

Installation

At your system prompt type:
gem install drx

Requirements 

require 'drx'
Errors and solutions:
  1. MissingSourceFile: no such file to load -- tk
    - require 'tcltklib' (install Tcl/Tk interface for Ruby)
  2. RuntimeError: TkPackage can't find package tile
    - install themed widget set provider library for Tk
  3. RuntimeError: ERROR: Failed to run the 'dot' command. Make sure you have the GraphViz package installed and that its bin folder appears in your PATH.
    - install rich set of graph drawing tools (
    GraphViz)