Class Resource
Extends
EventEmitter.
Base class for Spire API Resources
Defined in: resource.js.
| Constructor Attributes | Constructor Name and Description |
|---|---|
|
Resource(spire, data)
Base class for objects repreenting resources in the spire api.
|
| Field Attributes | Field Name and Description |
|---|---|
|
Actual data from the spire.io api.
|
|
|
Reference to spire object.
|
| Method Attributes | Method Name and Description |
|---|---|
|
authorization(resource, method)
Returns the Authorization header for this resource, or for another capability
if one is passed in.
|
|
|
Returns the capabilities for the resource.
|
|
|
capability(method)
Returns the capability for the resource and method
|
|
| <static> |
Resource.defineRequest(obj, name, fn)
Creates a request method on given object.
|
|
get(cb)
Gets the resource. |
|
|
getIfCapable(cb)
Gets the resource if we have the get capability. |
|
|
mediaType(name)
Returns the MIME type for this resource or the resource of a given name.
|
|
|
request()
Makes the request with the given name. |
|
|
schema(name)
Returns the resource schema for this resource the resource of a given name.
|
|
|
update(data, cb)
Updates (puts to) the message. |
|
|
url()
Returns the url for the resource.
|
Resources have methods for making requests to the spire api. These methods are defined with `Request.defineRequest`. Note that this is a method on the Request object itself, not the prototype. It is used to create methods on the prototype of Resource classes.
The `Resource` class provides default requests for the `get`, `put`, `delete`, and `post` methods. These methods can be overwritten by subclasses.
Once a request method has been defined, it can be run with
resource.request();
Such request methods have no side effects, and return JSON objects direct from the spire api.
- Parameters:
- {object} spire
- Spire object
- {object} data
- Resource data from the spire api
- Parameters:
- {Resource} resource Optional
- Optional resource
- {string} method
- Method
- Returns:
- {string} Authorization header
- Returns:
- {object} Capabilities
- Parameters:
- method
- Returns:
- {string} Capability
defineRequest(somePrototype, 'get', createGetReq);
is run by calling
resource.request('get', callback);
`fn` is a function that takes any number of arguments and returns a hash
describing the http request we are about to send. Any arguments to this
function can be passed to the `request` method.
For example, suppose `createGetReq` from above is the following function,
which takes an id number as argument as puts it in the query params:
function createGetReq (id) {
return {
method: 'get',
url: this.url(),
query: { id: id },
};
};
You would call that request like this:
resource.request('get', id, callback);
- Parameters:
- {object} obj
- Object to add the request method to.
- {string} name
- Name of the request method
- {function (*args)} fn
- Function taking any number of arguments, returning an object describing the HTTP request.
Gets the resource.
Default method that may be overwritten by subclasses.
- Parameters:
- {function (err|resource)} cb
- Callback
Gets the resource if we have the get capability. Otherwise just return the resource.
Default method that may be overwritten by subclasses.
- Parameters:
- {function (err|resource)} cb
- Callback
- Parameters:
- {string} name Optional
- Optional name of the resource MIME type to return
- Returns:
- {string} MIME type
Makes the request with the given name.
Arguments are the request name, followed by any number of arguments that will be passed to the function which creates the request description, and a callback.
- Parameters:
- {string} name Optional
- Optional name of the resource schema to return
- Returns:
- {string} Schema
Updates (puts to) the message.
Defined in: message.js.
- Parameters:
- {object} data
- Message data
- {function (err|resource)} cb
- Callback
- Returns:
- {string} url