HTML5, CSS3 & JS Experiments

By Martin Ivanov

API for TinyURL

Demo

Open your browsers's console and reload the page to see the class in action.

Register the Class

<script src="AcidJs.Shortener/js/jQuery.js"></script>
<script src="AcidJs.Shortener/js/Shortener.js"></script>

Usage and Code Insight

(function() {
    var
        config = {
            "http://wemakesites.net": "http://tinyurl.com/2cv6wrm",
            "http://acidjs.wemakesites.net/": "http://tinyurl.com/bonbupy",
            "http://acidmartin.wordpress.com": "http://tinyurl.com/clvk2v8"
        };

    window.shortener = new AcidJs.Shortener(config);
    window.shortener.get("http://stickeez.wemakesites.net", function(url) {
        window.console.log(url);
    });
})();
/*
 * Shortener
 * JavaScript API for TinyURL
 * developer's website: http://wemakesites.net/#!/web-dev
 * developer's twitter: https://twitter.com/#!/wemakesitesnet
 * developer's blog http://acidmartin.wordpress.com/
 **/

(function() {
    "use strict";
    
    /*
     * @namespace window.AcidJs
     **/
    if(undefined === window.AcidJs) {
        window.AcidJs = {};
    }
    
    /*
     * @constructor
     * @param urls (Object) set a predefined key-value pairs of urls/shorturls
     * {
     *  "url": "shorturl"
     * }
     **/
    function Shortener(urls) {
        this.urls = urls || {};
    }
    
    Shortener.prototype = {
        /*
         * @member APIS (Object)
         * @public
         **/
        APIS: {
            tinyurl: "http://tinyurl.com/api-create.php?url="
        },
        
        /*
         * @method get
         * @public
         * @param longUrl (String) URL that should be shortened
         * @param callback (Function) [optional] callback that will be executed after the URL has been processed
         * @param apiToUse (String) [optional] which shortening service should be used (default is "tinyurl")
         **/
        get: function(longUrl, callback, apiToUse) {
            var
                that = this,
                service = apiToUse ? this.APIS[apiToUse] : this.APIS.tinyurl;
            
            if(this.urls[longUrl]) {
                if(callback) {
                    callback.call(this, this.urls[longUrl]);
                    return;
                }
            }
            
            $.when($.ajax({
                    url: "pages/javascript-api-for-tinyurl/example/AcidJs.Shortener/php/Shortener.php",
                    data: "url=" + window.encodeURIComponent(longUrl) + 
                        "&service=" + window.encodeURIComponent(service),
                    dataType: "json"
                })).then(function(rsp){ 
                    that.set(longUrl, rsp[longUrl], callback);
                });
        },
        
        /*
         * @method set cache the short url in the urls object and reuse it from the DOM storage if requested again
         * @private
         * @param key (String) original expanded URL
         * @param value (String) shortened url
         * @param callback (Function) [optional] execute the callback, if set in the get() method
         **/
        set: function(key, value, callback) {
            this.urls[key] = value;
            if(callback) {
                callback.call(this, this.urls[key]);
            }
        }
    };
    
    window.AcidJs.Shortener = Shortener;
})();
<?php
header('Content-type: text/json');
ob_start('ob_gzhandler');

$url = $_REQUEST['url'];
$service = $_REQUEST['service'];

if($url && $service) {
    $tinyurl = file_get_contents($service.$url);

    echo '{"'.$url.'":"'.$tinyurl.'"}';
}
?>

Please, disable your ad blocker

This website is made possible by displaying online advertisements to the visitors. To view the content, please, disable your ad blocker, then refresh the page.