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
- applicationName - (string) a unique key for your application that maps to the url and (optional) partKey/partTag arguments
- 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
- 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
- CNETAPI.Utils.Base is extended into specific object classes. For example, CNETAPI.Utils.TechProduct lets you look up tech products. The utils classes return CNETAPI.Object instances with the same namespace. So for example, CNETAPI.Utils.TechProduct will return instances of CNETAPI.TechProduct.
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
- 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).
- 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 is extended into specific object classes. So for example CNETAPI.TechProduct is a tech product. You can look up / instanciate individual objects by instanciating them or you can use the corresponding CNETAPI.Utils.Base class. So CNETAPI.Utils.TechProduct returns instances of CNETAPI.TechProduct.
CNETAPI.Object Method: get
Gets an item from the CNETAPI.
Syntax
new CNETAPI.TechProduct().get(id);
Arguments
- 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
- See CNETAPI.Object
Class: CNETAPI.SoftwareProduct
Extends
Syntax
new CNETAPI.SoftwareProduct(id[, options]);
Arguments and Options
- See CNETAPI.Object
CNETAPI.SoftwareProduct Method: getSet
Gets a product from the CNET API by its set is.
Syntax
mySoftwareProduct.getSet(setId);
Arguments
- setId - (integer) the id of the set.
Returns
- (object) a CNETAPI.SoftwareProduct with the set id specified.
Example
var product = new CNETAPI.SoftwareProduct().getSet(10051892);
Class: CNETAPI.Category
Extends
Syntax
new CNETAPI.Category(id[, options]);
Arguments and Options
- See CNETAPI.Object
CNETAPI.Category Method: getChildren
Syntax
myCategory.getChildren();
Arguments
- data - (object, optional) additional lookup data that may be passed to the CNETAPI.
- 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
- See CNETAPI.Object
Class: CNETAPI.NewsGallery
Extends
Syntax
new CNETAPI.NewsGallery(id[, options]);
Arguments and Options
- See CNETAPI.Object
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
- queryTerm - (string) required; the query to search on
- 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
- id - (integer) the product id to retrieve
- 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
- ids - (array of integers) a list of pids to look up.
- 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
- queryTerm - (string) required; the query to search on
- 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
- id - (integer) the product id to retrieve
- 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
- ids - (array of integers) a list of pids to look up.
- 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
- id - (integer) the product id to retrieve
- 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
- ids - (array of integers) a list of pids to look up.
- 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
- CNETAPI.Utils.TechProduct for methods and options (they are the same)
Class: CNETAPI.Utils.NewsGallery
Extends
Contains methods for getting news galleries from the CNET API.
See
- CNETAPI.Utils.TechProduct for methods and options (they are the same)
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
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
- queryTerm - (string) the query to search on
- type - (string) either TechProduct, SoftwareProduct, NewsStory, or NewsGallery; see below
- 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
- id - (integer) the category id to retrieve
- 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
- ids - (array of integers) a list of pids to look up.
- 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]);