Skip to content

Instantly share code, notes, and snippets.

@manveru
Created July 5, 2010 15:24
Show Gist options
  • Save manveru/464460 to your computer and use it in GitHub Desktop.
Save manveru/464460 to your computer and use it in GitHub Desktop.
Interface with janrain
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment