├── untitled.md ├── SUMMARY.md ├── .travis.yml ├── .gitignore ├── bower.json ├── package.json ├── dweet.io.min.js ├── README.md ├── dweet.io.min.map ├── test └── test.js └── dweet.io.js /untitled.md: -------------------------------------------------------------------------------- 1 | # Untitled 2 | 3 | -------------------------------------------------------------------------------- /SUMMARY.md: -------------------------------------------------------------------------------- 1 | # Table of contents 2 | 3 | * [dweet.io Javascript Client](README.md) 4 | * [Untitled](untitled.md) 5 | 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | before_install: 5 | - 'npm install npm -g' 6 | notifications: 7 | email: false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | lcov.info 3 | *.seed 4 | *.log 5 | *.csv 6 | *.dat 7 | *.out 8 | *.pid 9 | *.gz 10 | 11 | pids 12 | logs 13 | results 14 | build 15 | .grunt 16 | .idea 17 | 18 | node_modules 19 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dweet.io", 3 | "version": "0.0.11", 4 | "main": "dweet.io.js", 5 | "ignore": [ 6 | ".jshintrc", 7 | "**/*.txt", 8 | "README.md", 9 | "test" 10 | ] 11 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dweet.io.js", 3 | "description": "JavaScript client library for dweet.io.", 4 | "homepage": "http://dweet.io", 5 | "version": "0.0.13", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/buglabs/dweetio-client.git" 9 | }, 10 | "devDependencies": { 11 | "uglify-js": "2.4.x" 12 | }, 13 | "scripts": { 14 | "build": "uglifyjs dweet.io.js -c -m --source-map dweet.io.min.map -o dweet.io.min.js -r \"io\"" 15 | }, 16 | "files": [ 17 | "dweet.io.js", 18 | "dweet.io.min.js" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /dweet.io.min.js: -------------------------------------------------------------------------------- 1 | !function(){function t(t){return"[object Array]"===Object.prototype.toString.call(t)}function e(t){return"function"==typeof t}var n=!0;try{n=require}catch(o){n=!1}var io,r,i,c="last-thing.dat",u="https://dweet.io:443",a=!0,l=5e3;if(n){if(io=require("socket.io-client"),r=require("request"),require("fs").existsSync(c))try{i=require("fs").readFileSync(c).toString()}catch(o){}}else r=function(t,e){var n=t.url+(t.url.indexOf("?")+1?"&":"?"),o=[],r="";for(r in t.json)o.push(r+"="+encodeURIComponent(t.json[r]));for(var i="callback",c=0;window.dweetCallback[i+c];)c++;i+=c,window.dweetCallback[i]=function(t){e(null,t,t)},o.push("callback=dweetCallback."+i),o.push("_="+Date.now()),n+=o.join("&"),dweet_script_loader(n,function(t){t.parentNode.removeChild(t),window.dweetCallback[i]=void 0,delete window.dweetCallback[i]})},window.dweetCallback={},function(){var t=/ded|co/,e="onload",n="onreadystatechange",o="readyState",r=function(r,i){var c=document.createElement("script");c[e]=c[n]=function(){(!this[o]||t.test(this[o]))&&(c[e]=c[n]=null,i&&i(c),c=null)},c.async=!0,c.src=r,document.body.appendChild(c)};window.dweet_script_loader=function(t,e){if("string"==typeof t)return void r(t,e);var n=t.shift();r(n,function(n){t.length?window.dweet_script_loader(t,e):e&&e(n)})}}();var f=function(){function o(t){return t.created&&(t.created=new Date(t.created)),t}function f(t){if(t instanceof Array)for(var e=0;e 9 | ``` 10 | 11 | Or from our CDN 12 | 13 | ```html 14 | 15 | ``` 16 | 17 | ### Dweeting 18 | 19 | Send a dweet and let dweet.io make up a name for you. Subsequent calls to this will result in the same name being used. 20 | ```js 21 | dweetio.dweet({some:"data"}, function(err, dweet){ 22 | 23 | console.log(dweet.thing); // The generated name 24 | console.log(dweet.content); // The content of the dweet 25 | console.log(dweet.created); // The create date of the dweet 26 | 27 | }); 28 | ``` 29 | 30 | Send a dweet with a name you define. 31 | ```js 32 | dweetio.dweet_for("my-thing", {some:"data"}, function(err, dweet){ 33 | 34 | console.log(dweet.thing); // "my-thing" 35 | console.log(dweet.content); // The content of the dweet 36 | console.log(dweet.created); // The create date of the dweet 37 | 38 | }); 39 | ``` 40 | 41 | ### Getting Dweets 42 | 43 | Get the latest dweet. 44 | ```js 45 | dweetio.get_latest_dweet_for("my-thing", function(err, dweet){ 46 | 47 | var dweet = dweet[0]; // Dweet is always an array of 1 48 | 49 | console.log(dweet.thing); // The generated name 50 | console.log(dweet.content); // The content of the dweet 51 | console.log(dweet.created); // The create date of the dweet 52 | 53 | }); 54 | ``` 55 | 56 | Get all dweets (up to 500 in the last 24 hours). 57 | ```js 58 | dweetio.get_all_dweets_for("my-thing", function(err, dweets){ 59 | 60 | // Dweets is an array of dweets 61 | for(theDweet in dweets) 62 | { 63 | var dweet = dweets[theDweet]; 64 | 65 | console.log(dweet.thing); // The generated name 66 | console.log(dweet.content); // The content of the dweet 67 | console.log(dweet.created); // The create date of the dweet 68 | } 69 | 70 | }); 71 | ``` 72 | 73 | ### Alerts 74 | 75 | Set an alert. 76 | ```js 77 | // Email addresses can also be an array 78 | dweetio.set_alert("my-thing", "email1@doh-main.com,email2@doh-main.com", "if(dweet.some_data > 100) return 'something wrong';", "my-key", function(err){ 79 | 80 | // If there was a problem, err will be returned, otherwise setting the alert was successful. 81 | 82 | }); 83 | ``` 84 | 85 | Get an alert 86 | ```js 87 | dweetio.get_alert("my-thing", "my-key", function(err, alertData){ 88 | 89 | // If there was a problem, err will be returned, otherwise the data for the alert will be returned in alertData 90 | 91 | }); 92 | ``` 93 | 94 | Remove an alert 95 | ```js 96 | // Email addresses can also be an array 97 | dweetio.remove_alert("my-thing", "my-key", function(err){ 98 | 99 | // If there was a problem, err will be returned, otherwise the alert will have been successfully removed. 100 | 101 | }); 102 | ``` 103 | 104 | ### Notifications 105 | 106 | Listen for all dweets from a thing. 107 | ```js 108 | dweetio.listen_for("my-thing", function(dweet){ 109 | 110 | // This will be called anytime there is a new dweet for my-thing 111 | 112 | }); 113 | ``` 114 | 115 | Stop listening for dweets from a thing. 116 | ```js 117 | dweetio.stop_listening_for("my-thing"); 118 | ``` 119 | 120 | Stop listening for dweets from everything. 121 | ```js 122 | dweetio.stop_listening(); 123 | ``` 124 | 125 | ### Locking & Security 126 | 127 | By default, all things are publicly accessible if you know the name of the thing. You can also lock things so that they are only accessible to users with valid security credentials. To purchase locks, visit https://dweet.io/locks. The locks will be emailed to you. 128 | 129 | To use purchased locks: 130 | 131 | ```js 132 | // To lock a thing 133 | dweetio.lock("my-thing", "my-lock", "my-key", function(err){ 134 | 135 | // If there was a problem, err will be returned, otherwise the lock was successful. 136 | 137 | }); 138 | 139 | // To unlock a thing 140 | dweetio.unlock("my-thing", "my-key", function(err){ 141 | 142 | // If there was a problem, err will be returned, otherwise the lock was successful. 143 | 144 | }); 145 | 146 | // To remove a lock no matter what it's attached to 147 | dweetio.remove_lock("my-lock", "my-key", function(err){ 148 | 149 | // If there was a problem, err will be returned, otherwise the lock was successful. 150 | 151 | }); 152 | ``` 153 | 154 | Once a thing has been locked, you must pass the key to the lock with any call you make to other functions in this client library. The key will be passed as a parameter before the callback function. For example: 155 | 156 | ```js 157 | dweetio.dweet_for("my-locked-thing", {some:"data"}, "my-key", callback); 158 | 159 | dweetio.get_latest_dweet_for("my-locked-thing", "my-key", callback); 160 | 161 | dweetio.get_all_dweets_for("my-locked-thing", "my-key", callback); 162 | 163 | dweetio.listen_for("my-locked-thing", "my-key", callback); 164 | ``` 165 | 166 | Failure to pass a key or passing an incorrect key for a locked thing will result in an error being returned in the callback. 167 | 168 | ### Copyright & License 169 | 170 | Copyright © 2013 Jim Heising (https://github.com/jheising) 171 |
172 | Copyright © 2013 Bug Labs, Inc. (http://buglabs.net) 173 |
174 | Licensed under the **MIT** license. 175 | 176 | -------------------------------------------------------------------------------- /dweet.io.min.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"dweet.io.min.js","sources":["dweet.io.js"],"names":["isArray","obj","Object","prototype","toString","call","isFunction","isNode","e","io","request","lastThing","LAST_THING_NAME","DWEET_SERVER","STRICT_SSL","REQUEST_TIMEOUT","require","existsSync","readFileSync","options","callback","src","url","indexOf","params","param_name","json","push","encodeURIComponent","callbackName","index","window","dweetCallback","data","Date","now","join","dweet_script_loader","script","parentNode","removeChild","undefined","re","onload","onreadystatechange","readyState","load","fn","document","createElement","this","test","async","body","appendChild","srces","shift","length","dweetioClient","normalizeDweet","dweet","created","normalizeDweets","dweets","Array","parseBody","responseData","String","JSON","parse","processResponse","err","Error","createKeyedURL","key","processDweetResponse","socket","self","listenCallbacks","currentThing","set_server","server","strictSSL","globalAgent","rejectUnauthorized","dweet_for","jar","method","followAllRedirects","timeout","response","thing","writeFile","get_latest_dweet_for","get_all_dweets_for","get_key_for","account","thingname","create_key_for","get_keys_for_account","startPosition","endPosition","listen_for","createSocket","connect","on","id","emit","msg","callbacks","stop_listening","disconnect","stop_listening_for","lock","unlock","remove_lock","set_alert","recipients","condition","get_alert","remove_alert","module","exports","dweetio"],"mappings":"CAIA,WA4GC,QAASA,GAAQC,GAChB,MAAiD,mBAA1CC,OAAOC,UAAUC,SAASC,KAAMJ,GAGxC,QAASK,GAAWL,GACnB,MAAsB,kBAARA,GA/Gf,GAAIM,IAAS,CAGb,KACCA,EAAS,QAEV,MAAOC,GACND,GAAS,EAGV,GAAIE,IACAC,EAMAC,EAJAC,EAAkB,iBAClBC,EAAe,uBACfC,GAAa,EACbC,EAAkB,GAGtB,IAAIR,GAIH,GAHAE,GAAKO,QAAQ,oBACbN,EAAUM,QAAQ,WAEdA,QAAQ,MAAMC,WAAWL,GAC5B,IACCD,EAAYK,QAAQ,MAAME,aAAaN,GAAiBR,WAEzD,MAAOI,SAKRE,GAAU,SAAUS,EAASC,GAC5B,GACIC,GAAMF,EAAQG,KAAOH,EAAQG,IAAIC,QAAQ,KAAO,EAAI,IAAM,KAC1DC,KACAC,EAAa,EAEjB,KAAKA,IAAcN,GAAQO,KAC1BF,EAAOG,KAAKF,EAAa,IAAMG,mBAAmBT,EAAQO,KAAKD,IAMhE,KAFA,GAAII,GAAe,WACfC,EAAQ,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,MAIzBM,EAAK0C,UAAY,SAAS5B,EAAOjB,EAAKtD,GAErCV,GACCY,IAAKmD,EAAe5D,EAAe,kBAAoB8E,EAAOjB,GAC9DY,KAAK,EACLG,QAAS1E,EACTmE,UAAWpE,GACT,SAAUyD,EAAKmB,EAAUrC,GACtBkB,IACJA,EAAMD,EAAgBjB,GAGvB,IAAIa,GAAeD,EAAUZ,EAEzBjC,IAAUA,EAASmD,EAAKL,EAAa,YAI3CW,EAAK2C,aAAe,SAAS7B,EAAOjB,EAAKtD,GAExCV,GACCY,IAAKT,EAAe,qBAAuB8E,EAAQ,QAAUjB,EAC7DY,KAAK,EACLG,QAAS1E,EACTmE,UAAWpE,GACT,SAAUyD,EAAKmB,EAAUrC,GACtBkB,IACJA,EAAMD,EAAgBjB,IAGnBjC,GAAUA,EAASmD,MAKtBhE,GACHkH,OAAOC,QAAUhE,EAGjB3B,OAAO4F,QAAU,GAAIjE"} -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var should = require("should"); 2 | var dweetIOClient = require("./../dweet.io.js"); 3 | 4 | var dweetio = new dweetIOClient(); 5 | 6 | dweetio.set_server(process.env.DWEET_SERVER || "https://dweet.io", false); 7 | 8 | var testData = { 9 | hello : "world", 10 | random: Math.floor((Math.random() * 50000) + 1) 11 | }; 12 | 13 | var mythingID = require("uuid").v4(); 14 | var generatedThing; 15 | var testLock = process.env.DWEET_LOCK; 16 | var testKey = process.env.DWEET_KEY; 17 | var testAlertCondition = "if(dweet.alertValue > 10) return 'TEST: Greater than 10'; if(dweet.alertValue < 10) return 'TEST: Less than 10';"; 18 | 19 | function checkValidDweetResponse(err, dweet) 20 | { 21 | if(err) 22 | { 23 | throw err; 24 | } 25 | 26 | dweet.should.have.properties("thing", "content", "created"); 27 | dweet.content.should.have.properties(testData); 28 | } 29 | 30 | function checkValidGetResponse(err, dweets) 31 | { 32 | if(err) 33 | { 34 | throw err; 35 | } 36 | 37 | dweets.should.be.an.Array; 38 | dweets.length.should.be.above(0); 39 | 40 | // Only check the first one 41 | checkValidDweetResponse(err, dweets[0]); 42 | } 43 | 44 | function shouldBeNoError(err) 45 | { 46 | if(err) 47 | { 48 | throw err; 49 | } 50 | } 51 | 52 | function shouldBeError(err) 53 | { 54 | if(!err) 55 | { 56 | throw "there should be an error, but we didnt' see one"; 57 | } 58 | } 59 | 60 | describe("locked", function() 61 | { 62 | this.timeout(5000); 63 | 64 | describe("#remove_lock()", function() 65 | { 66 | it("should return any response", function(done) 67 | { 68 | dweetio.remove_lock(testLock, testKey, function(err) 69 | { 70 | done(); 71 | }); 72 | }); 73 | }); 74 | 75 | describe("#lock()", function() 76 | { 77 | it("should return a valid response", function(done) 78 | { 79 | dweetio.lock(mythingID, testLock, testKey, function(err) 80 | { 81 | shouldBeNoError(err); 82 | done(); 83 | }); 84 | }); 85 | }); 86 | 87 | describe("#dweet_for() without a key", function() 88 | { 89 | it("should return an invalid response", function(done) 90 | { 91 | dweetio.dweet_for(mythingID, testData, function(err, dweet) 92 | { 93 | shouldBeError(err); 94 | done(); 95 | }); 96 | }); 97 | }); 98 | 99 | describe("#get_latest_dweet_for() without a key", function() 100 | { 101 | it("should return an invalid response", function(done) 102 | { 103 | dweetio.get_latest_dweet_for(mythingID, function(err, dweets) 104 | { 105 | shouldBeError(err); 106 | done(); 107 | }); 108 | }); 109 | }); 110 | 111 | describe("#dweet_for() with an invalid key", function() 112 | { 113 | it("should return an invalid response", function(done) 114 | { 115 | dweetio.dweet_for(mythingID, testData, "badKey", function(err, dweet) 116 | { 117 | shouldBeError(err); 118 | done(); 119 | }); 120 | }); 121 | }); 122 | 123 | describe("#get_latest_dweet_for() without an invalid key", function() 124 | { 125 | it("should return an invalid response", function(done) 126 | { 127 | dweetio.get_latest_dweet_for(mythingID, "badKey", function(err, dweets) 128 | { 129 | shouldBeError(err); 130 | done(); 131 | }); 132 | }); 133 | }); 134 | 135 | describe("#dweet_for() with a key", function() 136 | { 137 | it("should return a valid response", function(done) 138 | { 139 | dweetio.dweet_for(mythingID, testData, testKey, function(err, dweet) 140 | { 141 | checkValidDweetResponse(err, dweet); 142 | dweet.thing.should.equal(mythingID); 143 | done(); 144 | }); 145 | }); 146 | }); 147 | 148 | describe("#get_latest_dweet_for() with a key", function() 149 | { 150 | it("should return an valid response", function(done) 151 | { 152 | dweetio.get_latest_dweet_for(mythingID, testKey, function(err, dweets) 153 | { 154 | checkValidGetResponse(err, dweets); 155 | done(); 156 | }); 157 | }); 158 | }); 159 | 160 | describe("streaming dweets with a key", function() 161 | { 162 | this.timeout(20000); 163 | 164 | describe("#dweetio.listen_for()", function() 165 | { 166 | it("should hear dweets", function(done) 167 | { 168 | var testCount = 10; 169 | var listenFor = 5; // Listen for 5 dweets 170 | 171 | // Send a test dweet once every second for 10 times 172 | function sendTestDweet() 173 | { 174 | if(listenFor <= 0) 175 | { 176 | return; 177 | } 178 | 179 | dweetio.dweet_for(mythingID, testData, testKey, function() 180 | { 181 | testCount--; 182 | 183 | if(testCount > 0 && listenFor > 0) 184 | { 185 | setTimeout(sendTestDweet, 1000); 186 | } 187 | }); 188 | } 189 | 190 | dweetio.listen_for(mythingID, testKey, function(dweet) 191 | { 192 | if(listenFor <= 0) 193 | { 194 | return; 195 | } 196 | 197 | checkValidDweetResponse(null, dweet); 198 | 199 | listenFor--; 200 | 201 | if(listenFor == 0) 202 | { 203 | done(); 204 | } 205 | }); 206 | 207 | sendTestDweet(); 208 | }); 209 | }); 210 | }); 211 | 212 | describe("#set an alert", function() 213 | { 214 | it("should return an valid response", function(done) 215 | { 216 | dweetio.set_alert(mythingID, ["webmaster@dweet.io", "jim@buglabs.net"], testAlertCondition, testKey, function(err) 217 | { 218 | shouldBeNoError(err); 219 | done(); 220 | }); 221 | }); 222 | }); 223 | 224 | describe("#get an alert", function() 225 | { 226 | it("should return an valid response", function(done) 227 | { 228 | dweetio.get_alert(mythingID, testKey, function(err, response) 229 | { 230 | response.condition.should.equal(testAlertCondition); 231 | done(); 232 | }); 233 | }); 234 | }); 235 | 236 | describe("#dweet our alert data > 10", function() 237 | { 238 | it("should return a valid response", function(done) 239 | { 240 | dweetio.dweet_for(mythingID, {alertValue : 11}, testKey, function(err, dweet) 241 | { 242 | shouldBeNoError(err); 243 | done(); 244 | }); 245 | }); 246 | }); 247 | 248 | describe("#dweet our alert data < 10", function() 249 | { 250 | it("should return a valid response", function(done) 251 | { 252 | dweetio.dweet_for(mythingID, {alertValue : 5}, testKey, function(err, dweet) 253 | { 254 | shouldBeNoError(err); 255 | done(); 256 | }); 257 | }); 258 | }); 259 | 260 | describe("#dweet our alert data = 10", function() 261 | { 262 | it("should return a valid response", function(done) 263 | { 264 | dweetio.dweet_for(mythingID, {alertValue : 10}, testKey, function(err, dweet) 265 | { 266 | shouldBeNoError(err); 267 | done(); 268 | }); 269 | }); 270 | }); 271 | 272 | describe("#remove an alert", function() 273 | { 274 | it("should return a valid response", function(done) 275 | { 276 | dweetio.remove_alert(mythingID, testKey, function(err) 277 | { 278 | shouldBeNoError(err); 279 | done(); 280 | }); 281 | }); 282 | }); 283 | 284 | describe("#make sure alert is removed", function() 285 | { 286 | it("should return a 404 response", function(done) 287 | { 288 | dweetio.get_alert(mythingID, testKey, function(err, response) 289 | { 290 | shouldBeError(err); 291 | done(); 292 | }); 293 | }); 294 | }); 295 | 296 | describe("#unlock()", function() 297 | { 298 | it("should return a valid response", function(done) 299 | { 300 | dweetio.unlock(mythingID, testKey, function(err) 301 | { 302 | shouldBeNoError(err); 303 | done(); 304 | }); 305 | }); 306 | }); 307 | }); 308 | 309 | describe("unlocked", function() 310 | { 311 | this.timeout(5000); 312 | 313 | describe("dweeting", function() 314 | { 315 | describe("#dweet()", function() 316 | { 317 | it("should return a valid response", function(done) 318 | { 319 | dweetio.dweet(testData, function(err, dweet) 320 | { 321 | checkValidDweetResponse(err, dweet); 322 | generatedThing = dweet.thing; 323 | done(); 324 | }); 325 | }); 326 | }); 327 | 328 | describe("#dweet()", function() 329 | { 330 | it("should save the previously generated thing when dweeting again", function(done) 331 | { 332 | dweetio.dweet(testData, function(err, dweet) 333 | { 334 | checkValidDweetResponse(err, dweet); 335 | dweet.thing.should.equal(generatedThing); 336 | done(); 337 | }); 338 | }); 339 | }); 340 | 341 | describe("#dweet_for()", function() 342 | { 343 | it("should return a valid response", function(done) 344 | { 345 | dweetio.dweet_for(mythingID, testData, function(err, dweet) 346 | { 347 | checkValidDweetResponse(err, dweet); 348 | dweet.thing.should.equal(mythingID); 349 | done(); 350 | }); 351 | }); 352 | }); 353 | }); 354 | 355 | describe("reading dweets", function() 356 | { 357 | describe("#get_latest_dweet_for()", function() 358 | { 359 | it("should return a valid response", function(done) 360 | { 361 | dweetio.get_latest_dweet_for(mythingID, function(err, dweets) 362 | { 363 | checkValidGetResponse(err, dweets); 364 | done(); 365 | }); 366 | }); 367 | }); 368 | 369 | describe("#get_all_dweets_for()", function() 370 | { 371 | it("should return a valid response", function(done) 372 | { 373 | dweetio.get_all_dweets_for(mythingID, function(err, dweets) 374 | { 375 | checkValidGetResponse(err, dweets); 376 | done(); 377 | }); 378 | }); 379 | }); 380 | }); 381 | 382 | describe("streaming dweets", function() 383 | { 384 | this.timeout(20000); 385 | 386 | describe("#dweetio.listen_for()", function() 387 | { 388 | it("should hear dweets", function(done) 389 | { 390 | var testCount = 10; 391 | var listenFor = 5; // Listen for 5 dweets 392 | 393 | // Send a test dweet once every second for 10 times 394 | function sendTestDweet() 395 | { 396 | if(listenFor <= 0) 397 | { 398 | return; 399 | } 400 | 401 | dweetio.dweet_for(mythingID, testData, function() 402 | { 403 | testCount--; 404 | 405 | if(testCount > 0 && listenFor > 0) 406 | { 407 | setTimeout(sendTestDweet, 1000); 408 | } 409 | }); 410 | } 411 | 412 | dweetio.listen_for(mythingID, function(dweet) 413 | { 414 | if(listenFor <= 0) 415 | { 416 | return; 417 | } 418 | 419 | checkValidDweetResponse(null, dweet); 420 | 421 | listenFor--; 422 | 423 | if(listenFor == 0) 424 | { 425 | done(); 426 | } 427 | }); 428 | 429 | sendTestDweet(); 430 | }); 431 | }); 432 | }); 433 | }); -------------------------------------------------------------------------------- /dweet.io.js: -------------------------------------------------------------------------------- 1 | // dweet.io.js 2 | // http://dweet.io 3 | // (c) 2014 Jim Heising and Bug Labs, Inc. 4 | // dweet.io.js may be freely distributed under the MIT license. 5 | (function () { 6 | 7 | var isNode = true; 8 | 9 | // Is this loading into node.js? 10 | try { 11 | isNode = (require); 12 | } 13 | catch (e) { 14 | isNode = false; 15 | } 16 | 17 | var io; 18 | var request; 19 | 20 | var LAST_THING_NAME = "last-thing.dat"; 21 | var DWEET_SERVER = "https://dweet.io:443"; 22 | var STRICT_SSL = true; 23 | var REQUEST_TIMEOUT = 5000; 24 | var lastThing; 25 | 26 | if (isNode) { 27 | io = require("socket.io-client"); 28 | request = require("request"); 29 | 30 | if (require("fs").existsSync(LAST_THING_NAME)) { 31 | try { 32 | lastThing = require("fs").readFileSync(LAST_THING_NAME).toString(); 33 | } 34 | catch (e) { 35 | } 36 | } 37 | } 38 | else { 39 | request = function (options, callback) { 40 | var self = this; 41 | var src = options.url + (options.url.indexOf("?") + 1 ? "&" : "?"); 42 | var params = []; 43 | var param_name = ""; 44 | 45 | for (param_name in options.json) { 46 | params.push(param_name + "=" + encodeURIComponent(options.json[param_name])); 47 | } 48 | 49 | // Generate a unique callbackname 50 | var callbackName = "callback"; 51 | var index = 0; 52 | while (window.dweetCallback[callbackName + index]) { 53 | index++; 54 | } 55 | 56 | callbackName = callbackName + index; 57 | window.dweetCallback[callbackName] = function (data) { 58 | callback(null, data, data); 59 | }; 60 | 61 | // We're going to load everything with JSONP. 62 | params.push("callback=dweetCallback." + callbackName); 63 | params.push("_" + "=" + Date.now()); 64 | 65 | src += params.join("&"); 66 | 67 | dweet_script_loader(src, function (script) { 68 | script.parentNode.removeChild(script); 69 | window.dweetCallback[callbackName] = undefined; 70 | delete window.dweetCallback[callbackName]; 71 | }); 72 | }; 73 | 74 | window.dweetCallback = {}; 75 | 76 | (function () { 77 | var re = /ded|co/; 78 | var onload = 'onload'; 79 | var onreadystatechange = 'onreadystatechange'; 80 | var readyState = 'readyState'; 81 | 82 | var load = function (src, fn) { 83 | var script = document.createElement('script'); 84 | script[onload] = script[onreadystatechange] = function () { 85 | if (!this[readyState] || re.test(this[readyState])) { 86 | script[onload] = script[onreadystatechange] = null; 87 | fn && fn(script); 88 | script = null; 89 | } 90 | }; 91 | script.async = true; 92 | script.src = src; 93 | document.body.appendChild(script); 94 | }; 95 | window.dweet_script_loader = function (srces, fn) { 96 | if (typeof srces == 'string') { 97 | load(srces, fn); 98 | return; 99 | } 100 | var src = srces.shift(); 101 | load(src, function (script) { 102 | if (srces.length) { 103 | window.dweet_script_loader(srces, fn); 104 | } 105 | else { 106 | fn && fn(script); 107 | } 108 | }); 109 | }; 110 | })(); 111 | } 112 | 113 | function isArray(obj) { 114 | return Object.prototype.toString.call( obj ) === '[object Array]' 115 | } 116 | 117 | function isFunction(obj) { 118 | return typeof obj === 'function'; 119 | } 120 | 121 | var dweetioClient = function () { 122 | var self = this; 123 | var socket; 124 | var listenCallbacks = {}; 125 | var currentThing = lastThing; 126 | 127 | function normalizeDweet(dweet) { 128 | if (dweet.created) { 129 | dweet.created = new Date(dweet.created); 130 | } 131 | 132 | return dweet; 133 | } 134 | 135 | function normalizeDweets(dweets) { 136 | if (dweets instanceof Array) { 137 | for (var index = 0; index < dweets.length; index++) { 138 | var dweet = dweets[index]; 139 | normalizeDweet(dweet); 140 | } 141 | } 142 | else { 143 | normalizeDweet(dweets); 144 | } 145 | 146 | return dweets; 147 | } 148 | 149 | function parseBody(body) { 150 | var responseData; 151 | 152 | try { 153 | if (typeof body == 'string' || body instanceof String) { 154 | responseData = JSON.parse(body); 155 | } 156 | else { 157 | responseData = body; 158 | } 159 | } 160 | catch (e) { 161 | } 162 | 163 | return responseData; 164 | } 165 | 166 | function processResponse(body) { 167 | var err; 168 | 169 | var responseData = parseBody(body); 170 | 171 | if (!responseData) { 172 | err = new Error("server returned an invalid response"); 173 | } 174 | else if (responseData["this"] == "failed") { 175 | err = new Error(responseData["because"]); 176 | } 177 | 178 | return err; 179 | } 180 | 181 | function createKeyedURL(url, key) { 182 | if (key) { 183 | return url + (url.indexOf("?") + 1 ? "&" : "?") + "key=" + encodeURIComponent(key); 184 | } 185 | 186 | return url; 187 | } 188 | 189 | function processDweetResponse(err, callback, body) { 190 | var responseData = parseBody(body); 191 | 192 | if (!err) { 193 | err = processResponse(responseData); 194 | } 195 | 196 | if (responseData && responseData["with"]) { 197 | if (callback) callback(err, normalizeDweets(responseData["with"])); 198 | } 199 | else { 200 | if (callback) callback("no response from server", undefined); 201 | } 202 | } 203 | 204 | self.set_server = function (server, strictSSL) { 205 | DWEET_SERVER = server; 206 | STRICT_SSL = strictSSL; 207 | 208 | if (isNode) { 209 | if (strictSSL) 210 | require('https').globalAgent.options.rejectUnauthorized = true; 211 | else 212 | require('https').globalAgent.options.rejectUnauthorized = false; 213 | } 214 | } 215 | 216 | self.dweet = function (data, callback) { 217 | if (currentThing) { 218 | self.dweet_for(currentThing, data, callback); 219 | } 220 | else { 221 | request({ 222 | url: DWEET_SERVER + "/dweet", 223 | jar: true, 224 | method: "POST", 225 | followAllRedirects: true, 226 | timeout: REQUEST_TIMEOUT, 227 | strictSSL: STRICT_SSL, 228 | json: data 229 | }, function (err, response, body) { 230 | var responseData = parseBody(body); 231 | 232 | if (responseData["with"] && responseData["with"].thing != currentThing) { 233 | currentThing = responseData["with"].thing; 234 | 235 | if (isNode) { 236 | require("fs").writeFile(LAST_THING_NAME, currentThing); 237 | } 238 | } 239 | 240 | processDweetResponse(err, callback, responseData); 241 | }); 242 | } 243 | }; 244 | 245 | self.dweet_for = function (thing, data, key, callback) { 246 | if (isFunction(key)) { 247 | callback = key; 248 | key = null; 249 | } 250 | 251 | request({ 252 | url: createKeyedURL(DWEET_SERVER + "/dweet/for/" + thing, key), 253 | jar: true, 254 | method: "POST", 255 | followAllRedirects: true, 256 | timeout: REQUEST_TIMEOUT, 257 | strictSSL: STRICT_SSL, 258 | json: data 259 | }, function (err, response, body) { 260 | processDweetResponse(err, callback, body); 261 | }); 262 | } 263 | 264 | self.get_latest_dweet_for = function (thing, key, callback) { 265 | if (isFunction(key)) { 266 | callback = key; 267 | key = null; 268 | } 269 | 270 | request({ 271 | url: createKeyedURL(DWEET_SERVER + "/get/latest/dweet/for/" + thing, key), 272 | jar: true, 273 | timeout: REQUEST_TIMEOUT, 274 | strictSSL: STRICT_SSL 275 | }, function (err, response, body) { 276 | processDweetResponse(err, callback, body); 277 | }); 278 | } 279 | 280 | self.get_all_dweets_for = function (thing, key, callback) { 281 | if (isFunction(key)) { 282 | callback = key; 283 | key = null; 284 | } 285 | 286 | request({ 287 | url: createKeyedURL(DWEET_SERVER + "/get/dweets/for/" + thing, key), 288 | jar: true, 289 | timeout: REQUEST_TIMEOUT, 290 | strictSSL: STRICT_SSL 291 | }, function (err, response, body) { 292 | processDweetResponse(err, callback, body); 293 | }); 294 | } 295 | 296 | self.get_key_for = function (account, thingname, callback) { 297 | request({ 298 | url: createKeyedURL(DWEET_SERVER + "/get/key/for/" + account + '/' + thingname), 299 | jar: true, 300 | timeout: REQUEST_TIMEOUT, 301 | strictSSL: STRICT_SSL 302 | }, function (err, response, body) { 303 | processDweetResponse(err, callback, body); 304 | }); 305 | } 306 | 307 | self.create_key_for = function (account, thingname, callback) { 308 | request({ 309 | url: createKeyedURL(DWEET_SERVER + "/create/key/for/" + account + '/' + thingname), 310 | jar: true, 311 | timeout: REQUEST_TIMEOUT, 312 | strictSSL: STRICT_SSL 313 | }, function (err, response, body) { 314 | processDweetResponse(err, callback, body); 315 | }); 316 | } 317 | 318 | self.get_keys_for_account = function (account, callback) { 319 | request({ 320 | url: createKeyedURL(DWEET_SERVER + "/get/keys/for/" + account), 321 | jar: true, 322 | timeout: REQUEST_TIMEOUT, 323 | strictSSL: STRICT_SSL 324 | }, function (err, response, body) { 325 | processDweetResponse(err, callback, body); 326 | }); 327 | } 328 | 329 | self.get_keys_for_account = function (account, startPosition, endPosition, callback) { 330 | request({ 331 | url: createKeyedURL(DWEET_SERVER + "/get/keys/for/" + account + '?startPosition=' + startPosition + '&endPosition=' + endPosition), 332 | jar: true, 333 | timeout: REQUEST_TIMEOUT, 334 | strictSSL: STRICT_SSL 335 | }, function (err, response, body) { 336 | processDweetResponse(err, callback, body); 337 | }); 338 | } 339 | 340 | 341 | self.listen_for = function (thing, key, callback) { 342 | if (isFunction(key)) { 343 | callback = key; 344 | key = null; 345 | } 346 | 347 | // Initialize our callback list 348 | if (!listenCallbacks[thing]) { 349 | listenCallbacks[thing] = []; 350 | } 351 | 352 | // Add this to our callbacks 353 | if (listenCallbacks[thing].indexOf(callback) == -1) { 354 | listenCallbacks[thing].push(callback); 355 | } 356 | 357 | function createSocket() { 358 | socket = io.connect(DWEET_SERVER + "/stream"); 359 | 360 | socket.on("connect", function () { 361 | // Subscribe to all of the things that we might have asked for before connecting 362 | for (var id in listenCallbacks) { 363 | socket.emit("subscribe", {thing: id, key: key}); 364 | } 365 | }); 366 | 367 | socket.on("new_dweet", function (msg) { 368 | if (listenCallbacks[msg.thing]) { 369 | normalizeDweets(msg); 370 | 371 | var callbacks = listenCallbacks[msg.thing]; 372 | for (var index = 0; index < callbacks.length; index++) { 373 | callbacks[index](msg); 374 | } 375 | } 376 | }); 377 | } 378 | 379 | if (!socket) { 380 | if (isNode) { 381 | createSocket(); 382 | } 383 | else { 384 | dweet_script_loader([DWEET_SERVER + "/socket.io/socket.io.js"], function () { 385 | io = window.io; 386 | createSocket(); 387 | }); 388 | } 389 | } 390 | if (socket) { 391 | socket.emit("subscribe", {thing: thing, key: key}); 392 | } 393 | } 394 | 395 | self.stop_listening = function () { 396 | listenCallbacks = {}; 397 | 398 | if (socket) { 399 | socket.disconnect(); 400 | socket = undefined; 401 | } 402 | } 403 | 404 | self.stop_listening_for = function (thing) { 405 | listenCallbacks[thing] = undefined; 406 | delete listenCallbacks[thing]; 407 | 408 | if (socket) { 409 | socket.emit("unsubscribe", {thing: thing}); 410 | } 411 | } 412 | 413 | self.lock = function (thing, lock, key, callback) { 414 | request({ 415 | url: DWEET_SERVER + "/lock/" + thing + "?lock=" + lock + "&key=" + key, 416 | jar: true, 417 | timeout: REQUEST_TIMEOUT, 418 | strictSSL: STRICT_SSL 419 | }, function (err, response, body) { 420 | if (!err) { 421 | err = processResponse(body); 422 | } 423 | 424 | if (callback) callback(err); 425 | }); 426 | } 427 | 428 | self.unlock = function (thing, key, callback) { 429 | request({ 430 | url: createKeyedURL(DWEET_SERVER + "/unlock/" + thing, key), 431 | jar: true, 432 | timeout: REQUEST_TIMEOUT, 433 | strictSSL: STRICT_SSL 434 | }, function (err, response, body) { 435 | if (!err) { 436 | err = processResponse(body); 437 | } 438 | 439 | if (callback) callback(err); 440 | }); 441 | } 442 | 443 | self.remove_lock = function (lock, key, callback) { 444 | request({ 445 | url: DWEET_SERVER + "/remove/lock/" + lock + "?key=" + key, 446 | jar: true, 447 | timeout: REQUEST_TIMEOUT, 448 | strictSSL: STRICT_SSL 449 | }, function (err, response, body) { 450 | if (!err) { 451 | err = processResponse(body); 452 | } 453 | 454 | if (callback) callback(err); 455 | }); 456 | } 457 | 458 | self.set_alert = function(thing, recipients, condition, key, callback) 459 | { 460 | if(isArray(recipients)) 461 | { 462 | recipients = recipients.join(); 463 | } 464 | 465 | request({ 466 | url: createKeyedURL(DWEET_SERVER + "/alert/" + encodeURIComponent(recipients) + "/when/" + thing + "/" + encodeURIComponent(condition), key), 467 | jar: true, 468 | timeout: REQUEST_TIMEOUT, 469 | strictSSL: STRICT_SSL 470 | }, function (err, response, body) { 471 | if (!err) { 472 | err = processResponse(body); 473 | } 474 | 475 | if (callback) callback(err); 476 | }); 477 | } 478 | 479 | self.get_alert = function(thing, key, callback) 480 | { 481 | request({ 482 | url: createKeyedURL(DWEET_SERVER + "/get/alert/for/" + thing, key), 483 | jar: true, 484 | timeout: REQUEST_TIMEOUT, 485 | strictSSL: STRICT_SSL 486 | }, function (err, response, body) { 487 | if (!err) { 488 | err = processResponse(body); 489 | } 490 | 491 | var responseData = parseBody(body); 492 | 493 | if (callback) callback(err, responseData["with"]); 494 | }); 495 | } 496 | 497 | self.remove_alert = function(thing, key, callback) 498 | { 499 | request({ 500 | url: DWEET_SERVER + "/remove/alert/for/" + thing + "?key=" + key, 501 | jar: true, 502 | timeout: REQUEST_TIMEOUT, 503 | strictSSL: STRICT_SSL 504 | }, function (err, response, body) { 505 | if (!err) { 506 | err = processResponse(body); 507 | } 508 | 509 | if (callback) callback(err); 510 | }); 511 | } 512 | }; 513 | 514 | if (isNode) { 515 | module.exports = dweetioClient; 516 | } 517 | else { 518 | window.dweetio = new dweetioClient(); 519 | } 520 | })(); 521 | --------------------------------------------------------------------------------