-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
'use strict'; | ||
|
||
var invariant = require('invariant'), | ||
hat = require('hat'), | ||
makeService = require('../make_service'), | ||
constants = require('../constants'); | ||
|
||
var Styles = module.exports = makeService('MapboxStyles'); | ||
|
||
/** | ||
* To retrieve a listing of styles for a particular account. | ||
* | ||
* @param {Function} callback called with (err, datasets) | ||
* @returns {undefined} nothing, calls callback | ||
* @example | ||
* var MapboxClient = require('mapbox'); | ||
* var client = new MapboxClient('ACCESSTOKEN'); | ||
* client.listStyles(function(err, styles) { | ||
* console.log(datasets); | ||
* // [{ version: 8, | ||
* // name: 'Light', | ||
* // center: [ -77.0469979435026, 38.898634927602814 ], | ||
* // zoom: 12.511766533145998, | ||
* // bearing: 0, | ||
* // pitch: 0, | ||
* // created: '2016-02-09T14:26:15.059Z', | ||
* // id: 'STYLEID', | ||
* // modified: '2016-02-09T14:28:31.253Z', | ||
* // owner: '{username}' }, | ||
* // { version: 8, | ||
* // name: 'Dark', | ||
* // created: '2015-08-28T18:05:22.517Z', | ||
* // id: 'STYILEID', | ||
* // modified: '2015-08-28T18:05:22.517Z', | ||
* // owner: '{username}' }] | ||
* }); | ||
*/ | ||
Styles.prototype.listStyles = function(callback) { | ||
return this.client({ | ||
path: constants.API_STYLES_LIST, | ||
params: { | ||
owner: this.owner | ||
}, | ||
callback: callback | ||
}).entity(); | ||
}; | ||
|
||
/** | ||
* Create a style style, given the style as a JSON object. | ||
* | ||
* @param {Object} style Mapbox GL Style Spec object | ||
* @param {Function} callback called with (err, datasets) | ||
* @returns {undefined} nothing, calls callback | ||
* @example | ||
* var MapboxClient = require('mapbox'); | ||
* var client = new MapboxClient('ACCESSTOKEN'); | ||
* var style = { | ||
* 'version': 8, | ||
* 'name': 'My Awesome Style', | ||
* 'metadata': {}, | ||
* 'sources': {}, | ||
* 'layers': [], | ||
* 'glyphs': 'mapbox://fonts/{owner}/{fontstack}/{range}.pbf' | ||
* }; | ||
* client.createStyle(style, function(err, createdStyle) { | ||
* console.log(createdStyle); | ||
* }); | ||
*/ | ||
Styles.prototype.createStyle = function(style, callback) { | ||
return this.client({ | ||
path: constants.API_STYLES_CREATE, | ||
params: { | ||
owner: this.owner | ||
}, | ||
entity: style, | ||
callback: callback | ||
}).entity(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* eslint no-shadow: 0 */ | ||
'use strict'; | ||
|
||
var test = require('tap').test; | ||
var MapboxClient = require('../lib/services/styles'); | ||
var hat = require('hat'); | ||
|
||
var newStyleFixture = { | ||
'version': 8, | ||
'name': 'My Awesome Style', | ||
'metadata': {}, | ||
'sources': {}, | ||
'layers': [], | ||
'glyphs': 'mapbox://fonts/{owner}/{fontstack}/{range}.pbf' | ||
}; | ||
|
||
test('StyleClient', function(styleClient) { | ||
|
||
if (process.browser) { | ||
styleClient.pass('skipping style api in browser'); | ||
return styleClient.end(); | ||
} | ||
|
||
styleClient.test('#listStyles', function(listStyles) { | ||
|
||
listStyles.test('simple list', function(assert) { | ||
var client = new MapboxClient(process.env.MapboxAccessToken); | ||
assert.ok(client, 'created style client'); | ||
client.listStyles(function(err, styles) { | ||
assert.ifError(err, 'success'); | ||
assert.ok(Array.isArray(styles), 'lists styles'); | ||
styles.forEach(function(style) { | ||
assert.ok(style.id, 'Each style has an id'); | ||
}); | ||
assert.end(); | ||
}); | ||
}); | ||
|
||
listStyles.end(); | ||
}); | ||
|
||
styleClient.test('#createStyle', function(createStyle) { | ||
|
||
createStyle.test('create a style', function(assert) { | ||
var client = new MapboxClient(process.env.MapboxAccessToken); | ||
assert.ok(client, 'created style client'); | ||
newStyleFixture.glyphs = newStyleFixture.glyphs | ||
.replace('{owner}', client.owner); | ||
client.createStyle(newStyleFixture, function(err, style) { | ||
assert.ifError(err, 'success'); | ||
assert.ok(style.id, 'returned style has a valid id'); | ||
assert.end(); | ||
}); | ||
}); | ||
|
||
createStyle.end(); | ||
}); | ||
|
||
styleClient.test('#createStyle', function(createStyle) { | ||
|
||
createStyle.test('create a style', function(assert) { | ||
var client = new MapboxClient(process.env.MapboxAccessToken); | ||
assert.ok(client, 'created style client'); | ||
newStyleFixture.glyphs = newStyleFixture.glyphs | ||
.replace('{owner}', client.owner); | ||
client.createStyle(newStyleFixture, function(err, style) { | ||
assert.ifError(err, 'success'); | ||
assert.ok(style.id, 'returned style has a valid id'); | ||
assert.end(); | ||
}); | ||
}); | ||
|
||
createStyle.end(); | ||
}); | ||
|
||
styleClient.end(); | ||
}); |