Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ usages in the [src/examples directory][src-examples].
## Development

Versioning: `npm version [0.0.xx-development]`
Publishing: `npm publish`
Publishing: `npm publish --tag development`

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "syncromatics-track-api",
"version": "3.35.0",
"version": "3.36.0",
"description": "Library to interact with the Syncromatics Track API",
"main": "dist/index.js",
"scripts": {
Expand Down
23 changes: 23 additions & 0 deletions src/examples/get_stops.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ describe('When searching for stops by name', () => {

return stopsPromise;
});

it('should get a list of stops with patternHrefs', () => {
api.logIn({ username: 'charlie@example.com', password: 'securepassword' });

const stopsPromise = api.customer('SYNC').stops()
.withQuery('1st')
.include('patternHrefs') // Include patternHrefs associated with each stop
.getPage()
.then(page => page.list)
.then(stops => stops); // Do things with list of stops

return stopsPromise;
});
});

describe('When retrieving a stop by ID', () => {
Expand All @@ -45,6 +58,16 @@ describe('When retrieving a stop by ID', () => {

return stopsPromise;
});

it('should get a stop with patternHrefs', () => {
api.logIn({ username: 'charlie@example.com', password: 'securepassword' });

const stopPromise = api.customer('SYNC').stop(1)
.fetch({ include: 'patternHrefs' }) // Include patternHrefs associated with the stop
.then(stop => stop); // Do things with stop

return stopPromise;
});
});

describe('When creating a stop', () => {
Expand Down
18 changes: 18 additions & 0 deletions src/mocks/stops.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ const stops = {
Link: '</1/SYNC/stops?page=1&per_page=10&q=1st&sort=>; rel="next", </1/SYNC/stops?page=1&per_page=10&q=1st&sort=>; rel="last"',
},
});
const listResponseWithIncludePatternHrefs = () => new Response(
Client.toBlob(stops.listWithIncludePatternHrefs), {
headers: {
Link: '</1/SYNC/stops?page=1&per_page=10&q=1st&include=patternHrefs&sort=>; rel="next", </1/SYNC/stops?page=1&per_page=10&q=1st&include=patternHrefs&sort=>; rel="last"',
},
});
const singleResponse = () => new Response(Client.toBlob(stops.getById(1)));
const singleResponseWithIncludePatternHrefs = () => new Response(Client.toBlob(stops.getByIdWithIncludePatternHrefs(1)));
const postResponse = () => new Response(undefined, {
headers: {
Location: '/1/SYNC/stops/1',
Expand All @@ -25,19 +32,30 @@ const stops = {

fetchMock
.get(client.resolve('/1/SYNC/stops?page=1&per_page=10&q=1st&sort='), listResponse)
.get(client.resolve('/1/SYNC/stops?page=1&per_page=10&q=1st&include=patternHrefs&sort='), listResponseWithIncludePatternHrefs)
.get(client.resolve('/1/SYNC/stops/1'), singleResponse)
.get(client.resolve('/1/SYNC/stops/1?include=patternHrefs'), singleResponseWithIncludePatternHrefs)
.post(client.resolve('/1/SYNC/stops'), postResponse)
.get(client.resolve('/1/SYNC/stops?latitude=40.7128&longitude=-74.006&distanceMeters=200'), nearbyResponse)
.put(client.resolve('/1/SYNC/stops/1'), putResponse);
},
getById: id => stops.list.find(v => v.id === id),
getByIdWithIncludePatternHrefs: id => stops.listWithIncludePatternHrefs.find(v => v.id === id),
list: [{
href: '/1/SYNC/stops/1',
id: 1,
name: '1st/Main',
latitude: 34.081728,
longitude: -118.351585,
}],
listWithIncludePatternHrefs: [{
href: '/1/SYNC/stops/1',
id: 1,
name: '1st/Main',
latitude: 34.081728,
longitude: -118.351585,
patternHrefs: ['/1/SYNC/patterns/1', '/1/SYNC/patterns/2'],
}],
};

export default stops;
13 changes: 11 additions & 2 deletions src/resources/Stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,19 @@ class Stop extends Resource {

/**
* Fetches the data for this stop via the client
* @param {Object} [options] Options for including additional data
* @param {string} [options.include] Comma-separated list of fields to include in the response
* @returns {Promise} If successful, a hydrated instance of this stop
*/
fetch() {
return this.client.get(this.href)
fetch(options = {}) {
const { include } = options;
let url = this.href;

if (include) {
url += `?include=${include}`;
}

return this.client.get(url)
.then(response => response.json())
.then(stop => new Stop(this.client, this, stop));
}
Expand Down
12 changes: 12 additions & 0 deletions src/resources/StopsContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ class StopsContext extends PagedContext {
return this;
}

/**
* Include additional information in the response
* @param {string} term - The term to include
* @returns {StopsContext} Returns itself
*/
include(term) {
const include = this.params.include ? this.params.include.split(',') : [];
include.push(term);
this.params.include = include.join(',');
return this;
}

/**
* Gets the first page of results for this context
* @returns {Promise} If successful, a page of Stop objects
Expand Down