Comparing version 1.0.41 to 1.0.43
199
gopher.js
var clientRequest = require('client-request'); | ||
function isType(obj, type) { | ||
return Object.prototype.toString.call(obj) === '[object ' + type + ']'; | ||
}; | ||
var sprintf = require('./sprintf.js'); | ||
var extend = require('./extend.js'); | ||
var isArray = require('./is.js').isArray; | ||
var isString = require('./is.js').isString; | ||
var isObject = require('./is.js').isObject; | ||
function isArray(obj) { | ||
return isType(obj, 'Array'); | ||
}; | ||
var Gopher = module.exports = function(baseURL, defaultOptions) { | ||
function isString(obj) { | ||
return isType(obj, 'String'); | ||
}; | ||
function isObject(obj) { | ||
return obj !== null && isType(obj, 'Object'); | ||
}; | ||
module.exports = function(baseURL, opts) { | ||
var _debug = opts && opts.debug; | ||
this.get = function(path, params, headers) { | ||
return this.request('GET', path, params, headers); | ||
this.get = function(path, options) { | ||
return this.request('GET', path, options); | ||
} | ||
this.put = function(path, params, headers) { | ||
return this.request('PUT', path, params, headers); | ||
this.put = function(path, options) { | ||
return this.request('PUT', path, options); | ||
} | ||
this.post = function(path, params, headers) { | ||
return this.request('POST', path, params, headers); | ||
this.post = function(path, options) { | ||
return this.request('POST', path, options); | ||
} | ||
this.delete = function(path, params, headers) { | ||
return this.request('DELETE', path, params, headers); | ||
this.delete = function(path, options) { | ||
return this.request('DELETE', path, options); | ||
} | ||
this.request = function(method, path, params, headers) { | ||
this.request = function(method, path, options) { | ||
function buildPath(path, params) { | ||
method = method.toUpperCase(); | ||
options = extend({}, defaultOptions, options || {}); | ||
var parts = []; | ||
function buildPath() { | ||
path.split('/').forEach(function(part) { | ||
var match = part.match('^:([_$@A-Za-z0-9]+)$'); | ||
if (path == undefined) | ||
return ''; | ||
if (!match) | ||
match = part.match('^{([_$@A-Za-z0-9]+)}$'); | ||
return '/' + path; | ||
}; | ||
if (match) { | ||
var name = match[1]; | ||
function buildQuery() { | ||
if (params[name] != undefined) { | ||
parts.push(params[name]); | ||
delete params[name] | ||
} | ||
else | ||
parts.push(part); | ||
} | ||
else | ||
parts.push(part); | ||
var query = options.query; | ||
}); | ||
if (query == undefined) | ||
return ''; | ||
return parts.join('/'); | ||
}; | ||
function buildParams(params) { | ||
if (params == undefined) | ||
params = {}; | ||
function uriEncode(value) { | ||
@@ -86,79 +59,37 @@ | ||
var array = Object.keys(params).map(function(key) { | ||
return encodeURIComponent(key) + '=' + uriEncode(params[key]); | ||
var array = Object.keys(query).map(function(key) { | ||
return encodeURIComponent(key) + '=' + uriEncode(query[key]); | ||
}); | ||
return array.join('&'); | ||
return '?' + array.join('&'); | ||
} | ||
function buildHeaders(headers) { | ||
var result = {}; | ||
// Add default headers | ||
if (opts && opts.headers) { | ||
Object.keys(opts.headers).forEach(function(key) { | ||
result[key.toLowerCase()] = opts.headers[key]; | ||
}); | ||
} | ||
if (isObject(headers)) { | ||
Object.keys(headers).forEach(function(key) { | ||
result[key.toLowerCase()] = headers[key]; | ||
}); | ||
} | ||
if (result['content-type'] == undefined) | ||
result['content-type'] = 'application/json'; | ||
return result; | ||
function buildHeaders() { | ||
var headers = {}; | ||
return headers; | ||
}; | ||
function buildBody(method, params, headers) { | ||
if (method == 'post' || method == 'put') { | ||
if (headers['content-type'] == 'application/json') { | ||
return JSON.stringify(params); | ||
} | ||
if (headers['content-type'] == 'application/x-www-form-urlencoded') { | ||
return buildParams(params); | ||
} | ||
} | ||
} | ||
function buildQuery(method, params) { | ||
if (method == 'get' || method == 'delete') { | ||
return buildParams(params); | ||
} | ||
function buildBody() { | ||
return ''; | ||
}; | ||
} | ||
function buildURI() { | ||
return baseURL + buildPath() + buildQuery(); | ||
}; | ||
function buildURI(method, path, params) { | ||
var path = buildPath(path, params); | ||
var query = buildQuery(method, params); | ||
return new Promise(function(resolve, reject) { | ||
return baseURL + '/' + (query == '' ? path : path + '?' + query); | ||
} | ||
var requestOptions = {}; | ||
requestOptions.method = method; | ||
requestOptions.uri = buildURI(); | ||
requestOptions.headers = buildHeaders(); | ||
requestOptions.body = buildBody(); | ||
var options = {}; | ||
options.method = method.toLowerCase(); | ||
options.uri = buildURI(options.method, path, params); | ||
options.headers = buildHeaders(headers); | ||
options.body = buildBody(options.method, params, options.headers); | ||
return new Promise(function(resolve, reject) { | ||
if (_debug) { | ||
console.log('method', options.method); | ||
console.log('uri', options.uri); | ||
console.log('headers', options.headers); | ||
console.log('body', options.body); | ||
if (options.debug) { | ||
console.log('Gopher request:', JSON.stringify(requestOptions, null, ' ')); | ||
} | ||
clientRequest(options, function (error, response, body) { | ||
clientRequest(requestOptions, function (error, response, body) { | ||
if (!error && response.statusCode == 200) { | ||
var contentType = ''; | ||
@@ -173,3 +104,3 @@ | ||
try { | ||
// Pars JSON first | ||
// Parse JSON first | ||
var json = JSON.parse(body); | ||
@@ -190,2 +121,3 @@ | ||
else { | ||
if (error == null) | ||
@@ -210,1 +142,36 @@ error = body.toString(); | ||
}; | ||
function test() { | ||
var yahoo = new Gopher('https://query.yahooapis.com', {debug:false}); | ||
function getQuote(ticker) { | ||
var query = {}; | ||
query.q = 'select * from yahoo.finance.quotes where symbol = "' + ticker + '"'; | ||
query.format = 'json'; | ||
query.env = 'store://datatables.org/alltableswithkeys'; | ||
query.callback = ''; | ||
yahoo.get('v1/public/yql', {query:query}).then(function(data) { | ||
var quotes = data.query.results.quote; | ||
if (typeof qoutes != 'Array') | ||
quotes = [quotes]; | ||
console.log(ticker, '=', quotes[0].LastTradePriceOnly); | ||
}) | ||
.catch (function(error) { | ||
console.log(error); | ||
}); | ||
} | ||
getQuote('AAPL'); | ||
}; | ||
test(); |
{ | ||
"name": "yow", | ||
"version": "1.0.41", | ||
"version": "1.0.43", | ||
"description": "You Only Wish module", | ||
@@ -5,0 +5,0 @@ "main": "yow.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16428
401