126
126
* Api call to fetch a charm's details, with an optional local cache.
128
128
* @method charmWithCache
129
* @param {String} charmID the charm to fetch This is the fully qualified
130
* charm name in the format scheme:series/charm-revision..
129
* @param {String} charmID the charm to fetch.
131
130
* @param {Object} callbacks the success/failure callbacks to use.
132
131
* @param {Object} bindScope the scope of *this* in the callbacks.
133
132
* @param {ModelList} cache a local cache of browser charms.
135
charm: function(charmID, callbacks, bindScope, cache, defaultSeries) {
134
charm: function(charmID, callbacks, bindScope, cache) {
137
136
callbacks.success = Y.bind(callbacks.success, bindScope);
166
164
@method promiseCharm
167
165
@param {String} charmId to fetch.
168
166
@param {ModelList} cache a local cache of browser charms.
169
@param {String} defaultSeries to resolve.
170
167
@return {Promise} to load charm. Triggered with same result
173
promiseCharm: function(charmId, cache, defaultSeries) {
170
promiseCharm: function(charmId, cache) {
175
172
return Y.Promise(function(resolve, reject) {
176
173
self.charm(charmId, { 'success': resolve, 'failure': reject },
177
self, cache, defaultSeries);
182
Normalize a charm name so we can request its full data. Charm lookup
183
requires a very specific form of the charm identifier.
185
series/charm-revision
187
where revision can currently be any numeric placeholder.
189
@method normalizeCharmId
190
@param {String} charmId to normalize.
191
@param {String} defaultSeries to resolve.
192
@return {String} normalized id.
194
normalizeCharmId: function(charmId, defaultSeries) {
195
var result = charmId;
196
if (/^(cs:|local:)/.exec(result)) {
197
result = result.slice(result.indexOf(':') + 1);
200
if (result.indexOf('/') === -1) {
201
if (!defaultSeries) {
202
console.warn('Unable to normalize charm id, no defaultSeries');
203
defaultSeries = 'precise';
205
result = defaultSeries + '/' + result;
207
if (/\-\d+/.exec(result) === null) {
208
// Add in a revision placeholder
209
result = result + '-1';
215
Promises to return the latest charm ID for a given charm if a newer one
216
exists; this also caches the newer charm if one is available.
218
@method promiseUpgradeAvailability
219
@param {Charm} charm an existing charm potentially in need of an upgrade.
220
@param {ModelList} cache a local cache of browser charms.
221
@return {Promise} with an id or undefined.
223
promiseUpgradeAvailability: function(charm, cache) {
224
// Get the charm's store ID, then replace the version number
225
// with '-HEAD' to retrieve the latest version of the charm.
226
var storeId = charm.get('storeId').replace(/-\d+$/, '-HEAD');
227
return this.promiseCharm(storeId, cache)
228
.then(function(latest) {
229
var latestVersion = parseInt(latest.charm.id.split('-').pop(), 10),
230
currentVersion = parseInt(charm.get('revision'), 10);
231
if (latestVersion > currentVersion) {
232
return latest.charm.id;
240
179
* Api call to search charms