Skip to content

Instantly share code, notes, and snippets.

@manveru
Created July 19, 2010 16:03
Show Gist options
  • Save manveru/481592 to your computer and use it in GitHub Desktop.
Save manveru/481592 to your computer and use it in GitHub Desktop.
url: require 'url'
querystring: require 'querystring'
request: require 'request'
class Rpx
constructor: (apiKey) ->
@apiKey: apiKey
authInfo: (token, callback) ->
@apiCall 'auth_info', {token: token}, (data) ->
callback data['profile']
mappings: (primaryKey, callback) ->
@apiCall 'mappings', {primaryKey: primaryKey}, (data) ->
callback data['identifiers']
map: (identifier, key, callback) ->
@apiCall 'map', {primaryKey: key, identifier: identifier}, callback
unmap: (identifier, key, callback) ->
@apiCall 'unmap', {primaryKey: key, identifier: identifier}, callback
apiCall: (methodName, originalQuery, callback) ->
uri: url.parse("https://rpxnow.com/api/v2/${methodName}")
payload: querystring.stringify(
Object.merge({format: 'json', apiKey: @apiKey}, originalQuery)
)
request {method: 'POST', uri: uri, body: payload}, (error, response, body) ->
throw error if error
throw "Wrong status code: ${response.statusCode}" if response.statusCode isnt 200
json: JSON.parse(body)
unless json['stat'] is 'ok'
throw "Response wasn't ok, got ${json['stat']} instead"
callback json
module.exports: Rpx
(function(){
var Rpx, querystring, request, url;
url = require('url');
querystring = require('querystring');
request = require('request');
Rpx = function(apiKey) {
this.apiKey = apiKey;
return this;
};
Rpx.prototype.authInfo = function(token, callback) {
return this.apiCall('auth_info', {
token: token
}, function(data) {
return callback(data['profile']);
});
};
Rpx.prototype.mappings = function(primaryKey, callback) {
return this.apiCall('mappings', {
primaryKey: primaryKey
}, function(data) {
return callback(data['identifiers']);
});
};
Rpx.prototype.map = function(identifier, key, callback) {
return this.apiCall('map', {
primaryKey: key,
identifier: identifier
}, callback);
};
Rpx.prototype.unmap = function(identifier, key, callback) {
return this.apiCall('unmap', {
primaryKey: key,
identifier: identifier
}, callback);
};
Rpx.prototype.apiCall = function(methodName, originalQuery, callback) {
var payload, uri;
uri = url.parse(("https://rpxnow.com/api/v2/" + (methodName)));
payload = querystring.stringify(Object.merge({
format: 'json',
apiKey: this.apiKey
}, originalQuery));
return request({
method: 'POST',
uri: uri,
body: payload
}, function(error, response, body) {
var json;
if (error) {
throw error;
}
if (response.statusCode !== 200) {
throw ("Wrong status code: " + (response.statusCode));
}
json = JSON.parse(body);
if (!(json['stat'] === 'ok')) {
throw ("Response wasn't ok, got " + (json['stat']) + " instead");
}
return callback(json);
});
};
module.exports = Rpx;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment