Browse Source

standard code style

standard
Luke Childs 8 years ago
parent
commit
03f92a85af
  1. 48
      src/index.js

48
src/index.js

@ -1,13 +1,12 @@
const got = require('got'); const got = require('got')
const cacheManager = require('cache-manager'); const cacheManager = require('cache-manager')
const querystring = require('querystring'); const querystring = require('querystring')
const pkg = require('../package.json'); const pkg = require('../package.json')
class Onionoo { class Onionoo {
// Constructor returns a new object so instance properties are private // Constructor returns a new object so instance properties are private
constructor (options = {}) { constructor (options = {}) {
// Set default options // Set default options
this.options = Object.assign({}, { this.options = Object.assign({}, {
baseUrl: 'https://onionoo.torproject.org', baseUrl: 'https://onionoo.torproject.org',
@ -19,46 +18,44 @@ class Onionoo {
'clients', 'clients',
'uptime' 'uptime'
] ]
}, options); }, options)
if (options.cache !== false) { if (options.cache !== false) {
this.options.cache = cacheManager.caching(Object.assign({}, { this.options.cache = cacheManager.caching(Object.assign({}, {
store: 'memory', store: 'memory',
max: 500 max: 500
}, options.cache)); }, options.cache))
} }
// Return object containing endpoint methods // Return object containing endpoint methods
return this.options.endpoints.reduce((onionoo, endpoint) => { return this.options.endpoints.reduce((onionoo, endpoint) => {
onionoo[endpoint] = this.createEndpointMethod(endpoint); onionoo[endpoint] = this.createEndpointMethod(endpoint)
return onionoo; return onionoo
}, {}); }, {})
} }
// Returns cache max age from response headers // Returns cache max age from response headers
checkResponseMaxAge (response) { checkResponseMaxAge (response) {
const cacheControl = response.headers['cache-control']; const cacheControl = response.headers['cache-control']
const maxAgeRegex = /max-age=(\d+)/; const maxAgeRegex = /max-age=(\d+)/
const maxAge = cacheControl && cacheControl.match(maxAgeRegex); const maxAge = cacheControl && cacheControl.match(maxAgeRegex)
return maxAge && maxAge[1]; return maxAge && maxAge[1]
} }
// Returns a function to make requests to a given endpoint // Returns a function to make requests to a given endpoint
createEndpointMethod (endpoint) { createEndpointMethod (endpoint) {
return args => new Promise((resolve, reject) => { return args => new Promise((resolve, reject) => {
// Build query string (don't encode ':' for search filters) // Build query string (don't encode ':' for search filters)
const qs = querystring.encode(args).replace(/%3A/g, ':'); const qs = querystring.encode(args).replace(/%3A/g, ':')
// Build url // Build url
const url = `${this.options.baseUrl}/${endpoint}?${qs}`; const url = `${this.options.baseUrl}/${endpoint}?${qs}`
// Check for url in cache // Check for url in cache
if (this.options.cache) { if (this.options.cache) {
this.options.cache.get(url) this.options.cache.get(url).then(cachedResult => resolve(cachedResult || this.makeRequest(url)))
.then(cachedResult => resolve(cachedResult ? cachedResult : this.makeRequest(url)));
} else { } else {
resolve(this.makeRequest(url)); resolve(this.makeRequest(url))
} }
}) })
} }
@ -68,22 +65,21 @@ class Onionoo {
const options = { const options = {
json: true, json: true,
'user-agent': `onionoo-node-client v${pkg.version} (${pkg.homepage})` 'user-agent': `onionoo-node-client v${pkg.version} (${pkg.homepage})`
}; }
return got(url, options) return got(url, options)
.then(response => { .then(response => {
// Cache response // Cache response
if (this.options.cache) { if (this.options.cache) {
const ttl = this.checkResponseMaxAge(response); const ttl = this.checkResponseMaxAge(response)
if (ttl) { if (ttl) {
this.options.cache.set(url, response.body, { ttl }); this.options.cache.set(url, response.body, { ttl })
} }
} }
// Resolve response // Resolve response
return response.body; return response.body
}) })
} }
} }
module.exports = Onionoo; module.exports = Onionoo

Loading…
Cancel
Save