Created
February 8, 2011 03:45
-
-
Save atmos/815814 to your computer and use it in GitHub Desktop.
Revisions
-
atmos revised this gist
Feb 24, 2011 . 1 changed file with 18 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ class Campfire extends EventEmitter @rooms = options.rooms.split(",") @account = options.account @domain = @account + ".campfirenow.com" @authorization = "Basic " + new Buffer("#{@token}:x").toString("base64") Rooms: (callback) -> @get "/rooms", callback @@ -40,7 +40,7 @@ class Campfire extends EventEmitter speak: (text, callback) -> @message text, "TextMessage", callback message: (text, type, callback) -> body = { message: { "body":text, "type":type } } self.post "/room/#{id}/speak", body, callback # listen for activity in channels @@ -57,23 +57,28 @@ class Campfire extends EventEmitter "headers": headers request = HTTPS.request options, (response) -> response.setEncoding("utf8") response.on "data", (chunk) -> #console.log "#{new Date}: Received #{id} \"#{chunk}\"" if chunk.match(/^\S+/) try chunk.split("\r").forEach (part) -> data = JSON.parse part self.emit data.type, data.id, data.created_at, data.room_id, data.user_id, data.body data response.on "end", -> console.log "Streaming Connection closed. :(" response.on "error", (err) -> console.log err request.end() request.on "error", (err) -> console.log err console.log err.stack # Convenience HTTP Methods for posting on behalf of the token"d user get: (path, callback) -> @request "GET", path, null, callback @@ -107,7 +112,7 @@ class Campfire extends EventEmitter callback null, JSON.parse(data) catch err callback null, data || { } response.on "error", (err) -> callback err, { } if method == "POST" -
atmos revised this gist
Feb 21, 2011 . 1 changed file with 21 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ HTTPS = require "https" EventEmitter = require("events").EventEmitter class Campfire extends EventEmitter constructor: (options) -> @token = options.token @rooms = options.rooms.split(",") @@ -43,7 +44,7 @@ class Campfire self.post "/room/#{id}/speak", body, callback # listen for activity in channels listen: -> headers = "Host" : "streaming.campfirenow.com", "Authorization" : self.authorization @@ -55,12 +56,15 @@ class Campfire "method" : "GET" "headers": headers request = HTTPS.request options, (response) -> response.on "data", (chunk) -> if chunk && chunk.toString().match(/[^\s]+/) try data = JSON.parse chunk catch err data = chunk self.emit data.type, data.id, data.created_at, data.room_id, data.user_id, data.body data response.on 'error', (err) -> callback err, { } @@ -92,19 +96,24 @@ class Campfire if method == "POST" if typeof(body) != "string" body = JSON.stringify body options.headers["Content-Length"] = body.length request = HTTPS.request options, (response) -> data = "" response.on "data", (chunk) -> data += chunk response.on "end", -> try callback null, JSON.parse(data) catch err callback null, data || { } response.on 'error', (err) -> callback err, { } if method == "POST" request.end(body) else request.end() request.on "error", (err) -> console.log err console.log err.stack -
atmos revised this gist
Feb 17, 2011 . 1 changed file with 34 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,9 @@ HTTP = require "https" class Campfire constructor: (options) -> @token = options.token @rooms = options.rooms.split(",") @account = options.account @domain = @account + ".campfirenow.com" @authorization = "Basic " + new Buffer("#{@token}:x").toString('base64') @@ -43,21 +44,30 @@ class Campfire # listen for activity in channels listen: (callback) -> headers = "Host" : "streaming.campfirenow.com", "Authorization" : self.authorization options = "host" : "streaming.campfirenow.com" "port" : 443 "path" : "/room/#{id}/live.json" "method" : "GET" "headers": headers request = HTTP.request options, (response) -> data = "" response.on "data", (chunk) -> require("util").debug "{ #{chunk.toString()} }" if chunk && chunk.toString().match(/[^\s]+/) callback null, JSON.parse(chunk) response.on 'error', (err) -> callback err, { } request.end() request.on "error", (err) -> console.log err console.log err.stack # Convenience HTTP Methods for posting on behalf of the token'd user get: (path, callback) -> @@ -72,25 +82,31 @@ class Campfire "Host" : @domain "Content-Type" : "application/json" options = "host" : @domain "port" : 443 "path" : path "method" : method "headers": headers if method == "POST" if typeof(body) != "string" body = JSON.stringify body options.body = body options.headers["Content-Length"] = body.length request = HTTP.request options, (response) -> data = "{}" response.on "data", (chunk) -> data += chunk response.on "end", -> callback null, JSON.parse data response.on 'error', (err) -> callback err, { } request.end() request.on "error", (err) -> console.log err console.log err.stack exports.Campfire = Campfire -
atmos created this gist
Feb 8, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,96 @@ HTTP = require "http" class Campfire constructor: (options) -> @token = options.token @account = options.account @domain = @account + ".campfirenow.com" @authorization = "Basic " + new Buffer("#{@token}:x").toString('base64') Rooms: (callback) -> @get "/rooms", callback User: (id, callback) -> @get "/users/#{id}", callback Me: (callback) -> @get "/users/me", callback Room: (id) -> self = @ show: (callback) -> self.post "/room/#{id}", "", callback join: (callback) -> self.post "/room/#{id}/join", "", callback leave: (callback) -> self.post "/room/#{id}/leave", "", callback lock: (callback) -> self.post "/room/#{id}/lock", "", callback unlock: (callback) -> self.post "/room/#{id}/unlock", "", callback # say things to this channel on behalf of the token user paste: (text, callback) -> @message text, "PasteMessage", callback sound: (text, callback) -> @message text, "SoundMessage", callback speak: (text, callback) -> @message text, "TextMessage", callback message: (text, type, callback) -> body = { message: { 'body':text, 'type':type } } self.post "/room/#{id}/speak", body, callback # listen for activity in channels listen: (callback) -> path = "/room/#{id}/live.json" headers = "Host" : "streaming.campfirenow.com", "Authorization" : self.authorization client = HTTP.createClient 443, "streaming.campfirenow.com", true request = client.request "GET", path, headers request.on "response", (response) -> response.setEncoding("utf8") response.on "data", (chunk) -> if chunk != " " for data in chunk.split("\r") when data != "" do (data) -> callback null, JSON.parse(data) request.end() # Convenience HTTP Methods for posting on behalf of the token'd user get: (path, callback) -> @request "GET", path, null, callback post: (path, body, callback) -> @request "POST", path, body, callback request: (method, path, body, callback) -> headers = "Authorization" : @authorization "Host" : @domain "Content-Type" : "application/json" if method == "POST" if typeof(body) != "string" body = JSON.stringify body headers["Content-Length"] = body.length client = HTTP.createClient 443, @domain, true request = client.request method, path, headers request.on "response", (response) -> data = "" response.on "data", (chunk) -> data += chunk response.on "end", -> callback null, JSON.parse data response.on 'error', (err) -> callback err, { } if method == "POST" request.write body request.end() exports.Campfire = Campfire