Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajout des pays et leurs territoires #73

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
283 changes: 283 additions & 0 deletions forced_data/pays2016.tsv

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions lib/countries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const SearchableCollection = require('./searchableCollection')

const schema = {
nom: {
type: 'text',
queryWith: 'nom',
ref: 'code'
},
code: {type: 'token', queryWith: 'code'}
}

function getIndexedDb(options = {}) {
/* Source dataset */
const countries = options.countries || require(options.countriesDbPath || '../data/countries.json')

const searchableCollection = new SearchableCollection(schema)
searchableCollection.load(countries)

return searchableCollection
}

module.exports = {getIndexedDb}
8 changes: 8 additions & 0 deletions lib/countryHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const {initFields} = require('./helpers')

const initCountryFields = initFields({
default: ['nom', 'code', 'iso2', 'iso3', 'num', 'territories'],
base: ['nom', 'code']
})

module.exports = {initCountryFields}
118 changes: 118 additions & 0 deletions lib/integration/countries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const fs = require('fs')
const parse = require('csv-parse')
const t = require('through2').obj
const iconv = require('iconv-lite')
const JSONStream = require('JSONStream')
const streamify = require('stream-array')

/* Initialisation */
function init(ctx, next) {
ctx.countries = new Map()

ctx.getCountry = code => {
if (!ctx.countries.has(code)) {
const country = {code, territories: new Set()}
ctx.countries.set(code, country)
}
return ctx.countries.get(code)
}

next()
}

/* Chargement des territoires */
function loadTerritories(options = {}) {
return function (ctx, next) {
ctx.debug('Chargement du jeu de données pays2016 ')
let count = 0
const srcPath = options.srcPath || __dirname + '/../../forced_data/pays2016.tsv'

fs.createReadStream(srcPath)
.pipe(iconv.decodeStream('win1252'))
.on('error', next)
.pipe(parse({delimiter: '\t', columns: true}))
.on('error', next)
.pipe(t((data, enc, cb) => {
const code = data.COG
if (data.ACTUAL === '3') {
const territory = {
nom: data.LIBCOG,
iso2: data.CODEISO2,
iso3: data.CODEISO2,
num: data.CODENUM3
}
ctx.getCountry(code).territories.add(territory)
count++
}
cb()
}))
.on('error', next)
.on('finish', () => {
ctx.debug('Nombre de territoires chargées : %d', count)
next()
})
}
}

/* Chargement des pays */
function loadCountries(options = {}) {
return function (ctx, next) {
ctx.debug('Chargement du jeu de données pays2016 ')
let count = 0
const srcPath = options.srcPath || __dirname + '/../../forced_data/pays2016.tsv'

fs.createReadStream(srcPath)
.pipe(iconv.decodeStream('win1252'))
.on('error', next)
.pipe(parse({delimiter: '\t', columns: true}))
.on('error', next)
.pipe(t((data, enc, cb) => {
const code = data.COG
if (code !== 'XXXXX' && data.ACTUAL !== '3') {
const country = ctx.getCountry(code)
country.nom = data.LIBCOG
country.iso2 = data.CODEISO2
country.iso3 = data.CODEISO3
country.num = data.CODENUM3
count++
}
cb()
}))
.on('error', next)
.on('finish', () => {
ctx.debug('Nombre de pays chargées : %d', count)
next()
})
}
}

/* Sérialisation */
function serialize(options = {}) {
return function (ctx, next) {
ctx.debug('Sérialisation des données')
let count = 0

if (ctx.countries.size < 1) {
return next(new Error('No country'))
}
streamify([...ctx.countries.values()])
.on('error', next)
.pipe(t((country, enc, cb) => {
country.territories = [...country.territories].sort()
count++
cb(null, country)
}))
.on('error', next)
.pipe(JSONStream.stringify())
.on('error', next)
.pipe(fs.createWriteStream(options.destPath || __dirname + '/../../data/countries.json'))
.on('error', next)
.on('finish', () => {
ctx.debug('Nombre de pays écrits : %d', count)
next()
})
}
}

/* Exports */
module.exports = {init, serialize, loadCountries, loadTerritories}
8 changes: 7 additions & 1 deletion lib/integration/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const departements = require('./departements')
const communes = require('./communes')
const regions = require('./regions')
const countries = require('./countries')
const pipeline = require('./pipeline')

/* Pipeline */
Expand All @@ -24,7 +25,12 @@ function integrate(done) {
// Régions
regions.init,
regions.loadRegions(),
regions.serialize()
regions.serialize(),
// Pays
countries.init,
countries.loadCountries(),
countries.loadTerritories(),
countries.serialize()
], done)
}

