Skip to content

Commit

Permalink
moved over to gulp and mocha. going to put on npm.
Browse files Browse the repository at this point in the history
  • Loading branch information
larryosborn committed Oct 9, 2014
1 parent 98ab8ff commit 1dae9a1
Show file tree
Hide file tree
Showing 11 changed files with 1,589 additions and 207 deletions.
72 changes: 0 additions & 72 deletions Gruntfile.js

This file was deleted.

38 changes: 38 additions & 0 deletions gulpfile.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
gulp = require 'gulp'
coffee = require 'gulp-coffee'
coffeelint = require 'gulp-coffeelint'
notify = require 'gulp-notify'
mochaPhantomJS = require 'gulp-mocha-phantomjs'
uglify = require 'gulp-uglify'
rename = require 'gulp-rename'

handleErrors = ->
args = Array.prototype.slice.call(arguments)
notify.onError(title: 'Compile Error', message: '<%= error.message %>').apply(this, args)
this.emit 'end'

gulp.task 'default', ['build']

gulp.task 'coffee', ->
gulp.src './src/jsonp.coffee'
.pipe coffeelint()
.pipe coffeelint.reporter()
.pipe coffee().on('error', handleErrors)
.pipe gulp.dest('lib')

gulp.task 'build', ['coffee'], ->
gulp.src './lib/jsonp.js'
.pipe uglify()
.pipe rename(extname: '.min.js')
.pipe gulp.dest('lib')


gulp.task 'test', ['build'], ->
gulp.src './spec/**/*.coffee'
.pipe coffeelint()
.pipe coffeelint.reporter()
.pipe coffee(bare: true).on('error', handleErrors)
.pipe gulp.dest('test')
gulp.src './test/index.html'
.pipe mochaPhantomJS()

92 changes: 92 additions & 0 deletions lib/jsonp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
(function() {
var JSONP, createElement, encode, noop, objectToURI, random, randomString;

createElement = function(tag) {
return window.document.createElement(tag);
};

encode = window.encodeURIComponent;

random = Math.random;

JSONP = function(options) {
var callback, done, head, params, script;
options = options ? options : {};
params = {
data: options.data || {},
error: options.error || noop,
success: options.success || noop,
url: options.url || ''
};
if (params.url.length === 0) {
throw new Error('MissingUrl');
}
done = false;
callback = params.data[options.callbackName || 'callback'] = 'jsonp_' + randomString(15);
window[callback] = function(data) {
params.success(data);
try {
return delete window[callback];
} catch (_error) {
window[callback] = void 0;
return void 0;
}
};
script = createElement('script');
script.src = params.url;
script.src += params.url.indexOf('?' === -1) ? '?' : '&';
script.src += objectToURI(params.data);
script.async = true;
script.onerror = function(evt) {
return params.error({
url: script.src,
event: evt
});
};
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete')) {
done = true;
script.onload = script.onreadystatechange = null;
if (script && script.parentNode) {
return script.parentNode.removeChild(script);
}
}
};
head = head || window.document.getElementsByTagName('head')[0];
return head.appendChild(script);
};

noop = function() {
return void 0;
};

randomString = function(length) {
var str;
str = '';
while (str.length < length) {
str += random().toString(36)[2];
}
return str;
};

objectToURI = function(obj) {
var data, key, value;
data = [];
for (key in obj) {
value = obj[key];
data.push(encode(key) + '=' + encode(value));
}
return data.join('&');
};

if ((typeof define !== "undefined" && define !== null) && define.amd) {
define(function() {
return JSONP;
});
} else if ((typeof module !== "undefined" && module !== null) && module.exports) {
module.exports = JSONP;
} else {
this.JSONP = JSONP;
}

}).call(this);
1 change: 1 addition & 0 deletions lib/jsonp.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
{
"name": "JSONP",
"name": "browser-jsonp",
"description": "A slim JSONP request library for Javascript.",
"version": "1.0.0",
"keywords": [
"jsonp",
"javascript"
],
"author": "Larry Osborn",
"main": "lib/jsonp.js",
"scripts": {
"test": "grunt test"
"test": "gulp test"
},
"homepage": "https://github.com/larryosborn/JSONP",
"bugs": "https://github.com/larryosborn/JSONP/issues",
"repository": {
"type": "git",
"url": "https://github.com/larryosborn/JSONP"
"url": "git://github.com/larryosborn/JSONP.git"
},
"licenses": [
{
"type": "Apache-2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0"
}
],
"licenses": "MIT",
"devDependencies": {
"grunt": "*",
"grunt-contrib-clean": "^0.5.0",
"grunt-contrib-coffee": "^0.10.1",
"grunt-contrib-jasmine": "~0.5.0",
"grunt-contrib-jshint": "^0.10.0",
"grunt-contrib-uglify": "^0.4.0",
"grunt-contrib-watch": "^0.6.1"
"coffee-script": "^1.8.0",
"gulp": "^3.8.8",
"gulp-coffee": "^2.2.0",
"gulp-coffeelint": "^0.4.0",
"gulp-mocha-phantomjs": "^0.5.1",
"gulp-notify": "^2.0.0",
"gulp-rename": "^1.2.0",
"gulp-uglify": "^1.0.1",
"mocha": "^1.21.4"
}
}
57 changes: 57 additions & 0 deletions spec/jsonp.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

describe 'JSONP: Openweathermap.org API request', ->

it 'should create a remote request to an API', (done) ->

value = 'fail'

JSONP
url: 'http://api.openweathermap.org/data/2.5/weather'
data: q: 'London,UK'
success: (data) ->
if typeof data is 'object'
value = 'success'
expect(value).to.equal('success')
done()
error: (data) ->
expect(value).to.equal('success')
done()


describe 'JSONP: Forced failure', ->

it 'should force a failure of a remote request', (done) ->

value = 'fail'

JSONP
url: 'http://j3lk5f0dl3m0f3lt0fdlem30gugmtu5p5mgmdu34lflfm30fulddu93ldfu3ldfg2ufogk234-fu23nf3/'
data: q: 'London,UK'
success: (data) ->
expect(value).to.equal('success')
done()
error: (data) ->
value = 'success'
expect(value).to.equal('success')
done()

describe 'JSONP: Missing url parameter', ->

it 'should fail because of missing url parameter', (done) ->

flag = false
value = 'fail'

try
JSONP()
catch e
if e.message is 'MissingUrl'
flag = true
value = 'success'
else
flag = true
throw e

expect(value).to.equal('success')
done()

Loading

0 comments on commit 1dae9a1

Please sign in to comment.