Skip to content

Commit

Permalink
feat(api): Add basic API features
Browse files Browse the repository at this point in the history
The core API methods have been added
  • Loading branch information
sosna committed Mar 9, 2016
1 parent 4ea682c commit 25ccf5e
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{Service} = require './service/service.coffee'
{DataQuery} = require './data/data-query.coffee'
{MetadataQuery} = require './metadata/metadata-query.coffee'
{UrlGenerator} = require './utils/url-generator.coffee'

getService = (id) ->
if typeof id is 'object'
return Service.from id
if typeof id is 'string' and Service[id]
return Service[id]
throw Error "Unknown service #{id}"

getDataQuery = (opts) ->
return DataQuery.from opts

getMetadataQuery = (opts) ->
return MetadataQuery.from opts

getUrl = (query, service) ->
return new UrlGenerator().getUrl query, service

exports.getService = getService
exports.getDataQuery = getDataQuery
exports.getMetadataQuery = getMetadataQuery
exports.getUrl = getUrl
140 changes: 140 additions & 0 deletions test/index.test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
sdmxrest = require '../src/index.coffee'
{ServiceType} = require '../src/service/service-type.coffee'

describe 'API', ->

it 'should have the expected functions', ->
sdmxrest.should.have.property 'getService'
sdmxrest.should.have.property 'getDataQuery'
sdmxrest.should.have.property 'getMetadataQuery'
sdmxrest.should.have.property 'getUrl'

it 'should offer to get an existing service', ->
service = sdmxrest.getService 'ECB'
service.should.be.an 'object'
service.should.have.property('id').that.equals 'ECB'
service.should.have.property('name').that.equals 'European Central Bank'
service.should.have.property('url').that.contains 'sdw-wsrest'
service.should.have.property('api').that.is.not.undefined

it 'should offer to create a service from properties', ->
input = {
id: 'TEST'
url: 'http://test.com'
}
service = sdmxrest.getService input
service.should.be.an 'object'
service.should.have.property('id').that.equals input.id
service.should.have.property('name').that.is.undefined
service.should.have.property('url').that.equals input.url
service.should.have.property('api').that.equals ServiceType.LATEST

it 'should fail if the service is unknown', ->
try
sdmxrest.getService 'UNKNOWN'
assert.fail 'An error should have been triggered'
catch error
error.message.should.contain 'Unknown service'

it 'should fail if the input to service is not of the expected type', ->
try
sdmxrest.getService 2
assert.fail 'An error should have been triggered'
catch error
error.message.should.contain 'Unknown service'

try
sdmxrest.getService undefined
assert.fail 'An error should have been triggered'
catch error
error.message.should.contain 'Unknown service'

it 'should offer to create a data query from properties', ->
input = {
flow: 'EXR'
key: 'A..EUR.SP00.A'
}
query = sdmxrest.getDataQuery input
query.should.be.an 'object'
query.should.have.property('flow').that.equals input.flow
query.should.have.property('key').that.equals input.key
query.should.have.property('provider').that.equals 'all'
query.should.have.property('start').that.is.undefined
query.should.have.property('end').that.is.undefined
query.should.have.property('updatedAfter').that.is.undefined
query.should.have.property('firstNObs').that.is.undefined
query.should.have.property('lastNObs').that.is.undefined
query.should.have.property('obsDimension').that.equals 'TIME_PERIOD'
query.should.have.property('detail').that.equals 'full'
query.should.have.property('history').that.is.false

it 'should fail if input to data query is not of the expected type', ->
try
sdmxrest.getDataQuery undefined
assert.fail 'An error should have been triggered'
catch error
error.message.should.contain 'Not a valid data query'

try
sdmxrest.getDataQuery {test: 'TEST'}
assert.fail 'An error should have been triggered'
catch error
error.message.should.contain 'Not a valid data query'

it 'should offer to create a metadata query from properties', ->
input = {
resource: 'codelist'
id: 'CL_FREQ'
}
query = sdmxrest.getMetadataQuery input
query.should.be.an 'object'
query.should.have.property('resource').that.equals input.resource
query.should.have.property('id').that.equals input.id
query.should.have.property('agency').that.equals 'all'
query.should.have.property('version').that.equals 'latest'
query.should.have.property('item').that.equals 'all'
query.should.have.property('detail').that.equals 'full'
query.should.have.property('references').that.equals 'none'

it 'should fail if input to metadata query is not of the expected type', ->
try
sdmxrest.getMetadataQuery undefined
assert.fail 'An error should have been triggered'
catch error
error.message.should.contain 'Not a valid metadata query'

try
sdmxrest.getMetadataQuery {test: 'TEST'}
assert.fail 'An error should have been triggered'
catch error
error.message.should.contain 'Not a valid metadata query'

it 'should offer to create a URL from a query and a service', ->
query = sdmxrest.getDataQuery {flow: 'EXR', key: 'A.CHF.NOK.SP00.A'}
service = sdmxrest.getService 'ECB'
url = sdmxrest.getUrl query, service
url.should.be.a 'string'
url.should.contain service.url
url.should.contain query.flow
url.should.contain query.key

it 'should fail if input to URL are not of the expected types', ->
try
sdmxrest.getUrl undefined, sdmxrest.getService 'ECB'
assert.fail 'An error should have been triggered'
catch error
error.message.should.contain 'not a valid SDMX data or metadata query'

query = sdmxrest.getDataQuery {flow: 'EXR', key: 'A.CHF.NOK.SP00.A'}

try
sdmxrest.getUrl query, sdmxrest.getService 'TEST'
assert.fail 'An error should have been triggered'
catch error
error.message.should.contain 'Unknown service'

try
sdmxrest.getUrl query
assert.fail 'An error should have been triggered'
catch error
error.message.should.contain 'not a valid service'

0 comments on commit 25ccf5e

Please sign in to comment.