Skip to content

Commit

Permalink
partial work towards multi api list
Browse files Browse the repository at this point in the history
  • Loading branch information
daviortega committed Jun 6, 2018
1 parent 9f7944b commit f18b183
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 36 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"nyc": "^11.3.0"
},
"dependencies": {
"figlet": "^1.2.0",
"through2": "^2.0.3"
}
}
10 changes: 6 additions & 4 deletions src/Genes.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ class Genes extends NodeMist3 {
return gene.aseq_id === item.id
})[0]
if (!gene.ai) {
this.log.warn(`Aseq ${gene.aseq_id} not found`)
if (options.keepGoing === false) {
this.log.error(`Aseq ${gene.aseq_id} not found`)
throw Error(`Aseq ${gene.aseq_id} not found`)
}
else {
this.log.warn(`Aseq ${gene.aseq_id} not found`)
}
}
}
else {
Expand Down Expand Up @@ -157,12 +159,12 @@ class Genes extends NodeMist3 {
})
}

byGenome(version) {
byGenomeVersion(version) {
const allGenes = []
let page = 1
const getGenes = (v, p) => {
return new Promise((resolve, rejectg) => {
this.byGenomePerPage(v, p)
this.byGenomeVersionPerPage(v, p)
.then((newGenes) => {
if (newGenes.length !== 0) {
newGenes.forEach((gene) => {
Expand All @@ -180,7 +182,7 @@ class Genes extends NodeMist3 {
return getGenes(version, page)
}

byGenomePerPage(version, page = 1) {
byGenomeVersionPerPage(version, page = 1) {
const genes = []
const genesPerPage = 100
this.httpOptions.method = 'GET'
Expand Down
54 changes: 27 additions & 27 deletions src/Genes.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ const should = chai.should()
const Genes = require('./Genes')

describe('Genes', function() {
describe('byGenomes', function() {
describe('byGenomeVersion', function() {
it('should pass', function() {
this.timeout(14000)
const genes = new Genes()
const expectedNumberOfGenes = 736
const genome = 'GCF_000701865.1' // 'GCF_000006765.1'
return genes.byGenome(genome).then((items) => {
return genes.byGenomeVersion(genome).then((items) => {
expect(items.length).eql(expectedNumberOfGenes)
})
})
Expand All @@ -29,41 +29,41 @@ describe('Genes', function() {
describe('info', function() {
it('should pass', function() {
const genes = new Genes()
const geneVersion = 'GCF_000302455.1-A994_RS01985'
return genes.info(geneVersion).then((item) => {
expect(item.stable_id).eql(geneVersion)
const stableId = 'GCF_000302455.1-A994_RS01985'
return genes.info(stableId).then((item) => {
expect(item.stable_id).eql(stableId)
})
})
it('should throw error when fed with invalid id', function() {
const genes = new Genes()
const geneVersion = 'GCF_000302455.1-A994_RS0198'
return genes.info(geneVersion).should.be.rejectedWith(Error)
const stableId = 'GCF_000302455.1-A994_RS0198'
return genes.info(stableId).should.be.rejectedWith(Error)
})
it.skip('should throw error when fed with empty id', function() {
const genes = new Genes()
const geneVersion = ''
return genes.info(geneVersion).should.be.rejectedWith(Error)
const stableId = ''
return genes.info(stableId).should.be.rejectedWith(Error)
})
})
describe('infoAll', function() {
it('should pass', function() {
const genes = new Genes()
const geneVersions = ['GCF_000302455.1-A994_RS01985', 'GCF_000302455.1-A994_RS00010']
return genes.infoAll(geneVersions).then((items) => {
const stableIds = ['GCF_000302455.1-A994_RS01985', 'GCF_000302455.1-A994_RS00010']
return genes.infoAll(stableIds).then((items) => {
items.forEach((item, i) => {
expect(item.stable_id).eql(geneVersions[i])
expect(item.stable_id).eql(stableIds[i])
})
})
})
it('should be rejected with invalid stable id', function() {
const genes = new Genes()
const geneVersions = ['GCF_000302455.1-A994_RS01985', 'xxxGCF_000302455.1-A994_RS00010']
return genes.infoAll(geneVersions).should.be.rejectedWith('Not Found')
const stableIds = ['GCF_000302455.1-A994_RS01985', 'xxxGCF_000302455.1-A994_RS00010']
return genes.infoAll(stableIds).should.be.rejectedWith('Not Found')
})
it('should pass with bunch', function() {
this.timeout(14000)
const genes = new Genes()
const geneVersions = [
const stableIds = [
'GCF_000006765.1-PA1072',
'GCF_000006765.1-PA1073',
'GCF_000006765.1-PA1074',
Expand Down Expand Up @@ -230,9 +230,9 @@ describe('Genes', function() {
'GCF_000196175.1-BD_RS15605',
'GCF_000196175.1-BD_RS15610'
]
return genes.infoAll(geneVersions).then((items) => {
return genes.infoAll(stableIds).then((items) => {
items.forEach((item, i) => {
expect(item.stable_id).eql(geneVersions[i])
expect(item.stable_id).eql(stableIds[i])
})
})
})
Expand Down Expand Up @@ -267,8 +267,8 @@ describe('Genes', function() {
describe('addAseqInfo', function() {
it('should pass', function() {
const genes = new Genes()
const geneVersion = 'GCF_000302455.1-A994_RS01985'
return genes.info(geneVersion).then((gene) => {
const stableId = 'GCF_000302455.1-A994_RS01985'
return genes.info(stableId).then((gene) => {
return genes.addAseqInfo([gene]).then((result) => {
expect(result[0].ai).to.not.be.null
expect(result.length).eql(1)
Expand All @@ -277,24 +277,24 @@ describe('Genes', function() {
})
it('should reject if gene is not found', function() {
const genes = new Genes()
const geneVersion = 'GCF_000302455.1-A994_RS01985'
return genes.info(geneVersion).then((gene) => {
const stableId = 'GCF_000302455.1-A994_RS01985'
return genes.info(stableId).then((gene) => {
gene.aseq_id = 'wTCio8ibKOlaJ_LDGhkSVA'
return genes.addAseqInfo([gene], {keepGoing: false}).should.be.rejectedWith('not found')
})
})
it('should reject if aseq is null', function() {
const genes = new Genes()
const geneVersion = 'GCF_000302455.1-A994_RS01985'
return genes.info(geneVersion).then((gene) => {
const stableId = 'GCF_000302455.1-A994_RS01985'
return genes.info(stableId).then((gene) => {
gene.aseq_id = null
return genes.addAseqInfo([gene], {keepGoing: false}).should.be.rejectedWith('has null aseq')
})
})
it('should pass (with warning) null if aseq_id is null and told to keep going', function() {
const genes = new Genes()
const geneVersion = 'GCF_000302455.1-A994_RS01985'
return genes.info(geneVersion).then((gene) => {
const stableId = 'GCF_000302455.1-A994_RS01985'
return genes.info(stableId).then((gene) => {
gene.aseq_id = null
return genes.addAseqInfo([gene], {keepGoing: true}).then((results) => {
expect(results[0].ai).to.be.null
Expand All @@ -303,8 +303,8 @@ describe('Genes', function() {
})
it('should warns if gene is not found and asked to not throw error in options', function() {
const genes = new Genes()
const geneVersion = 'GCF_000302455.1-A994_RS01985'
return genes.info(geneVersion).then((gene) => {
const stableId = 'GCF_000302455.1-A994_RS01985'
return genes.info(stableId).then((gene) => {
gene.aseq_id = 'wTCio8ibKOlaJ_LDGhkSVA'
return genes.addAseqInfo([gene], {keepGoing: true}).then((results) => {
expect(results[0].ai).to.be.undefined
Expand Down
29 changes: 24 additions & 5 deletions src/NodeMist3Abstract.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
'use strict'

let httpDefaultOptions = {
method: 'GET',
hostname: 'api.mistdb.com',
port: 5000,
const https = require('https')

const apiHostnameList = [
'api.mistdb.com',
'api.mistdb.caltech.edu'
]

const httpsDefaultOptions = {
headers: {},
agent: false
}

module.exports =
class NodeMist3 {
constructor(options) {
this.httpOptions = options ? options : httpDefaultOptions
this.httpsOptions = options ? options : httpsDefaultOptions
}

init() {
httpsDefaultOptions.method = 'HEAD'
const requests = []
for (const apiHostname of apiHostnameList) {
httpsDefaultOptions.hostname = apiHostname
requests.push(https.request(httpsDefaultOptions))
}
return Promise.all(requests).then((responses) => {
for (const response of responses) {
console.log(response.statusCode)
}
return
})
}
}
18 changes: 18 additions & 0 deletions src/NodeMist3Abstract.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')

chai.use(chaiAsPromised)

const expect = chai.expect
const should = chai.should()

const NodeMist3Abstract = require('./NodeMist3Abstract')

describe.only('NodeMist3Abstract', function() {
it('init should work', function() {
const abstract = new NodeMist3Abstract()
return abstract.init()
})
})

0 comments on commit f18b183

Please sign in to comment.