CNET JS Code Documentation

Hash: CNETAPI

CNETAPI is the namespace containing all the CNETAPI methods and properties; a collection of functionality designed to interact with CNET's api (http://api.cnet.com) to help with remote retrieval of product data.

Authors

  • Hunter Brown
  • Aaron Newton

CNETAPI Method: register

Adds an application to the CNETAPI list for future requests.

Syntax

CNETAPI.register(applicationName[, options]);
 

Arguments

  1. applicationName - (string) a unique key for your application that maps to the url and (optional) partKey/partTag arguments
  2. options - (object) set of name/value options

Options

  • partKey - (integer or string) your partKey for your application; see http://api.cnet.com. Note: you typically only use a partKey or a partTag, not both.
  • partTag - (string; optional) your partTag for your application; see http://api.cnet.com. Note: you typically only use a partKey or a partTag, not both.
  • apiUrl - (string) defaults to http://api.cnet.com/restApi/v1.0;
  • requestUrl - (string) the url to your application; if set, this url will be sent the request with the api request url encoded as an argument set to "cnetApiRequest", e.g. http://myApplicationUrl?cnetApiRequest=http://api.cnet.com...

Notes

  • For front-end applications, we do not surface the partTag (and you shouldn't use your partKey for any front end development at all). This means that your application should converse with a server-side app that then makes requests to the API, passing along your partTag. This means your partTag is not exposed anywhere in the javascript.

For internal development

If you just want to fool around with the API, all you have to do is register the "default" app with your partKey or partTag:

CNETAPI.register('default', {
    partKey: 'myPartKey', 
    apiUrl: 'http://api.cnet.com/restApi/v1.0' //this is the default
});
//the url is optional, so this does the same thing:
CNETAPI.register('default', {
    partKey: 'myPartKey'
});
 

For external development

You would register your app, no key, and a url pointing to your application server:

CNETAPI.register('default', {
    requestUrl: 'http://foo.cnet.com/myApp'
});
 

Then your application would insert the partKey and forward requests to the API.

For internal development that's still proxied to the api through your application:

CNETAPI.register('myApp', {
    requestUrl: 'http://foo.cnet.com/myApp'
    apiUrl: 'http://internal-api.cnet.com/restApi/v1.0' //this is only for internal CNET development
});
 

Returns

  • nothing

Class: CNETAPI.Utils.Base

Implements

Foundation class for all CNETAPIUtil lookup classes.

Syntax

This class is not designed to be used on its own; you instead should use one of its extensions (see below).

new CNETAPI.Utils.TechProduct(options);
 

Arguments

  1. options - (object) key/value set of options.

Options

  • jsonpOptions - (object) options object passed to jsonp; defaults to data.viewType = 'json' and data.partKey = your application's partKey (see CNETAPI.register);
  • applicationName - (string) the name of your application registered with the CNETAPI (see [CNETAPI.register]); defaults to "default"
  • instantiateResults - (boolean) if true, will attempt to initialize a resultClass object (see next option) with the data returned by requests
  • requlstClass - (Class) a CNETAPI.Object class to initialize with the data returned by the request
  • errorPath - (string) the path in return data to inspect for error messages; defaults to CNETResponse.Error.ErrorMessage.$

Events:

  • onComplete - (function) callback executed whenever a request is complete; passed either an array of data or a string representing an error message.
  • onSuccess - (function) callback executed when no error is returned; always passed an array, though it may be empty.
  • onError - (function) callback executed when an error is returned; always passed a string (whatever the api service returned).

Example

//you have to do this only once on your page
//this is my dev key; get your own!
CNETAPI.register('default', {partKey: 19926949750937665684988687810562});
 
//now our request:
new CNETAPI.Utils.TechProduct({
  onSuccess: function(items){
    var ol = new Element('ol');
    items.each(function(item){
      ol.adopt(new Element('li').setHTML(item.data.Name));
    });
    $('apiResults').adopt(ol);
  }
}).search("Ipod");
 

Notes

Class: CNETAPI.Object

Base class for all CNET API returned objects. Currently it just cleans up the Objects and allows for inspection with its type property.

Implements

Syntax

new CNETAPI.<ObjectType>(item[, options]);
 

Where ObjectType is one of CNETAPI.Object's extended types (TechProduct, SoftwareProduct, NewsStory, etc). See example below.

Arguments

  1. item - (mixed: object or integer) if integer the class will attempt to get the object from the CNETAPI.Utils.* class. If object then the class will use this object as the data (making the assumption that it came from the API).
  2. options - (object) key/value map of options.

Options

  • extraLookupData - (object) key/value pairs for additional parameters to be sent in API requests (siteId for example)
  • type - (string) CNET Type of object. Must related to a CNETAPI.* class (i.e. "TechProduct" = CNETAPI.TechProduct)

Events

  • onSucceess - (function) callback executed whenever results are returned from the CNET API using the get method. passed the instance of the object, the .data value, and the .json value as arguments.
  • onError - (function) callback executed when there is an error retrieving data from the API. passed an error message as argument.

Properties

  • ready - (boolean) true if there is data present in the class
  • json - (object) the raw data passed in or returned by the API
  • data - (object) the cleaned data derived from the JSON (no .$, @, etc.)

Example

//you have to do this only once on your page
//this is my dev key; get your own!
new CNETAPI.register('default', {partKey: 19926949750937665684988687810562});
//now our request:
new CNETAPI.TechProduct(32069546).chain(function(){
  dbug.log("got the Ipod, here's the data: ", this.data);
  alert(this.data.EditorsRating.$);
});
 

Note

CNETAPI.Object Method: get

Gets an item from the CNETAPI.

Syntax

new CNETAPI.TechProduct().get(id);
 

Arguments

  1. id/data - (mixed: integer or JSON object) the object id of the object or an object returned by the CNETAPI.

Returns

  • (object) This instance of a CNETAPI.Object

Note

  • get is not intended to be used if you know the id at initialization but rather as a method of loading data into an instance of a CNETAPI.Object. You can initialize an instance with an id by just passing it to the constructor (new CNETAPI.TechProduct(id)). However, if you already have an instance and wish to load data into it, get is the method you pass your id to.
  • the constructor for CNETAPI.Object can take either the id of an item in the CNET catalog or a JSON object retreived from it. In this manner you can initialize an object into an instance of the class if you already have the data.

Class: CNETAPI.TechProduct

Extends

Syntax

new CNETAPI.TechProduct(id[, options]);
 

Arguments and Options

Class: CNETAPI.SoftwareProduct

Extends

Syntax

new CNETAPI.SoftwareProduct(id[, options]);
 

Arguments and Options

CNETAPI.SoftwareProduct Method: getSet

Gets a product from the CNET API by its set is.

Syntax

mySoftwareProduct.getSet(setId);
 

Arguments

  1. setId - (integer) the id of the set.

Returns

Example

var product = new CNETAPI.SoftwareProduct().getSet(10051892);
 

Class: CNETAPI.Category

Extends

Syntax

new CNETAPI.Category(id[, options]);
 

Arguments and Options

CNETAPI.Category Method: getChildren

Syntax

myCategory.getChildren();
 

Arguments

  1. data - (object, optional) additional lookup data that may be passed to the CNETAPI.
  2. options - (object, optional) additional options specific to this lookup; see the options in CNETAPI.Object

Returns

  • (mixed) either this instance of the CNETAPI.Category or null if the request cannot be made due to configuration error.

Example

myCategory.getChildren().chain(function(){
    myCategory.children.each(function(child){
        console.log(child.data);
    });
});
 

Class: CNETAPI.NewsStory

Extends

Syntax

new CNETAPI.NewsStory(id[, options]);
 

Arguments and Options

Class: CNETAPI.NewsGallery

Extends

Syntax

new CNETAPI.NewsGallery(id[, options]);
 

Arguments and Options

Class: CNETAPI.Utils.TechProduct

Contains methods for getting tech products from the CNET API.

Extends

Syntax

new CNETAPI.Utils.TechProduct(options);
 

Arguments and Options

CNETAPI.Utils.TechProduct Method: search

Retrieves a list of items based on a search string.

Syntax

myTechProductUtils.search(queryTerm[, data]);
 

Arguments

  1. queryTerm - (string) required; the query to search on
  2. data - (object) optional data passed on to JsonP.options.data. The data object will already contain query, partKey, and view.

Returns

  • (object) this instance of CNETAPI.Utils.Techproduct

Example

new CNETAPI.Utils.TechProduct({
    onSuccess: function(items) {
        items.each(function(item){
            console.log(item.data.Name);
        });
    }
}).search("ipod");
 

CNETAPI.Utils.TechProduct Method: get

Gets an individual tech product from the CNET API.

Syntax

myTechProductUtils.get(id[, data])
 

Arguments

  1. id - (integer) the product id to retrieve
  2. data - (object) optional data passed on to JsonP.options.data. The data object will already contain productId, partKey, and view.

Returns

  • (object) this instance of CNETAPI.Utils.Techproduct

Example

new CNETAPI.Utils.TechProduct({
    onSuccess: function(item) {
        console.log(item.data.Name);
    }
}).get(32069546);
 

CNETAPI.Utils.TechProduct Method: getMany

Gets numerous tech products with the ids passed in.

Syntax

myTechProductUtils.getMany(ids[, data]);
 

Arguments

  1. ids - (array of integers) a list of pids to look up.
  2. data - (object) optional data passed on to JsonP.options.data. The data object will already contain productIds, partKey, and view.

Returns

  • (object) this instance of CNETAPI.Utils.Techproduct

Example

new CNETAPI.Utils.TechProduct({
    onSuccess: function(items) {
        items.each(function(item){
            console.log(item.data.Name);
        });
    }
}).getMany([32069546, 32069547, 32069548]);
 

Class: CNETAPI.Utils.SoftwareProduct

Contains methods for getting software products from the CNET API.

Extends

Syntax

new CNETAPI.Utils.SoftwareProduct(options);
 

Arguments and Options

CNETAPI.Utils.SoftwareProduct Method: search

Retrieves a list of items based on a search string.

Syntax

mySoftwareProductUtils.search(queryTerm[, data]);
 

Arguments

  1. queryTerm - (string) required; the query to search on
  2. data - (object) optional data passed on to JsonP.options.data. The data object will already contain query, partKey, and view.

Returns

  • (object) this instance of CNETAPI.Utils.Softwareproduct

Example

new CNETAPI.Utils.SoftwareProduct({
    onSuccess: function(items) {
        items.each(function(item){
            console.log(item.data.Name);
        });
    }
}).search("winzip");
 

CNETAPI.Utils.SoftwareProduct Method: get

Gets an individual software product from the CNET API.

Syntax

mySoftwareProductUtils.get(id[, data])
 

Arguments

  1. id - (integer) the product id to retrieve
  2. data - (object) optional data passed on to JsonP.options.data. The data object will already contain productId, partKey, and view.

Returns

  • (object) this instance of CNETAPI.Utils.Softwareproduct

Example

new CNETAPI.Utils.SoftwareProduct({
    onSuccess: function(item) {
        console.log(item.data.Name);
    }
}).get(32069546);
 

CNETAPI.Utils.SoftwareProduct Method: getMany

Gets numerous software products with the ids passed in.

Syntax

mySoftwareProductUtils.getMany(ids[, data]);
 

Arguments

  1. ids - (array of integers) a list of pids to look up.
  2. data - (object) optional data passed on to JsonP.options.data. The data object will already contain productIds, partKey, and view.

Returns

  • (object) this instance of CNETAPI.Utils.SofwtareProduct

Example

new CNETAPI.Utils.SoftwareProduct({
    onSuccess: function(items) {
        items.each(function(item){
            console.log(item.data.Name);
        });
    }
}).getMany([32069546, 32069547, 32069548]);
 

CNETAPI.Utils.SoftwareProduct Method: getSet

Gets an individual software product from the CNET API by it's set id.

Syntax

mySoftwareProductUtils.getSet(id[, data])
 

Arguments

  1. id - (integer) the product id to retrieve
  2. data - (object) optional data passed on to JsonP.options.data. The data object will already contain productId, partKey, and view.

Returns

  • (object) this instance of CNETAPI.Utils.Softwareproduct

Example

new CNETAPI.Utils.SoftwareProduct({
    onSuccess: function(item) {
        console.log(item.data.Name);
    }
}).getSet(32069546);
 

CNETAPI.Utils.SoftwareProduct Method: getManySets

Gets numerous software product by their set ids passed in.

Syntax

mySoftwareProductUtils.getManySets(ids[, data]);
 

Arguments

  1. ids - (array of integers) a list of pids to look up.
  2. data - (object) optional data passed on to JsonP.options.data. The data object will already contain productIds, partKey, and view.

Returns

  • (object) this instance of CNETAPI.Utils.SofwtareProduct

Example

new CNETAPI.Utils.SoftwareProduct({
    onSuccess: function(items) {
        items.each(function(item){
            console.log(item.data.Name);
        });
    }
}).getManySets([32069546, 32069547, 32069548]);
 

Class: CNETAPI.Utils.NewsStory

Extends

Contains methods for getting news stories from the CNET API.

See

Class: CNETAPI.Utils.NewsGallery

Extends

Contains methods for getting news galleries from the CNET API.

See

Class: CNETAPI.Utils.Category

Contains methods for getting catgories from the CNET API.

Extends

Note

  • For Categories, you must either pass in a siteId as an option on instantiation or pass in siteId in the data object on requests.

Syntax

See CNETAPI.Utils.Base

Options

  • everything in CNETAPI.Utils.Base
  • siteId - (integer) required site id for category selection.

CNETAPI.Utils.Category Method: search

Retrieves a list of items based on a search string.

Syntax

myCategoryUtils.search(queryTerm[, data]);
 

Arguments

  1. queryTerm - (string) the query to search on
  2. type - (string) either TechProduct, SoftwareProduct, NewsStory, or NewsGallery; see below
  3. data - (object) optional data passed on to JsonP.options.data. The data object will already contain results: 1, iod: relatedCast, and the query term.

Returns

  • (object) this instance of CNETAPI.Utils.Category

Example

new CNETAPI.Utils.Category({
    onSuccess: function(items) {
        items.each(function(item){
            console.log(item.data.Name);
        });
    }
}).search("ipod");
 

Note

  • Currently this search only works for TechProducts.

CNETAPI.Utils.Category Method: get

Gets an individual category from the CNET API.

Syntax

myCategoryUtils.get(id[, data])
 

Arguments

  1. id - (integer) the category id to retrieve
  2. data - (object) optional data passed on to JsonP.options.data. The data object will already contain categoryId, partKey, and view.

Returns

  • (object) this instance of CNETAPI.Utils.Category

Example

new CNETAPI.Utils.Category({
    onSuccess: function(category) {
        console.log(category.data.Name);
    }
}).get(32069546);
 

CNETAPI.Utils.Category Method: getMany

Gets numerous categories with the ids passed in.

Syntax

myCategoryUtils.getMany(ids[, data]);
 

Arguments

  1. ids - (array of integers) a list of pids to look up.
  2. data - (object) optional data passed on to JsonP.options.data. The data object will already contain categoryIds, partKey, and view.

Returns

  • (object) this instance of CNETAPI.Utils.Category

Example

new CNETAPI.Utils.Category({
    onSuccess: function(items) {
        items.each(function(item){
            console.log(item.data.Name);
        });
    }
}).getMany([32069546, 32069547, 32069548]);
 

CNETAPI.Utils.Category Method: getChildren

Gets the children of a category from the CNET API.

Arguments

  • id - (integer) the id of the parent category to retrieve its children
  • data - (object) optional data passed on to JsonP.options.data. The data object will already contain categoryId, partKey, and view.

Returns

  • (object) this instance of CNETAPI.Utils.Category

Example

new CNETAPI.Utils.Category({
    onSuccess: function(children) {
        children.each(function(child){
            console.log(child.data.Name);
        });
    }
}).getMany([32069546, 32069547, 32069548]);