Skip to content

Instantly share code, notes, and snippets.

@tyler-smith
Created October 26, 2012 17:09
Show Gist options
  • Save tyler-smith/3959993 to your computer and use it in GitHub Desktop.
Save tyler-smith/3959993 to your computer and use it in GitHub Desktop.
OAuth = require('oauth').OAuth;
class UVClient
constructor: (@subdomain, @api_key, @api_secret, @callback=null, @token='', @secret='') ->
@api_url = "https://#{@subdomain}.uservoice.com"
@consumer = new OAuth null, null, @api_key, @api_secret, "1.0", null, "HMAC-SHA1"
request: (method, path, params={}, callback=null) ->
# Allow users to not specify empty params
if typeof params is 'function'
[callback, params] = [params, {}]
body = JSON.stringify(params)
method = method.toLowerCase()
url = "#{@api_url}/api/v1/#{path}"
@consumer[method] url, null, null, (err, data, result) ->
if err
throw new Error(err)
callback(JSON.parse(data))
get: (path, params) -> @request('get', path, params)
put: (path, params) -> @request('put', path, params)
post: (path, params) -> @request('post', path, params)
delete: (path, params) -> @request('delete', path, params)
(function() {
var OAuth, UVClient, uservoice;
OAuth = require('oauth').OAuth;
UVClient = (function() {
function UVClient(subdomain, api_key, api_secret, callback, token, secret) {
this.subdomain = subdomain;
this.api_key = api_key;
this.api_secret = api_secret;
this.callback = callback != null ? callback : null;
this.token = token != null ? token : '';
this.secret = secret != null ? secret : '';
this.api_url = "https://" + this.subdomain + ".uservoice.com";
this.consumer = new OAuth(null, null, this.api_key, this.api_secret, "1.0", null, "HMAC-SHA1");
}
UVClient.prototype.request = function(method, path, params, callback) {
var body, url, _ref;
if (params == null) {
params = {};
}
if (callback == null) {
callback = null;
}
if (typeof params === 'function') {
_ref = [params, {}], callback = _ref[0], params = _ref[1];
}
body = JSON.stringify(params);
method = method.toLowerCase();
url = "" + this.api_url + "/api/v1/" + path;
return this.consumer[method](url, null, null, function(err, data, result) {
if (err) {
throw new Error(err);
}
return callback(JSON.parse(data));
});
};
UVClient.prototype.get = function(path, params) {
return this.request('get', path, params);
};
UVClient.prototype.put = function(path, params) {
return this.request('put', path, params);
};
UVClient.prototype.post = function(path, params) {
return this.request('post', path, params);
};
UVClient.prototype["delete"] = function(path, params) {
return this.request('delete', path, params);
};
return UVClient;
})();
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment