Skip to content

Commit

Permalink
feat(enplugScreenshotAndConfiguration): Add calls for getting screens…
Browse files Browse the repository at this point in the history
…hots and updating config (#70)

* feat(enplugScreenshotAndConfiguration): Add calls for getting an enplug screenshot and updating configuration

* mock put call for configuration update

* mock response empty

* add customer helper functions for usage

* remove hash
  • Loading branch information
k-grant authored Dec 2, 2019
1 parent 3461c46 commit f83852f
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/resources/Customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import Driver from './Driver';
import DriversContext from './DriversContext';
import ExternalApi from './ExternalApi';
import ExternalApisContext from './ExternalApisContext';
import EnplugScreenshot from './EnplugScreenshot';
import EnplugConfiguration from './EnplugConfiguration';
import Message from './Message';
import MessagesContext from './MessagesContext';
import Pattern from './Pattern';
Expand Down Expand Up @@ -182,6 +184,27 @@ class Customer extends Resource {
return this.resource(Driver, Driver.makeHref(this.code, id));
}

/**
* Gets an enplug screenshot by id
* @param {String} serial Serial of the Enplug device
* @returns {EnplugScreenshot} EnplugScreenshot resource
*/
enplugScreenshot(serial) {
return this.resource(EnplugScreenshot, EnplugScreenshot.makeHref(this.code, serial));
}

/**
* Gets an enplug configuration for customer with fields
* @param {Object} [configOptions] Values for this enplug configuration.
* @returns {EnplugConfiguration} EnplugConfiguration resource
*/
enplugConfiguration(configOptions) {
const options = {
...EnplugConfiguration.makeHref(this.code, configOptions.deviceSerial),
...configOptions };
return this.resource(EnplugConfiguration, options);
}

/**
* Gets a context for querying this customer's external APIs
* @returns {ExternalApisContext} Context for querying this customer's external APIs
Expand Down
59 changes: 59 additions & 0 deletions src/resources/EnplugConfiguration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import Resource from './Resource';

/**
* EnplugConfiguration resource
*/
class EnplugConfiguration extends Resource {
/**
* Creates a new enplugConfiguration
*
* Will populate itself with the values given to it after the client parameter
* @example <caption>Assigning partial enplug configuration data to a new instance</caption>
* const client = new Client();
* const partialEnplugConfiguration = {
* href: '/1/SYNC/enplugs/LLM3/configuration',
* volume: 27,
* vehicleHref: '/1/SYNC/vehicles/1234',
* };
* const enplugConfiguration = new EnplugConfiguration(client, partialEnplugConfiguration);
*
* enplugConfiguration.hydrated == true;
* @param {Client} client Instance of pre-configured client
* @param {Array} rest Remaining arguments to use in assigning values to this instance
*/
constructor(client, ...rest) {
super(client);

const newProperties = Object.assign({}, ...rest);
const hydrated = !Object.keys(newProperties).every(k => k === 'href');

Object.assign(this, newProperties, {
hydrated,
});
}

/**
* Makes a href for a given customer code and ID
* @param {string} customerCode Customer code
* @param {string} deviceSerial Enplug Serial
* @returns {string} URI to instance of enplug configuration
*/
static makeHref(customerCode, deviceSerial) {
return {
href: `/1/${customerCode}/enplugs/${deviceSerial}/configuration`,
};
}

/**
* Updates configuration for an enplug via the client
* @returns {Promise} if successful returns instance of this user
*/
update() {
// eslint-disable-next-line no-unused-vars
const { client, hydrated, href, ...body } = this;
return this.client.put(href, { body })
.then(() => new EnplugConfiguration(this.client, { ...this }));
}
}

export default EnplugConfiguration;
54 changes: 54 additions & 0 deletions src/resources/EnplugConfiguration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import chai from 'chai';
import fetchMock from 'fetch-mock';

import chaiAsPromised from 'chai-as-promised';
import Client from '../Client';
import EnplugConfiguration from './EnplugConfiguration';

chai.should();
chai.use(chaiAsPromised);

describe('When instantiating an enplug configuration based on customer and enplug serial', () => {
const client = new Client();
const enplugConfiguration = new EnplugConfiguration(client, EnplugConfiguration.makeHref('SYNC', 'SERIAL11'));

it('should set the href', () => enplugConfiguration.href.should.equal('/1/SYNC/enplugs/SERIAL11/configuration'));
it('should not be hydrated', () => enplugConfiguration.hydrated.should.equal(false));
});

describe('When instantiating a vehicle media based on an object', () => {
const client = new Client();
const mockConfiguration = new EnplugConfiguration(client, { deviceSerial: 'LLCM2', volume: 28, shouldSwitch: true, vehicleHref: '/1/SYNC/vehicles/1234' });
const enplugConfiguration = new EnplugConfiguration(client, mockConfiguration);

it('should set the volume', () => enplugConfiguration.volume.should.equal(28));
it('should set the deviceSerial', () => enplugConfiguration.deviceSerial.should.equal('LLCM2'));
it('should set the vehicleHref', () => enplugConfiguration.vehicleHref.should.equal('/1/SYNC/vehicles/1234'));
it('should set the shouldSwitch', () => enplugConfiguration.shouldSwitch.should.equal(true));
});

describe('When updating enplug configuration', () => {
const client = new Client();
const mockConfiguration = new EnplugConfiguration(client, {
href: '/1/SYNC/enplugs/LLCM2/configuration',
code: 'SYNC',
deviceSerial: 'LLCM2',
volume: 28,
shouldSwitch: true,
vehicleHref: '/1/SYNC/vehicles/1234' });

const singleResponse = () => new Response();
beforeEach(() => {
fetchMock.put(client.resolve('/1/SYNC/enplugs/LLCM2/configuration'), singleResponse);
});
beforeEach(() => fetchMock.catch(503));

let promise;
beforeEach(() => {
promise = new EnplugConfiguration(client, mockConfiguration).update();
});

it('should resolve the promise', () => promise.should.be.fulfilled);
it('should set the volume', () => promise.then(v => v.volume).should.eventually.equal(28));
it('should set the deviceSerial', () => promise.then(v => v.deviceSerial).should.eventually.equal('LLCM2'));
});
61 changes: 61 additions & 0 deletions src/resources/EnplugScreenshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import Resource from './Resource';

/**
* EnplugScreenshot resource
*/
class EnplugScreenshot extends Resource {
/**
* Creates a new enplugScreenshot
*
* Will populate itself with the values given to it after the client parameter
* @example <caption>Assigning partial enplug screenshot data to a new instance</caption>
* const client = new Client();
* const partialEnplugScreenshot = {
* href: '/1/SYNC/enplugs/LLM3/screenshot',
* contentType: 'image/jpeg',
* name: '1',
* };
* const enplugScreenshot = new EnplugScreenshot(client, partialEnplugScreenshot);
*
* enplugScreenshot.hydrated == true;
* @param {Client} client Instance of pre-configured client
* @param {Array} rest Remaining arguments to use in assigning values to this instance
*/
constructor(client, ...rest) {
super(client);

const newProperties = Object.assign({}, ...rest);
const hydrated = !Object.keys(newProperties).every(k => k === 'href');

Object.assign(this, newProperties, {
hydrated,
});
}

/**
* Makes a href for a given customer code and ID
* @param {string} customerCode Customer code
* @param {string} deviceSerial Enplug Serial
* @returns {string} URI to instance of enplug screenshot
*/
static makeHref(customerCode, deviceSerial) {
return {
href: `/1/${customerCode}/enplugs/${deviceSerial}/screenshot`,
};
}

/**
* Fetches the data for this enplug screenshot via the client
* @returns {Promise} If successful, a hydrated instance of this enplug screenshot
*/
fetch() {
return this.client.get(this.href)
.then(response => new EnplugScreenshot(this.client, this, {
name: response.headers.get('Name'),
contentType: response.headers.get('Content-Type'),
data: response.blob(),
}));
}
}

export default EnplugScreenshot;
15 changes: 15 additions & 0 deletions src/resources/EnplugScreenshot.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import Client from '../Client';
import EnplugScreenshot from './EnplugScreenshot';

chai.should();
chai.use(chaiAsPromised);

describe('When instantiating an enplug screenshot based on customer and enplug serial', () => {
const client = new Client();
const enplugScreenshot = new EnplugScreenshot(client, EnplugScreenshot.makeHref('SYNC', 'SERIAL11'));

it('should set the href', () => enplugScreenshot.href.should.equal('/1/SYNC/enplugs/SERIAL11/screenshot'));
it('should not be hydrated', () => enplugScreenshot.hydrated.should.equal(false));
});

0 comments on commit f83852f

Please sign in to comment.