forked from larryosborn/JSONP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved over to gulp and mocha. going to put on npm.
- Loading branch information
1 parent
98ab8ff
commit 1dae9a1
Showing
11 changed files
with
1,589 additions
and
207 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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() | ||
|
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
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); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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" | ||
} | ||
} |
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
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() | ||
|
Oops, something went wrong.