Skip to content

Commit

Permalink
lat and lng must be numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallford committed Jun 13, 2016
1 parent 8610bde commit c337811
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -15,7 +15,7 @@

* ~~Throttling per provider~~
* ~~lat & lng truncation~~
* Refactor API, avoid nesting
* ~~Refactor API, avoid nesting~~
* Postgres/PostGIS as a provider support
* Concurrency throttling
* Fallback on throttle (fallback in general)
9 changes: 6 additions & 3 deletions src/api.js
Expand Up @@ -7,7 +7,7 @@ import config from '~/config'
import client from '~/redis'

const limiter = new RateLimiter(
config.providers.openstreetmap.limit.number,
config.providers.google.limit.number,
config.providers.google.limit.period,
true
)
Expand All @@ -18,6 +18,9 @@ const api = () => {
if (!req.body.lat || !req.body.lng) {
return res.status(400).json({ message: 'lat and lng value required' })
}
if (!_.isNumber(req.body.lat) || !_.isNumber(req.body.lng)) {
return res.status(400).json({ message: 'lat and lng must be a number' })
}
const input = {
lat: truncate(req.body.lat),
lng: truncate(req.body.lng)
Expand All @@ -30,9 +33,9 @@ const api = () => {
if (error || remainingRequests < 0) {
return res.status(500).json({ message: 'provider rate limit reached' })
}
axios.get(config.providers.openstreetmap.url(input.lat, input.lng, config.providers.openstreetmap.key))
axios.get(config.providers.google.url(input.lat, input.lng, config.providers.google.key))
.then((response) => {
const result = _.get(response, config.providers.openstreetmap.path)
const result = _.get(response, config.providers.google.path)
if (!result) {
return res.status(500).json({ message: 'not a valid provider path or lat/lng' })
}
Expand Down
2 changes: 1 addition & 1 deletion src/server.js
Expand Up @@ -32,5 +32,5 @@ client.on('connect', () => {
})

client.on('error', (error) => {
console.log(error)
console.error(error)
})
10 changes: 4 additions & 6 deletions src/utils.js
@@ -1,16 +1,14 @@
import _ from 'lodash'
import config from '~/config'

const latlng = (lat, lng) => `${lat},${lng}`

const truncate = (strNumber, truncate = config.truncate || 2) => {
strNumber = strNumber.toString()
if (strNumber.indexOf('.') > 0) {
const decimalLength = strNumber.slice(strNumber.indexOf('.'), strNumber.length - 1).length
if (decimalLength < truncate) {
strNumber += '0'.repeat(truncate - decimalLength)
}
return strNumber.slice(0, (strNumber.indexOf('.')) + truncate + 1)
return _.toNumber(strNumber.slice(0, (strNumber.indexOf('.')) + truncate + 1))
}
return strNumber
return _.toNumber(strNumber)
}

export { latlng, truncate }
21 changes: 6 additions & 15 deletions test/truncate.js
Expand Up @@ -4,27 +4,18 @@ import { truncate } from '~/utils'

describe('truncate number strings', () => {
it('makes no changes correctly', () => {
const positiveDecimal = '45.222'
const negativeDecimal = '-55.888'
const positiveInteger = '12'
const negativeInteger = '-24'
const positiveDecimal = 45.222
const negativeDecimal = -55.888
const positiveInteger = 12
const negativeInteger = -24

expect(truncate(positiveDecimal, 3)).to.equal(positiveDecimal)
expect(truncate(negativeDecimal, 3)).to.equal(negativeDecimal)
expect(truncate(positiveInteger, 3)).to.equal(positiveInteger)
expect(truncate(negativeInteger, 3)).to.equal(negativeInteger)
})
it('truncates correctly', () => {
const positiveDecimal = '45.12345'
const negativeDecimal = '-55.56789'

expect(truncate(positiveDecimal, 3)).to.equal('45.123')
expect(truncate(negativeDecimal, 3)).to.equal('-55.567')
})
it('adds zeroes correctly', () => {
expect(truncate('45.0', 4)).to.equal('45.0000')
expect(truncate('45.00', 4)).to.equal('45.0000')
expect(truncate('45.3433', 4)).to.equal('45.3433')
expect(truncate('45.343', 4)).to.equal('45.3430')
expect(truncate(45.12345, 3)).to.equal(45.123)
expect(truncate(-55.56789, 3)).to.equal(-55.567)
})
})

0 comments on commit c337811

Please sign in to comment.