Skip to content

Commit

Permalink
#reqestToken
Browse files Browse the repository at this point in the history
  • Loading branch information
macalinao committed Jun 15, 2012
1 parent e6736e6 commit dcc6bae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
24 changes: 23 additions & 1 deletion lib/index.coffee
Expand Up @@ -14,6 +14,7 @@ module.exports = class QuizletAPI
Creates a new instance of a Quizlet API accessor.
###
@clientId = if params.clientId? then params.clientId else throw new Error 'Need client ID!'
@secret = if params.secret? then params.secret else throw new Error 'Need secret!'

getAuthUrl: (scopes = ['read'], state = 'state', redirectURI) ->
###
Expand All @@ -35,6 +36,26 @@ module.exports = class QuizletAPI

return authBaseUrl + '?' + querystring.stringify(params)

requestToken: (code, redirectUri) ->
###
Requests a token from Quizlet.
###
basic = new Buffer(@clientId + ':' + @secret).toString 'base64'

params =
grant_type: 'authorization_code'
code: code

request.post(tokenUrl)
.type('form')
.set('Authorization', 'Basic ' + basic)
.send()
.end (res) ->
if res.body.error
cb true, res.body
else
cb null, res.body

get: (resource, params, cb) ->
###
Performs a general GET request against the Quizlet API.
Expand All @@ -43,7 +64,8 @@ module.exports = class QuizletAPI
request.get(baseUrl + resource + '?' + querystring.stringify(params)).end (res) ->
if res.body.error
cb true, res.body
cb null, res.body
else
cb null, res.body

getUser: (username, cb) ->
###
Expand Down
5 changes: 4 additions & 1 deletion test/tests.coffee
Expand Up @@ -5,7 +5,7 @@ url = require 'url'
describe 'QuizletAPI', ->
beforeEach ->
# All API calls are assumed to be authenticated.
@api = new Quizlet clientId: config.clientId
@api = new Quizlet clientId: config.clientId, secret: config.secret

describe '#getAuthUrl', ->
it 'should create a new URL based on the client ID', ->
Expand All @@ -25,6 +25,9 @@ describe 'QuizletAPI', ->
parse = url.parse @api.getAuthUrl(['READ', 'Write_Set']), true
parse.query.scope.should.equal 'read%20write_set'

describe '#requestToken', ->
it 'is untestable', ->

describe '#getUser', ->
it 'should get the details of a user', (done) ->
@api.getUser 'simplyianm', (err, user) ->
Expand Down

0 comments on commit dcc6bae

Please sign in to comment.