Archive for the ‘code snippets’ Category

Generating Code from html tables using javascript.

July 1st, 2011 No comments

I frequently write sdks for different applications / sites. I frequently find when I do an integration of some kind for a customer the company does not provide a good sdk for the platform I am using. Because I prefer to make things easy. I usually like to write a tool to auto-generate an sdk for that tool.

An example of this is my 3dcart sdk:

While these sdks aren’t always perfect they allow me to create the integration MUCH faster. Which means I make more money.

Recently I needed to do this for Infusionsoft to create a simple integration that could access a few objects in java via their DataService api. I didn’t need much, just 3 objects so that a fulfillment company could read order information from Infusionsoft. When I worked at Infusionsoft I created a tool that took an xml file and automatically generated their api documentation. Unfortunately, since I am no longer an employee I don’t have access to that xml document. But! I do have access to the public api documentation that my tool auto-generated. And it’s in a table!

So, I quickly used the following code to auto generate the code for my class, my class factory (that populates the class from a map) and the code to populate a list of available fields.

It worked beautifully! This same technique could be adapted to any well organized api documentation. The page I used is located here:

Remember if the page doesn’t have jQuery on it, just install it as follows:

var s = document.createElement('script');
s.setAttribute('src', '');
jQuery('#staticFields, #publicFields, #factoryCode').remove();
jQuery('body').append('<pre id="staticFields"></pre> <br/>');
jQuery('body').append('<pre id="publicFields"></pre> <br/>');
jQuery('body').append('<pre id="factoryCode"></pre> <br/>');

jQuery('table tr').each(
        var $this = jQuery(this);
        $tds = $this.find('td');
        if($tds.length == 3){
            jQuery('#staticFields').append('fields.add("' + jQuery($tds.get(0)).html() + '");\n');                   

            jQuery('#publicFields').append('public ' + jQuery($tds.get(1)).html() + ' ' + jQuery($tds.get(0)).html().substr(0, 1).toLowerCase() + jQuery($tds.get(0)).html().substr(1) + ';\n');                   

            jQuery('#factoryCode').append('contact.' + jQuery($tds.get(0)).html().substr(0, 1).toLowerCase() + jQuery($tds.get(0)).html().substr(1) + ' = (' + jQuery($tds.get(1)).html() + ') map.get("' + jQuery($tds.get(0)).html()+ '");\n');                   

Categories: code generation, code snippets, java Tags:

Page refresh with JavaScript

June 16th, 2011 No comments

The easiest way is to do a: window.location.reload();

It is important though that you use the “POST/Redirect/Get” pattern on your pages, otherwise a window.location.reload() will cause the browser to resend any post data it sent on the last request. 

Categories: code snippets, javascript Tags: