Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit.

  • Loading branch information...
commit f03b283444a675b8bb1b2b0b63ed221e10dd78f6 0 parents
@scottgonzalez authored
Showing with 123 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE.txt
  2. +103 −0 browserstack.js
20 MIT-LICENSE.txt
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Scott González http://scottgonzalez.com
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
103 browserstack.js
@@ -0,0 +1,103 @@
+var http = require( "http" ),
+ querystring = require( "querystring" );
+
+function extend( a, b ) {
+ for ( var p in b ) {
+ a[ p ] = b[ p ];
+ }
+
+ return a;
+};
+
+function Client( settings) {
+ if ( !settings.username ) {
+ throw new Error( "Username is required." );
+ }
+ if ( !settings.password ) {
+ throw new Error( "Password is required." );
+ }
+
+ this._username = settings.username;
+ this._password = settings.password;
+ this._authHeader = "Basic " +
+ new Buffer( this._username + ":" + this._password ).toString( "base64" );
+}
+
+extend( Client.prototype, {
+ getBrowsers: function( fn ) {
+ this.request({
+ path: this.path( "/browsers" )
+ }, fn );
+ },
+
+ createWorker: function( options, fn ) {
+ var data = querystring.stringify( options );
+ this.request({
+ path: this.path( "/worker" ),
+ method: "POST"
+ }, data, fn );
+ },
+
+ terminateWorker: function( id, fn ) {
+ this.request({
+ path: this.path( "/worker/" + id ),
+ method: "DELETE"
+ }, fn );
+ },
+
+ path: function( path ) {
+ return "/1" + path;
+ },
+
+ request: function( options, data, fn ) {
+ if ( typeof data === "function" ) {
+ fn = data;
+ data = null;
+ }
+
+ var headers = {
+ authorization: this._authHeader
+ };
+ headers[ "content-length" ] = typeof data === "string" ? data.length : 0;
+
+ var req = http.request( extend({
+ host: "api.browserstack.com",
+ port: 80,
+ method: "GET",
+ headers: headers
+ }, options ), function( res ) {
+ var response = "";
+ res.setEncoding( "utf8" );
+ res.on( "data", function( chunk ) {
+ response += chunk;
+ });
+ res.on( "end", function() {
+ if ( res.statusCode !== 200 ) {
+ var message;
+ if ( res.headers[ "content-type" ].indexOf( "json" ) !== -1 ) {
+ message = JSON.parse( response ).message;
+ } else {
+ message = response;
+ }
+ if ( !message && res.statusCode === 403 ) {
+ message = "Forbidden";
+ }
+ fn( new Error( message ) );
+ } else {
+ fn( null, JSON.parse( response ) );
+ }
+ });
+ });
+
+ if ( data ) {
+ req.write( data );
+ }
+ req.end();
+ }
+});
+
+module.exports = {
+ createClient: function( settings ) {
+ return new Client( settings );
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.