Last active
August 29, 2015 14:22
-
-
Save dasher/756a067cf9c090914d86 to your computer and use it in GitHub Desktop.
Gitter Desktop App for testing the API
This file contains 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 characters
Alora, | |
Here is some code I quickly hacked up to help somebody in the gitterHQ/gitter channel in Gitter who was having a problem with his client. This example shows how you can play with the Gitter desktop app and hack quickly against the Gitter API. | |
It's not pretty - quick and dirty but it should work and gives you an idea of how you can play in the desktop app. | |
Open up the desktop app and select the Gitter Menu -> developer tools. If the desktop app crashes at this point - you have multiple instances of Gitter open - the desktop app doesn't always close down properly.. On Windows open up the task manager and kill any lingering processes. Start gitter again and try to open the dev tools. | |
When it's open.. click console and then you can start pasting from the code from the JS. | |
It's lightly documented and it should work but your milage may vary. | |
It should be obvious what it's doing. | |
Any headaches or questions - ask in the gitterHQ/gitter tab |
This file contains 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 characters
var dummyHandle = function() { | |
// Just here to help with debugging if its needed | |
//console.log(arguments); | |
} | |
var fetchAttibuteByName = function(element, name, regex) { | |
// Fetch a named attribute from a DOM element - passing in the element, the attribute name | |
// and an optional regex to parse the attribute name by | |
if (Array.isArray(element)) { | |
// helper if you're using a quick and dirty selector which returns an array of items | |
element = element[0]; | |
} | |
var attributes = element.attributes.getNamedItem(name); | |
if (regex) { | |
return attributes.value.match(regex)[1]; | |
} else { | |
return attributes; | |
} | |
} | |
var buildRequest = function(url, token, msg) { | |
// Builds and executes the http request - send is wrapped to avoid crashes with this types of wrapped desktop apps | |
var xmlhttp = new XMLHttpRequest(); | |
xmlhttp.onreadystatechange = dummyHandle; | |
xmlhttp.open("GET", url, true); | |
xmlhttp.setRequestHeader("Authorization", "Bearer "+token); | |
try { | |
xmlhttp.send(); | |
return xmlhttp; | |
} catch(err) { | |
console.log(msg, arguments); | |
return null; | |
} | |
} | |
// Fetch a valid token from the iframe attribute for querying the server | |
var token = fetchAttibuteByName(document.getElementsByTagName("iFrame"), "nwuseragent", /Token\/(\w+)/); | |
// A little debug to help know what's happening | |
console.log("Token: %s",s); | |
// We need the userId - so we do a quick request to fetch the user data | |
var xmlhttp = buildRequest("https://api.gitter.im/v1/user", token, "Error attemping to fetch user:"); | |
var userId = null; | |
try { | |
// Decode in a try - better safe than crashed | |
var userData = JSON.parse(xmlhttp.responseText); | |
console.log("GET v1/user returned", userData); | |
userId = userData[0].id; | |
} catch (err) { | |
console.log("Error trying to decode reponse", err); | |
} | |
var rooms = null; | |
// Build a request to fetch the rooms a user is in - using the userId from the earlier request | |
var xmlhttp = buildRequest("https://api.gitter.im/v1/user/"+userId+"/rooms", token, "Error attemping to fetch rooms for ["+userId+"]: "); | |
try { | |
// Decode in a try - better safe than crashed | |
rooms = JSON.parse(xmlhttp.responseText); | |
console.log("Rooms returned:", rooms); | |
} catch (err) { | |
console.log("Error trying to decode reponse",err); | |
} | |
console.log("Listing Rooms that are oneToOne"); | |
rooms.forEach( | |
function(room){ | |
if (room.oneToOne){ | |
console.log(room) | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment