Source: farmaframe-api.js

/**
 * @namespace
 */
var farmaframe = (function() {
	/**
	 * Supported thumbnail sizes
	 * @enum {number}
	 * @memberOf farmaframe
	 */
	var THUMBNAIL_SIZES = {
		/**
		 * The image will be at most 150 pixels on the long side. 
		 */
	    T_150: 150,
	};	
	
	/**
	 * Supported image sizes
	 * @enum {number}
	 * @memberOf farmaframe
	 */
	var IMAGE_SIZES = {
		/**
		 * The image will be at most 150 pixels on the long side. 
		 */
	    T_150: 150,
		/**
		 * The image will be at most 500 pixels on the long side. 
		 */
	    F_500: 500,
	};
	
	/**
	 * Supported languages
	 * @enum {string}
	 * @memberOf farmaframe
	 */
	var LANGUAGES = {
			/**
			 * Dutch
			 */
			NL: 'nl',
			/**
			 * French
			 */
			FR: 'fr',
	};
	
	/**
	 * Represents access to the FarmaFrame api.
	 * @memberOf farmaframe
	 * @constructor
	 * 
	 * @param {Object}		options				
	 * 		Options
	 * @param {string}		options.apiKey		
	 * 		Your api key
	 * @param {string}		options.clientKey	
	 *		Your client key
	 * @param {farmaframe.LANGUAGES}	options.language
	 * 		Preferred content language
	 * @param {string}		[options.baseUrl=https://api.farmaframe.be/v1/]
	 * 		Base url for the api.
	 */
	function ImageApi(options) {
		options = options || {};
	    this.clientKey = options.clientKey || null;
	    this.apiKey = options.apiKey || null;
	    this.language = options.language || null;
	    this.baseUrl = options.baseUrl || "https://api.farmaframe.be/v1/";
	};
	
	ImageApi.prototype =
	/** @lends farmaframe.ImageApi */ 
	{
		/**
		 * Returns a thumbnail url. This does not perform an API call.
		 * 
		 * @param {string}		product
		 * 		The product CNK-code.
		 * @param {farmaframe.THUMBNAIL_SIZES} size
 		 * 		Preferred dimensions of the image.
		 * @returns {string}
		 * 		The url to a thumbnail.
		 */
		getThumbnailUrl: function(product, size) {
		    return this.baseUrl + "image/thumbnail/" + this.language + "/" + product + "/" + size;
		},
		
		/**
		 * Returns the image listings for the specified products.
		 * 
		 * @param {Array.<string>} products
		 * 		One or more product CNK codes.
		 * 
		 * @returns {Deferred} 
		 * 		A jquery deferred response.
		 * 		Deffered.done({Array.ProductImages}) 
		 */
		getImages: function(products) {
			// local reference for inner function
			var apiKey_ = this.apiKey;
			var baseUrl_ = this.baseUrl;
			
			// convert products to an array if it isn't already
			if (!(products instanceof Array)) {
				products = [products];
			}
			
			// split into parts consisting of 100 product-ids
			var requests = [];
			var chunk = 100;
			for (var i=0; i<products.length; i+=chunk) {
		        requests.push(products.slice(i,i+chunk));
			}
			
			// deferred response
			var dfd = $.Deferred();
			
			// api call if at least one product-id exists
			if (requests.length > 0) {
				// keep only the first 100 and silently drop all others
				var parameters = {
					'cnk': requests[0],
					'appkey': this.clientKey,
				};
				
				// perform api call
				apiCall(this.baseUrl + "image/list", this.language, parameters, false)
				.done(function(data) {
					var images = [];
					
					for (var product in data) {
					    if (!data.hasOwnProperty(product)) {
					        continue;
					    }
						images.push(new ProductImages(apiKey_, baseUrl_, data[product]));
					}
					
					dfd.resolve(images);
				})
				.fail(function(data) {
					dfd.reject(data);
				});
			} else {
				// fast path in case no product-ids specified. 
				dfd.resolve([]);
			}
			
			return dfd.promise();
		},
	};
	
	
	/**
	 * Represents a single product image.
	 * @constructor
	 * @memberOf farmaframe
	 * 
	 * @param	{string}	apiKey
	 * 		Your api key.
	 * @param	{sting}		baseUrl
	 * 		Base url for the api.
	 * @param	{string}	product
	 * 		The product CNK-code.
	 * @param	{string}	token
	 * 		The image exchange token that uniquely identifies the image.
	 */
	function ProductImage(apiKey, baseUrl, product, token) {
	    this.apiKey = apiKey;
	    this.baseUrl = baseUrl;
	    
	    this.product = product;
	    this.token = token; 
	}

	ProductImage.prototype =
	/** @lends farmaframe.ProductImage */ 
	{
		/**
		 * Returns the url for this image at the specified dimensions.
		 * 
		 * @param 	{farmaframe.IMAGE_SIZES}	size
		 * 		Preferred dimensions of the image. 
		 * 
		 * @returns {string}
		 * 		The url to an image.
		 */
		getUrl: function(size) {
			return this.baseUrl + "image/get/" + this.apiKey + "/" + this.product + "/" + this.token + "/" + size;	
		}
	};
	
	
	/**
	 * Represents the set of available images for a product.
	 * @constructor
	 * @memberOf farmaframe
	 * 
	 * @param	{string}	apiKey
	 * 		Your api key.
	 * @param	{sting}		baseUrl
	 * 		Base url for the api.
	 * @param	{Object}	json
	 * 		Json response part from the API for a specific product 
	 */
	function ProductImages(apiKey, baseUrl, json) {
		this.baseUrl = baseUrl;
	    this.apiKey = apiKey;
	    
	    this.cnk = json.cnk;
	    this.primaryToken = json.primaryToken;
	    this.secondaryTokens = json.secondaryTokens;
	    this.externalTokens = json.externalTokens;
	    this.unitDoseTokens = json.unitDoseTokens;
	};

	ProductImages.prototype = 
	/** @lends farmaframe.ProductImages */ 
	{
		/**
		 * Returns the CNK code for this product.
		 * 
		 * @returns {number}
		 * 		The product CNK code.
		 */
		getCnkCode: function() {
			return this.cnk;
		},
			
		/**
		 * Returns the primary image for this product. This is the image we find best
		 * represents the product.
		 * 
		 * @returns {farmaframe.ProductImage} 
		 * 		The primary image.
		 */
		getPrimaryImage: function() {
			return new ProductImage(this.apiKey, this.baseUrl, this.cnk, this.primaryToken);
		},

		/**
		 * Returns the secondary images for this product. 
		 * 
		 * @returns {Array.<farmaframe.ProductImage>} 
		 * 		The secondary images.
		 */
		getSecondaryImages: function() {
		    return getImagesFromTokenArray(this.apiKey, this.baseUrl, this.cnk,  this.secondaryTokens);
		},

		/**
		 * Returns the external images for this product. 
		 * 
		 * @returns {Array.<farmaframe.ProductImage>} 
		 * 		The external images.
		 */
		getExternalImages: function() {
		    return getImagesFromTokenArray(this.apiKey, this.baseUrl, this.cnk,  this.externalTokens);
		},

		/**
		 * Returns the unit dose images for this product. 
		 * 
		 * @returns {Array.<farmaframe.ProductImage>} 
		 * 		The unit dose images.
		 */
		getUnitDoseImages: function() {
		    return getImagesFromTokenArray(this.apiKey, this.baseUrl, this.cnk,  this.unitDoseTokens);
		},
		
		/**
		 * Returns all images for this product. Use this if you do not care about the type
		 * of the image.
		 * 
		 * @returns {Array.<farmaframe.ProductImage>} 
		 * 		All available images in order of importance (primary, then secondary,
		 * 		then external, then unit dose)
		 */
		getImages: function() {
		    var tokens = [];
		    tokens.push(this.primaryToken);
		    tokens = tokens.concat(this.secondaryTokens);
		    tokens = tokens.concat(this.externalTokens);
		    tokens = tokens.concat(this.unitDoseTokens);

		    return getImagesFromTokenArray(this.apiKey, this.baseUrl, this.cnk, tokens);
		}
	};	
	
	getImagesFromTokenArray = function(apiKey, baseUrl, cnk, tokens) {
	    var images = [];

	    for (var i=0; i < tokens.length; i++) {
	        images.push(new ProductImage(apiKey, baseUrl, cnk, tokens[i]));
	    }
	    
	    return images;
	};	
	
	apiCall = function(url, language, parameters, json) {
		var options = {
			cache: true,
			headers: {
				'Accept-language': language,
			},
			data: parameters,
			'traditional': true,
		};
		
		if (json) {
			options.jsonp = 'jsonp';
			options.dataType = 'jsonp';
		}
		
		return $.ajax(url, options);	
	};
	
	return {
		THUMBNAIL_SIZES: THUMBNAIL_SIZES,
		IMAGE_SIZES: IMAGE_SIZES,
		LANGUAGES: LANGUAGES,
		ImageApi: ImageApi,
	};
})();