Skip to content

Commit

Permalink
Get the last numeric data point from RWS data. Fixes tomkr#1
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Kruijsen committed Jan 18, 2014
1 parent 4c2dccf commit 06990cc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/lib.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ zip = require 'node-zip'
xml2js = require 'xml2js'
Location = require('./models').Location

Waterdata =
Waterdata =
# RWS open data url
url: 'http://www.rws.nl/rws/opendata/meetdata/meetdata.zip'

Expand Down Expand Up @@ -42,12 +42,11 @@ Waterdata =
parameterType = 3
name = 1
lastUpdate = 8
lastValue = 5
for data, index in dataArray
metadata = metadataArray[index]
if metadata[parameterType].trim() == 'H10'
#callback(metadata[name], parseInt data[lastValue])
res[res.length] = {lokatie: metadata[name].trim(), waarde:parseInt(data[lastValue]), laatstBijgewerkt: metadata[lastUpdate].trim()}
res[res.length] = {lokatie: metadata[name].trim(), waarde:@getLastValue(data), laatstBijgewerkt: metadata[lastUpdate].trim()}
return res

# Write the new values to the database for storage
Expand All @@ -69,6 +68,13 @@ Waterdata =
# store the data in the database
async.each values, @writeToDb, callback

getLastValue: (values) ->
numbers = values.filter (value) ->
value = value.replace(' ','')
value.length > 0 and (value >=0 or value < 0)
return '-' if numbers.length < 1
parseInt(numbers[numbers.length-1])

Metadata =
getXml: (url, callback) ->
http.get url, (res) ->
Expand Down
20 changes: 20 additions & 0 deletions test/dataTest.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Waterdata = require('../lib/lib').Waterdata
assert = require 'assert'

describe 'handle rws water data', ->

it 'should get the last value if it is numeric', ->
result = Waterdata.getLastValue(['0','1','2','3','4','5'])
assert.equal result, 5

it 'should return a numeric value if the last one is not', ->
result = Waterdata.getLastValue(['0','1','2','3','4','f'])
assert.equal result, 4

it 'should not return an empty string', ->
result = Waterdata.getLastValue(['0','1','2','3','4',''])
assert.equal result, 4

it 'should return a dash if there is no numeric value', ->
result = Waterdata.getLastValue(['n','n','n','n','f'])
assert.equal result, '-'

0 comments on commit 06990cc

Please sign in to comment.