Golem.de API

Golem  alpha

Golem.de API > Golem > Article.js (source view)
Search:
 
Filters
/*
Copyright (c) 2009, Klass und Ihlenfeld Verlag GmbH
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright 
      notice, this list of conditions and the following disclaimer in the 
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Klass und Ihlenfeld Verlag GmbH nor the names of its
      contributors may be used to endorse or promote products derived from 
      this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*/

if(!Golem.Api.Theme) {

    Golem.Api.Theme = {};

}

/**
 * Get a list of the latest articles of a theme category via Golem web interface
 *
 * @class Article
 * @namespace Golem.Api.Theme
 * @extends Golem.Api
 * @constructor
 *
 * @param {String}  theme        the short name of the theme category
 * @param {Integer} maxArticles the number of top articles
 */
Golem.Api.Theme.Article = function(theme, maxArticles) {

    this.theme = theme;

    if(maxArticles) {

        this.maxArticles = maxArticles;

    }

}

/**
 * The URL of the used web service
 *
 * @property URL
 * @type {String}
 * @protected
 */
Golem.Api.Theme.Article.URL  = 'http://api.golem.de/api/theme/article/';

/**
 * The type of access method for the web service
 *
 * @property TYPE
 * @type {String}
 * @protected
 */
Golem.Api.Theme.Article.TYPE = 'GET';

Golem.Api.Theme.Article.prototype = {

    /**
     * Short name of the theme category
     *
     * @property theme
     * @type {String}
     * @protected
     */
    theme : null,

    /**
     * Count of latest articles to fetch
     *
     * @property maxArticles
     * @type {Integer}
     * @protected
     */
    maxArticles : 10,

    /**
     * Shortcut method for traversing the article data
     *
     * <p>
     * Fetches the articles via <i>fetch()</i> first
     * and then calls <i>fnCallback</i> for every
     * article record like <i>each()</i> does.
     * </p>
     * <p>
     * There is no way to handle errors with this method!
     * </p>
     *
     * @method fetchEach
     *
     * @param {Function} fnCallback - Function to call for every article record
     * @param {Mixed} params - additional data to pass to the callback function
     */
    fetchEach : function(fnCallback, params) {

        var oSelf = this;

        this.fetch(
            {

                fnSuccess : function() {

                    if(fnCallback) {

                        oSelf.each(fnCallback, params);

                    }

                }

            }

        );

    },

    /**
     * Fetches the list of latest articles via web service
     *
     * <p>
     * In case of a successful request, the <i>obj.fnSuccess</i>-callback function
     * will be called. The first argument for the callback method is the result
     * data of the request. The second argument will be the data provided by <i>obj.params</i>.
     * </p>
     * <p>
     * In case of a failed request, the <i>obj.fnError</i> callback function
     * will be called. The first argument to the callback is the error code,
     * the second ist the error message.
     * </p>
     *
     * @method fetch
     *
     * @param {Object} obj - Object literal specifing the callback methods
     */
    fetch : function(obj) {

        var oSelf = this;

        if(null == this.theme) {
            
            throw "Theme category is not given!";

        }

        Golem.Request.doRequest(Golem.Api.Theme.Article.URL+this.theme+'/'+this.maxArticles+'/',
            '',
            function(result) {
                oSelf.data = result.data;

                if(obj.fnSuccess) {

                    var p = undefined;

                    if(obj.params) {

                        p = obj.params;

                    }

                    obj.fnSuccess.call(oSelf, result, p);

                }

            },
            function(errCode, errMsg) {

                oSelf.errorMessage = errMsg;
                oSelf.errorCode    = errCode;

                if(obj.fnError) {

                    var p = undefined;

                    if(obj.params) {

                        p = obj.params;

                    }

                    obj.fnError.call(oSelf, p);

                }
            }, this.timeout
        );

    },

    /**
     * Traverses through the article list.
     *
     * <p>
     * After a succesful request you can traverse
     * through the article list. You pass a callback
     * function to the method, and the method is
     * called for each article
     * </p>
     * <p>
     * The first argument to this callback function is
     * an <i>article</i> object with the properties:
     * <ul>
     * <li><i>articleid</i> - {Integer} - The article identifier</li>
     * <li><i>headline</i> - {String(60)} - The article headline</li>
     * <li><i>abstracttext</i> - {String} - The article abstract</li>
     * <li><i>url</i> - {String} - The article URL</li>
     * <li><i>date</i> - {Integer} - publishing date as unix timestamp</li>
     * <li><i>images</i> - {Integer} - the number of associated images</li>
     * <li><i>leadimg</i> - {Object} - The article headline image
     *     <ul>
     *      <li><i>url</i> - {String} - the image URL</li>
     *      <li><i>width</i> - {Integer} - the image width</li>
     *      <li><i>height</i> - {Integer} - the image height</li>
     *     </ul>
     * </li>     
     * </ul>
     *
     * The second argument will be the data provided by <i>params</i>.
     * </p>
     *
     * @method each
     *
     * @param {Function} fnCallback - the callback function to call for each article
     * @param {Mixed} params - additional data passed to the callback function
     */
    each : function(fnCallback, params) {

        for(var i = 0; i < this.data.length; i++) {

            if(fnCallback) {

                var r = this.data[i];

                fnCallback.call(this, {
                      articleid    : r.articleid,
                      headline     : r.headline,
                      url          : r.url,
                      abstracttext : r.abstracttext,
                      date         : r.date,
                      images       : r.images,
                      leadimg      : r.leadimg
                      },
                      params
                );

            }

        }

    }

}

Golem.Api.Theme.Article.prototype.data    = Golem.Api.prototype.data;
Golem.Api.Theme.Article.prototype.getData = Golem.Api.prototype.getData;

Golem.Api.Theme.Article.prototype.errorMessage    = Golem.Api.prototype.errorMessage;
Golem.Api.Theme.Article.prototype.errorCode       = Golem.Api.prototype.errorCode;
Golem.Api.Theme.Article.prototype.getErrorMessage = Golem.Api.prototype.getErrorMessage;
Golem.Api.Theme.Article.prototype.getErrorCode    = Golem.Api.prototype.getErrorCode;

Golem.Api.Theme.Article.prototype.useTimeout = Golem.Api.prototype.useTimeout;
Golem.Api.Theme.Article.prototype.timeout    = Golem.Api.prototype.timeout;

/**
 * Indicates, that the article limit is out of the valid range
 *
 * @property ERROR_LIMIT
 * @type {Integer}
 */
Golem.Api.Theme.Article.ERROR_LIMIT = 102;

/**
 * Indicates, that no theme category was provided or does not exist
 *
 * @property ERROR_NO_THEME
 * @type {Integer}
 */
Golem.Api.Theme.Article.ERROR_NO_THEME = 103;

Copyright © 2010 Golem.de.