Expand Down
27 changes: 26 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const morgan = require('morgan')
const {initCommuneFields, initCommuneFormat} = require('./lib/communeHelpers')
const {initDepartementFields} = require('./lib/departementHelpers')
const {initRegionFields} = require('./lib/regionHelpers')
const {initCountryFields} = require('./lib/countryHelpers')
const {formatOne, initLimit} = require('./lib/helpers')
const dbCommunes = require('./lib/communes').getIndexedDb()
const dbDepartements = require('./lib/departements').getIndexedDb()
const dbRegions = require('./lib/regions').getIndexedDb()
const dbCountries = require('./lib/countries').getIndexedDb()
const {pick} = require('lodash')

const app = express()
Expand All @@ -19,7 +21,8 @@ app.use((req, res, next) => {
req.db = {
communes: dbCommunes,
departements: dbDepartements,
regions: dbRegions
regions: dbRegions,
countries: dbCountries
}
next()
})
Expand Down Expand Up @@ -136,6 +139,28 @@ app.get('/regions/:code/departements', initLimit(), initDepartementFields, (req,
}
})

/* Pays */
app.get('/pays', initLimit(), initCountryFields, (req, res) => {
const query = pick(req.query, 'code', 'nom')

if (query.nom) req.fields.add('_score')

res.send(
dbCountries
.search(query)
.map(country => formatOne(req, country))
)
})

app.get('/pays/:code', initCountryFields, (req, res) => {
const countries = dbCountries.search({code: req.params.code})
if (countries.length === 0) {
res.sendStatus(404)
} else {
res.send(formatOne(req, countries[0]))
}
})

/* Definition */
app.get('/definition.yml', (req, res) => {
res.sendFile(__dirname + '/definition.yml')
Expand Down
67 changes: 67 additions & 0 deletions test/countries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-env mocha */
const expect = require('expect.js')
const countries = require('../lib/countries')
const {cloneDeep} = require('lodash')

describe('countries', () => {
let db
const country1 = {nom: 'one', code: '11'}
const country2 = {nom: 'two', code: '22'}
const country3 = {nom: 'three', code: '33'}

beforeEach(done => {
db = countries.getIndexedDb({countries: [country1, country2, country3].map(cloneDeep)})
done()
})

describe('getIndexedDb()', () => {
describe('bad countries db path', () => {
it('should throw an error', () => {
expect(() => countries.getIndexedDb({countriesDbPath: '_'})).to.throwError()
})
})
})

describe('indexes', () => {
describe('indexes list', () => {
const db = countries.getIndexedDb({countries: []});
[
'nom',
'code'
].forEach(index => {
it(`should contain '${index}' index`, () => {
expect(db._indexes).to.have.key(index)
})
})
})
})

describe('search()', () => {
describe('No criteria', () => {
it('should return everything', () => {
expect(db.search()).to.eql([country1, country2, country3])
})
})
describe('Simple matching criteria', () => {
it('should return an array with 1 country', () => {
expect(db.search({code: '11'})).to.eql([country1])
})
})
describe('Disjoint criteria', () => {
it('should return an empty array', () => {
expect(db.search({nom: 'three', code: '22'})).to.eql([])
})
})
describe('All criteria', () => {
it('should return an array with 1 country', () => {
expect(db.search({nom: 'three', code: '33'})).to.eql([
{nom: 'three', code: '33', _score: 1}
])
db.search({nom: 'three', code: '33'}).forEach(reg => {
expect(reg).to.have.key('_score')
expect(reg._score >= 0).to.be.ok()
})
})
})
})
})
42 changes: 42 additions & 0 deletions test/countryHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-env mocha */
const expect = require('expect.js')
const {initCountryFields} = require('../lib/countryHelpers')

describe('countryHelpers', () => {
describe('initCountryFields()', () => {
const runTestCase = (reqParams, expectedFields, done) => {
const req = {query: reqParams.query ? reqParams.query : {}}
initCountryFields(req, undefined, err => {
expect(err).to.be(undefined)
expect(req.fields).to.be.a(Set)
expect([...req.fields].sort()).to.eql(expectedFields.sort())
done()
})
}

// 3 tests volontairement identiques en attendant les futures évolutions
it('empty request should return default fields', done => {
runTestCase(
{},
['nom', 'code', 'iso2', 'iso3', 'num', 'territories'],
done
)
})

it('fields should be read from query', done => {
runTestCase(
{query: {fields: 'nom,code'}},
['nom', 'code'],
done
)
})

it('`nom` and `code` should always be present', done => {
runTestCase(
{query: {fields: 'nom'}},
['nom', 'code'],
done
)
})
})
})
Loading