Permalink
Browse files

Add ability to authenticate with OAuth2 token, and without any auth m…

…ethods
  • Loading branch information...
1 parent 32ffb58 commit 3a2b11c7854eb37e92e188785a6baa03765b5072 @meritt committed Aug 16, 2011
Showing with 10 additions and 8 deletions.
  1. +10 −8 src/gisty.coffee
View
@@ -10,11 +10,11 @@ xhr = require 'request'
class Gisty
api = 'https://api.github.com'
- constructor: (@login, @password) ->
- @username = @login
+ constructor: (options = {}) ->
+ @setOptions options
- setUsername: (username) ->
- @username = username
+ setOptions: (@options) ->
+ @options.username = @options.login if @options.login? and not @options.username?
@
fetch: (id, fn) ->
@@ -38,10 +38,12 @@ class Gisty
request @, fn
request = (self, fn = ->) ->
- options =
- url: api + self.scheme,
- headers:
- Authorization: 'Basic ' + new Buffer(self.login + ':' + self.password).toString('base64')
+ options = url: api + self.scheme
+
+ if self.options.token?
+ options['headers'] = Authorization: "token #{self.options.token}"
+ else if self.options.login? and self.options.password?
+ options['headers'] = Authorization: "Basic " + new Buffer(self.options.login + ':' + self.options.password).toString('base64')
xhr options, (error, request, body) ->
body = JSON.parse body

0 comments on commit 3a2b11c

Please sign in to comment.